Example usage for org.springframework.security.core.session SessionDestroyedEvent getSecurityContexts

List of usage examples for org.springframework.security.core.session SessionDestroyedEvent getSecurityContexts

Introduction

In this page you can find the example usage for org.springframework.security.core.session SessionDestroyedEvent getSecurityContexts.

Prototype

public abstract List<SecurityContext> getSecurityContexts();

Source Link

Document

Provides the SecurityContext instances which were associated with the destroyed session.

Usage

From source file:com.sibvisions.rad.server.security.spring.logout.DestroySessionApplicationListener.java

/**
 * {@inheritDoc}//from  ww w . ja  v a  2  s  .c  o  m
 */
@Override
public void onApplicationEvent(SessionDestroyedEvent pEvent) {
    List<SecurityContext> securityContexts = pEvent.getSecurityContexts();

    if (securityContexts != null) {
        SecurityContext securityContext = null;

        for (int i = 0, ic = securityContexts.size(); i < ic; i++) {
            securityContext = securityContexts.get(i);

            if (securityContext != null) {
                doLogout(securityContext.getAuthentication());
            }
        }
    }
}

From source file:org.springframework.security.authentication.jaas.AbstractJaasAuthenticationProvider.java

/**
 * Handles the logout by getting the security contexts for the destroyed session and
 * invoking {@code LoginContext.logout()} for any which contain a
 * {@code JaasAuthenticationToken}./*from w w  w  . j a v a2s.c om*/
 *
 *
 * @param event the session event which contains the current session
 */
protected void handleLogout(SessionDestroyedEvent event) {
    List<SecurityContext> contexts = event.getSecurityContexts();

    if (contexts.isEmpty()) {
        this.log.debug("The destroyed session has no SecurityContexts");

        return;
    }

    for (SecurityContext context : contexts) {
        Authentication auth = context.getAuthentication();

        if ((auth != null) && (auth instanceof JaasAuthenticationToken)) {
            JaasAuthenticationToken token = (JaasAuthenticationToken) auth;

            try {
                LoginContext loginContext = token.getLoginContext();
                boolean debug = this.log.isDebugEnabled();
                if (loginContext != null) {
                    if (debug) {
                        this.log.debug("Logging principal: [" + token.getPrincipal() + "] out of LoginContext");
                    }
                    loginContext.logout();
                } else if (debug) {
                    this.log.debug("Cannot logout principal: [" + token.getPrincipal() + "] from LoginContext. "
                            + "The LoginContext is unavailable");
                }
            } catch (LoginException e) {
                this.log.warn("Error error logging out of LoginContext", e);
            }
        }
    }
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void logout() throws Exception {
    SessionDestroyedEvent event = mock(SessionDestroyedEvent.class);
    SecurityContext securityContext = mock(SecurityContext.class);
    JaasAuthenticationToken token = mock(JaasAuthenticationToken.class);
    LoginContext context = mock(LoginContext.class);

    when(event.getSecurityContexts()).thenReturn(Arrays.asList(securityContext));
    when(securityContext.getAuthentication()).thenReturn(token);
    when(token.getLoginContext()).thenReturn(context);

    provider.onApplicationEvent(event);/*from w  w  w.j  a v  a 2s .c  o  m*/

    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verify(token).getLoginContext();
    verify(context).logout();
    verifyNoMoreInteractions(event, securityContext, token, context);
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void logoutNullAuthentication() {
    SessionDestroyedEvent event = mock(SessionDestroyedEvent.class);
    SecurityContext securityContext = mock(SecurityContext.class);

    when(event.getSecurityContexts()).thenReturn(Arrays.asList(securityContext));

    provider.handleLogout(event);//  w  w  w  .  j a va2 s  .co  m

    verify(event).getSecurityContexts();
    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verifyNoMoreInteractions(event, securityContext);
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void logoutNonJaasAuthentication() {
    SessionDestroyedEvent event = mock(SessionDestroyedEvent.class);
    SecurityContext securityContext = mock(SecurityContext.class);

    when(event.getSecurityContexts()).thenReturn(Arrays.asList(securityContext));
    when(securityContext.getAuthentication()).thenReturn(token);

    provider.handleLogout(event);//from   w  ww. j  a  v a2s  .c om

    verify(event).getSecurityContexts();
    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verifyNoMoreInteractions(event, securityContext);
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void logoutNullLoginContext() throws Exception {
    SessionDestroyedEvent event = mock(SessionDestroyedEvent.class);
    SecurityContext securityContext = mock(SecurityContext.class);
    JaasAuthenticationToken token = mock(JaasAuthenticationToken.class);

    when(event.getSecurityContexts()).thenReturn(Arrays.asList(securityContext));
    when(securityContext.getAuthentication()).thenReturn(token);

    provider.onApplicationEvent(event);/* w ww .  j  a  v a2  s . c om*/
    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verify(token).getLoginContext();

    verifyNoMoreInteractions(event, securityContext, token);
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void logoutLoginException() throws Exception {
    SessionDestroyedEvent event = mock(SessionDestroyedEvent.class);
    SecurityContext securityContext = mock(SecurityContext.class);
    JaasAuthenticationToken token = mock(JaasAuthenticationToken.class);
    LoginContext context = mock(LoginContext.class);
    LoginException loginException = new LoginException("Failed Login");

    when(event.getSecurityContexts()).thenReturn(Arrays.asList(securityContext));
    when(securityContext.getAuthentication()).thenReturn(token);
    when(token.getLoginContext()).thenReturn(context);
    doThrow(loginException).when(context).logout();

    provider.onApplicationEvent(event);// w w  w.ja v  a 2  s . c  o  m

    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verify(token).getLoginContext();
    verify(context).logout();
    verify(log).warn(anyString(), eq(loginException));
    verifyNoMoreInteractions(event, securityContext, token, context);
}