title: Logout/Session timeout catching with spring security
body: <p>I'm using spring/spring-security 3.1 and want to take some action whenever the user logs out (or if the session is timed out). I managed to get the action done for logout but for session timeout, I can't get it working.</p>
  
  <p>In web.xml I only have the ContextLoaderListener specified ( can this be the issue? ) and of course the DelegatingFilterProxy.</p>
  
  <p>I use the auto config like this.</p>
  
  <pre><code>    &lt;security:http auto-config="false" use-expressions="false"&gt;
      &lt;security:intercept-url pattern="/dialog/*"
          access="ROLE_USERS" /&gt;
      &lt;security:intercept-url pattern="/boa/*"
          access="ROLE-USERS" /&gt;
      &lt;security:intercept-url pattern="/*.html"
          access="ROLE-USERS" /&gt;
  
      &lt;security:form-login login-page="/auth/login.html"
          default-target-url="/index.html" /&gt;
      &lt;security:logout logout-url="/logout"
           invalidate-session="true"
          delete-cookies="JSESSIONID" success-handler-ref="logoutHandler" /&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="logoutHandler" class="com.bla.bla.bla.LogoutHandler"&gt;
      &lt;property name="logoutUrl" value="/auth/logout.html"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The logout handler is called when user clicks logout, which will make some calls to a database. </p>
  
  <p>But how do I handle the session timeout ??? </p>
  
  <p>One way to handle it would be to inject the username into the session when user logs in and then use an ordinary httpsessionlistener and do the same thing on session timeout. </p>
  
  <p>Is there a similar way with spring security, so that when spring discovers that the session is to timeout, I can hook in there, access the Authentication and get the UserDetails from there and do the clean up.</p>