简体   繁体   English

会话ID在我的Java EE应用程序中不一样

[英]Session ID not the same in my Java EE application

I've written a application with a custom login system. 我用自定义登录系统编写了一个应用程序。 And then written my own security filter for it which sets the area that can be accessed. 然后为它编写自己的安全过滤器,设置可以访问的区域。 Yet I always get redirected to the login page and then to the index page with is the logged in home page. 然而,我总是被重定向到登录页面然后到索引页面,登录主页。 I have discovered that the session ID is different from when I login to when I try to use something that is restricted. 我发现会话ID与我登录时尝试使用受限制的内容时不同。 Here is my code: 这是我的代码:

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.                   
}
}

Here is my web.xml file: 这是我的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>

Try changing 尝试改变

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

to

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

so that it never creates a new session inside your filter. 这样它就不会在过滤器中创建新的会话。 Let the login page create the sessions. 让登录页面创建会话。

In your login servlet you have 在您的登录servlet中

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

So if you have no username attribute in your session, it is because this code block is not being executed. 因此,如果您的会话中没有username属性,那是因为此代码块未被执行。 I assume that you are looping through the results of a database query, so check whether the actual query that you are executing returns any results. 我假设您循环遍历数据库查询的结果,因此请检查您正在执行的实际查询是否返回任何结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM