【问题标题】:Session ID not the same in my Java EE application我的 Java EE 应用程序中的会话 ID 不同
【发布时间】:2019-07-19 07:15:57
【问题描述】:

我编写了一个带有自定义登录系统的应用程序。然后为它编写了我自己的安全过滤器来设置可以访问的区域。然而,我总是被重定向到登录页面,然后是登录主页的索引页面。我发现会话 ID 与我登录时和尝试使用受限制的东西时不同。这是我的代码:

public class securtityFilter implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {
    //To change body of implemented methods use File | Settings | File Templates.
}

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
            // if there is no userBean, then they have not gone through
            // the login, so kick them to the login page
            if(null==req.getSession().getAttribute("username"))
            {
                ((HttpServletResponse)servletResponse).sendRedirect("../Login.jsp");
                System.out.println("Redirected - No session");

            }
                    // otherwise, let them go to the page/resource they want
                    filterChain.doFilter(servletRequest, servletResponse);
                System.out.println("Gone through Filter");

              //  System.out.println("In Filter Servlet: "+ req.getSession().getId());

            }

public void destroy() {
    //To change body of implemented methods use File | Settings | File Templates.                   
}
}

这是我的 web.xml 文件:

  <filter>
     <filter-name>SecurityFilter</filter-name>
     <filter-class>filters.securtityFilter</filter-class>   
</filter>
<filter-mapping>
    <filter-name>SecurityFilter</filter-name>
    <url-pattern>/add/*</url-pattern>
</filter-mapping>

【问题讨论】:

  • 您确定登录页面设置了用户名属性吗?
  • 在登录 servlet 中是:while(rs.next()) { HttpSession session = request.getSession(true);字符串 tmp = rs.getString(1); System.out.println(tmp); session.setAttribute("用户名",tmp); /*角色 = rs.getInt("级别"); session.setAttribute("role",role);*/ count++; }

标签: java session authentication servlets


【解决方案1】:

在您的登录 servlet 中有

while (rs.next())
{
  HttpSession session = request.getSession(true);
  String tmp = rs.getString(1);
  System.out.println(tmp);
  session.setAttribute("username", tmp); 
  count++;
 }

所以如果你的会话中没有用户名属性,那是因为这个代码块没有被执行。我假设您正在循环访问数据库查询的结果,因此请检查您正在执行的实际查询是否返回任何结果。

【讨论】:

    【解决方案2】:

    尝试改变

    if(null==req.getSession().getAttribute("username"))
    

    HttpSession ses = req.getSession(false); // null if no current
    if(null == ses || 
       null == ses.getAttribute("username"))
    

    这样它就不会在您的过滤器中创建新会话。让登录页面创建会话。

    【讨论】:

    • 这是现在弹出的错误:25-Jul-2009 22:46:30 org.apache.jasper.runtime.JspFactoryImpl internalGetPageContext 严重:异常初始化页面上下文 java.lang.IllegalStateException:不能提交响应后创建会话
    • 在重定向后立即添加返回语句。并确保您没有对过滤器链中的 HttpResponse 做任何事情
    • 你是什么意思?抱歉,我是 J2EE 新手。
    • 无论你有 sendRedirect() 调用,只需在它们之后添加一个返回。它之所以抱怨,是因为除了您想要的重定向之外,还有一些东西正在写入 HttpResponse。
    • 好的谢谢你,但它没有工作,我仍然被重定向到登录页面。
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 2011-07-26
    • 2023-03-30
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多