Results 1 to 6 of 6

Thread: Spring security concurrent-session and HttpSessionListener problem

  1. #1
    Join Date
    Aug 2010
    Posts
    26

    Question Spring security concurrent-session and HttpSessionListener problem

    I have implemented HttpSessionListener and it works fine except for the case when a logged in user concurrently logs in a second time. Spring terminates the first session correctly, but the destroySession event is never fired, at least my listener never gets it.

    My spring-security is as follows:
    Code:
    <session-management 
        session-fixation-protection="migrateSession" >
    
       <concurrency-control 
           max-sessions="1"
           expired-url="/login_sessionexpired" />
    </session-management>
    The above logs the user out of the first session, if they concurrently log in a second time, however, the HttpSessionListener.sessionDestroyed is never called.

    The HttpSessionListener.sessionDestroyed is called normally for manual logout and session time out.

    I have a 'delegating proxy' for the listener in web.xml:
    Code:
    <listener>
    		<listener-class>com.test.security.DelegatingHttpSessionEventListenerProxy</listener-class>
    	</listener>
    This listener delegates to a spring-bean defined in the my-servlet.xml as:
    Code:
    <bean id="httpSessionEventListener"
    		class="com.test.security.SimpleHttpSessionEventListenerImpl" />
    The delegating listener is coded as:
    Code:
    public class DelegatingHttpSessionEventListenerProxy implements
    		HttpSessionListener {
    
    	/**
    	 * Delegates sessionCreated Event to the Spring-bean
    	 */
    	@Override
    	public void sessionCreated(HttpSessionEvent se) {
    		ApplicationContext context = WebApplicationContextUtils
    				.getWebApplicationContext(se.getSession().getServletContext());
    
    		HttpSessionListener target = context.getBean(
    				"httpSessionEventListener", HttpSessionListener.class);
    		target.sessionCreated(se);
    	}
    
    	/**
    	 * Delegates sessionDestroyed Event to the Spring-bean
    	 */
    	@Override
    	public void sessionDestroyed(HttpSessionEvent se) {
    		ApplicationContext context = WebApplicationContextUtils
    				.getWebApplicationContext(se.getSession().getServletContext());
    
    		HttpSessionListener target = context.getBean(
    				"httpSessionEventListener", HttpSessionListener.class);
    		target.sessionDestroyed(se);
    	}
    }
    I'm using spring-security-3.0.5, can somebody please tell me what am I missing?

    Thank you.

  2. #2
    Luke Taylor is offline Senior Member Acegi Security System TeamSpring Team
    Join Date
    Aug 2004
    Location
    Glasgow, Scotland
    Posts
    3,449

    Default

    The session will only be invalidated if the user tries to use it again after starting a new session, otherwise it will time out as normal. This is because it isn't possible to invalidate a session other than during a request made as part of that session. At least not without resorting to container-specific APIs.
    Spring - by Pivotal
    twitter @tekul

  3. #3
    Join Date
    Aug 2010
    Posts
    26

    Default

    You are absolutely right, it is. I was expecting it to timeout the moment another concurrent log in was made.
    Thank you.

  4. #4
    Join Date
    May 2010
    Posts
    9

    Default

    Luke, is that part of the Servlet spec? I was unaware of this.

  5. #5
    Luke Taylor is offline Senior Member Acegi Security System TeamSpring Team
    Join Date
    Aug 2004
    Location
    Glasgow, Scotland
    Posts
    3,449

    Default

    Quote Originally Posted by ericacm View Post
    Luke, is that part of the Servlet spec? I was unaware of this.
    Sorry, not sure what you mean. Is what part of the servlet spec? You need a reference to the HttpSession to be able to invalidate it and that is only obtainable through the HttpServletRequest, which is what I meant by my comment.
    Last edited by Luke Taylor; Jun 15th, 2011 at 05:30 PM.
    Spring - by Pivotal
    twitter @tekul

  6. #6
    Join Date
    May 2010
    Posts
    9

    Default

    I see - but if you have a reference to a HttpSession from somewhere else (for example, a container specific API or a cache you are maintaining in a HttpSessionListener) then you can still call invalidate() on it, correct?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •