Search in sources :

Example 96 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project spring-security by spring-projects.

the class GrantedAuthorityDefaultsJcTests method doFilterIsUserInRole.

// SEC-2926
@Test
public void doFilterIsUserInRole() throws Exception {
    SecurityContext context = SecurityContextHolder.getContext();
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            assertThat(httpRequest.isUserInRole("USER")).isTrue();
            assertThat(httpRequest.isUserInRole("INVALID")).isFalse();
            super.doFilter(request, response);
        }
    };
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(chain.getRequest()).isNotNull();
}
Also used : ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SecurityContext(org.springframework.security.core.context.SecurityContext) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 97 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project spring-security by spring-projects.

the class AbstractJaasAuthenticationProvider method handleLogout.

/**
	 * Handles the logout by getting the security contexts for the destroyed session and
	 * invoking {@code LoginContext.logout()} for any which contain a
	 * {@code JaasAuthenticationToken}.
	 *
	 *
	 * @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);
            }
        }
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) LoginException(javax.security.auth.login.LoginException)

Example 98 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project spring-security by spring-projects.

the class DelegatingSecurityContextCallable method call.

public V call() throws Exception {
    this.originalSecurityContext = SecurityContextHolder.getContext();
    try {
        SecurityContextHolder.setContext(delegateSecurityContext);
        return delegate.call();
    } finally {
        SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
        if (emptyContext.equals(originalSecurityContext)) {
            SecurityContextHolder.clearContext();
        } else {
            SecurityContextHolder.setContext(originalSecurityContext);
        }
        this.originalSecurityContext = null;
    }
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext)

Example 99 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project spring-security by spring-projects.

the class DelegatingSecurityContextRunnable method run.

public void run() {
    this.originalSecurityContext = SecurityContextHolder.getContext();
    try {
        SecurityContextHolder.setContext(delegateSecurityContext);
        delegate.run();
    } finally {
        SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
        if (emptyContext.equals(originalSecurityContext)) {
            SecurityContextHolder.clearContext();
        } else {
            SecurityContextHolder.setContext(originalSecurityContext);
        }
        this.originalSecurityContext = null;
    }
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext)

Example 100 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project spring-security by spring-projects.

the class DefaultJaasAuthenticationProviderTests method logoutLoginException.

@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);
    verify(event).getSecurityContexts();
    verify(securityContext).getAuthentication();
    verify(token).getLoginContext();
    verify(context).logout();
    verify(log).warn(anyString(), eq(loginException));
    verifyNoMoreInteractions(event, securityContext, token, context);
}
Also used : LoginContext(javax.security.auth.login.LoginContext) SecurityContext(org.springframework.security.core.context.SecurityContext) LoginException(javax.security.auth.login.LoginException) SessionDestroyedEvent(org.springframework.security.core.session.SessionDestroyedEvent) Test(org.junit.Test)

Aggregations

SecurityContext (org.springframework.security.core.context.SecurityContext)138 Test (org.junit.Test)60 Authentication (org.springframework.security.core.Authentication)60 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)46 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)46 MifosUser (org.mifos.security.MifosUser)38 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)29 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 HttpServletResponse (javax.servlet.http.HttpServletResponse)15 Before (org.junit.Before)15 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)15 GrantedAuthority (org.springframework.security.core.GrantedAuthority)11 ArrayList (java.util.ArrayList)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)7 SessionDestroyedEvent (org.springframework.security.core.session.SessionDestroyedEvent)7 HttpSession (javax.servlet.http.HttpSession)6 MeetingBO (org.mifos.application.meeting.business.MeetingBO)5