I have a Web app using Spring 3.1.1 and Spring Security 3.1.0. I implemented an ApplicationListener that checks SessionDestroyedEvent(s) and should log the username and other data. However, the getSecurityContexts() always returns an empty Collection. I am authenticating against an LDAP server. I also checked the getSource() method and it returns session data which holds the Principal information. However, the objects are container specific implementations which differ and there is no interface/abstract class that I can use. My question is whether this is a bug in SpringSecurity, or can I do some additional configuration?
Here is some relevant code:
@Service
public class ApplicationSecurityListener implements ApplicationListener<ApplicationEvent>{
@Override
public void onApplicationEvent(ApplicationEvent event)
{
else if ( event instanceof SessionDestroyedEvent )
{
SessionDestroyedEvent sessinEvent = ( SessionDestroyedEvent ) event;
//System.out.println ( "SessionDestroyedEvent:" + sessinEvent.getId() );
//load session if it is not empty
if(sessinEvent.getSecurityContexts() != null && !sessinEvent.getSecurityContexts().isEmpty())
{
...
}}}}
This is a bug in 3.1.0 that will be released as part of 3.1.1 (see SEC-1870). Until 3.1.1 is released you can get around the issue by obtaining the SecurityContext’s in onApplicationEvent manually. Using the changeset from the previously mentioned JIRA as a guide you would come up with something like this:
If you know there is only a single SecurityContext and you haven’t changed the attribute name the SecurityContext is stored in (typical) you can also obtain it using the following: