httpservletrequest - create new session / change session Id

后端 未结 2 1454
渐次进展
渐次进展 2020-12-24 06:11

I\'m maintaining a Java web application.

Looking into the login code it gets an HttpSession out of HttpServletRequest via the getSession() method of HttpServletReque

2条回答
  •  失恋的感觉
    2020-12-24 06:37

    The Servlet 3.0 API doesn't allow you to change the session id on an existing session. Typically, to protect against session fixation, you'll want to just create a new one and invalidate the old one as well.

    You can invalidate a session like this

    request.getSession(false).invalidate();
    

    and then create a new session with

    getSession(true) (getSession() should work too)

    Obviously, if you have an data in the session that you want to persist, you'll need to copy it from the first session to the second session.

    Note, for session fixation protection, it's commonly considered okay to just do this on the authentication request. But a higher level of security involves a tossing the old session and making a new session for each and every request.

提交回复
热议问题