Example usage for org.springframework.security.authentication.jaas JaasAuthenticationToken getLoginContext

List of usage examples for org.springframework.security.authentication.jaas JaasAuthenticationToken getLoginContext

Introduction

In this page you can find the example usage for org.springframework.security.authentication.jaas JaasAuthenticationToken getLoginContext.

Prototype

public LoginContext getLoginContext() 

Source Link

Usage

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  av  a2 s  . co m
 *
 *
 * @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  av  a2 s . c om

    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 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);/*from  w ww  .j  a v  a 2 s  . co m*/

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