Search in sources :

Example 36 with SecurityContext

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

the class SecurityContextLogoutHandlerTests method clearsAuthentication.

// SEC-2025
@Test
public void clearsAuthentication() {
    SecurityContext beforeContext = SecurityContextHolder.getContext();
    handler.logout(request, response, SecurityContextHolder.getContext().getAuthentication());
    assertThat(beforeContext.getAuthentication()).isNull();
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 37 with SecurityContext

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

the class SecurityContextLogoutHandlerTests method disableClearsAuthentication.

@Test
public void disableClearsAuthentication() {
    handler.setClearAuthentication(false);
    SecurityContext beforeContext = SecurityContextHolder.getContext();
    Authentication beforeAuthentication = beforeContext.getAuthentication();
    handler.logout(request, response, SecurityContextHolder.getContext().getAuthentication());
    assertThat(beforeContext.getAuthentication()).isNotNull();
    assertThat(beforeContext.getAuthentication()).isSameAs(beforeAuthentication);
}
Also used : Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 38 with SecurityContext

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

the class HttpSessionDestroyedEventTests method getSecurityContextsMulti.

@Test
public void getSecurityContextsMulti() {
    session.setAttribute("another", new SecurityContextImpl());
    List<SecurityContext> securityContexts = destroyedEvent.getSecurityContexts();
    assertThat(securityContexts).hasSize(2);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 39 with SecurityContext

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

the class SecurityContextHolderAwareRequestFilterTests method startAsyncWithRequestResponseStart.

@Test
public void startAsyncWithRequestResponseStart() throws Exception {
    ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password", "ROLE_USER");
    context.setAuthentication(expectedAuth);
    SecurityContextHolder.setContext(context);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(this.request.startAsync(this.request, this.response)).thenReturn(asyncContext);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
        }
    };
    wrappedRequest().startAsync(this.request, this.response).start(runnable);
    verifyZeroInteractions(this.authenticationManager, this.logoutHandler);
    verify(asyncContext).start(runnableCaptor.capture());
    DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
    assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, "delegateSecurityContext")).isEqualTo(context);
    assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, "delegate")).isEqualTo(runnable);
}
Also used : DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) SecurityContext(org.springframework.security.core.context.SecurityContext) AsyncContext(javax.servlet.AsyncContext) DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with SecurityContext

use of org.springframework.security.core.context.SecurityContext in project head by mifos.

the class RepayLoanActionStrutsTest method setMifosUserFromContext.

private void setMifosUserFromContext() {
    SecurityContext securityContext = new SecurityContextImpl();
    MifosUser principal = new MifosUser(userContext.getId(), userContext.getBranchId(), userContext.getLevelId(), new ArrayList<Short>(userContext.getRoles()), userContext.getName(), "".getBytes(), true, true, true, true, new ArrayList<GrantedAuthority>(), userContext.getLocaleId());
    Authentication authentication = new TestingAuthenticationToken(principal, principal);
    securityContext.setAuthentication(authentication);
    SecurityContextHolder.setContext(securityContext);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) MifosUser(org.mifos.security.MifosUser) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken)

Aggregations

SecurityContext (org.springframework.security.core.context.SecurityContext)147 Test (org.junit.Test)65 Authentication (org.springframework.security.core.Authentication)63 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 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)21 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 GrantedAuthority (org.springframework.security.core.GrantedAuthority)13 ArrayList (java.util.ArrayList)10 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 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6