Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

servlets - HttpSession invalidate is redirecting to login page

Hi I'm using servlet filter to change session ID on every request in order to avoid session fixation. My problem is when the method doFilter ends the application is redirected to login page. I just want to invalidate and create new session, without redirect. I don't have any other filter.

There is my doFilter code:

HttpSession session= httpServletReq.getSession();
    
if(session!=null){
  User u = session.getAttribute("user");
  session.invalidate();

  HttpSession newSession = httpServletReq.getSession(true);
  newSession.setAttribute("user", u);
}

chain.doFilter(req, resp);

The pattern on filter is ***.xhtml**

Why am I getting redirect to login?

Is it ok to change session ID on a filter?

Thanks

question from:https://stackoverflow.com/questions/66055702/httpsession-invalidate-is-redirecting-to-login-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

change session ID on every request in order to avoid session fixation

You should do exactly that instead of invalidating the session. You can use HttpServletRequest#changeSessionId() for this.

HttpSession session = httpRequest.getSession(false);
    
if (session != null) {
    httpRequest.changeSessionId();
}

chain.doFilter(request, response);

Do note that HttpServletRequest#getSession() defaults to auto-creating the session, so it actually never returns null. You need to explicitly pass false to HttpServletRequest#getSession(boolean).

That said, you don't necessarily need to perform this on every request, it's sufficient to perform that only at the moment when you login the user.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...