Clear Spring Session for login?

Update . I expected this issue to be related to the specific version of Spring I was using, but Rob's answer indicates that this is probably something specific to my environment. We found a workaround for this particular issue (a filter that manually clears the session on login), so I answered Rob's answer correctly.


In my Spring application, my sessions persist between logins. I would like the session to be cleared on login.

I am using Spring Security 2.0.4 (not asking) and my security configuration looks something like this:

<security:http ...
session-fixation-protection="newSession">
...
        <security:logout invalidate-session="true" logout-success-url="/login.html" />
</security:http>

      

I was under the impression that session-fixation-protection = "newSession" will clear sessions from user login. Another interesting point is that sessions are cleared on logout, so invalidate-session = "true" has the desired effect.

When testing, I use the following methods:

@RequestMapping(value = "writeSession")
String writeSession(HttpServletRequest request) {
  request.getSession().setAttribute("username", MySecurityService.getLoggedInUsername());
  ...
}

@RequestMapping(value = "readSession")
String readSession(HttpServletRequest request) {
  log.info("Current username: " + request.getSession().getAttribute("username");
  ...
}

      

Then I:

  • Login as user1
  • Visit the writeSession function (sets the session username of user "user1")
  • Visit readSession (log output: Current username: user1)
  • Login as user2 (no logout)
  • Visit readSession (log output: Current username: user1 )

Note that if I exit the steps between steps 3 and 4, I get the expected results (Current username: null)

+3


source to share


1 answer


I've created a sample that shows that in general this should work. See the so-clear-spring-session-on-login section of this git repo for a working example.

There can be several things that can cause problems depending on the rest of your configuration.



  • Can you share the rest of your Spring security configuration? For example, if you are switching between HTTP and HTTPS, it might be switching which session is in use. You can refer to the Spring Security FAQ for more details on what can go wrong with sessions.

  • What does the MySecurityService implementation look like?

  • What does your web.xml look like?

+1


source







All Articles