Search in sources :

Example 21 with SecurityContext

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

the class SecurityMockMvcRequestPostProcessorsUserTests method userCustomAuthoritiesVarargs.

@Test
public void userCustomAuthoritiesVarargs() {
    String username = "customuser";
    user(username).authorities(authority1, authority2).postProcessRequest(request);
    verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
    SecurityContext context = contextCaptor.getValue();
    assertThat(context.getAuthentication().getAuthorities()).containsOnly(authority1, authority2);
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 22 with SecurityContext

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

the class SecurityMockMvcRequestPostProcessorsUserTests method userWithCustom.

@Test
public void userWithCustom() {
    String username = "customuser";
    user(username).roles("CUSTOM", "ADMIN").password("newpass").postProcessRequest(request);
    verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
    SecurityContext context = contextCaptor.getValue();
    assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
    assertThat(context.getAuthentication().getName()).isEqualTo(username);
    assertThat(context.getAuthentication().getCredentials()).isEqualTo("newpass");
    assertThat(context.getAuthentication().getAuthorities()).extracting("authority").containsOnly("ROLE_CUSTOM", "ROLE_ADMIN");
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 23 with SecurityContext

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

the class TestSecurityContextHolder method getContext.

/**
	 * Gets the {@link SecurityContext} from {@link TestSecurityContextHolder}.
	 *
	 * @return the {@link SecurityContext} from {@link TestSecurityContextHolder}.
	 */
public static SecurityContext getContext() {
    SecurityContext ctx = contextHolder.get();
    if (ctx == null) {
        ctx = getDefaultContext();
        contextHolder.set(ctx);
    }
    return ctx;
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext)

Example 24 with SecurityContext

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

the class HttpSessionSecurityContextRepositoryTests method sessionIsntCreatedIfContextDoesntChange.

@Test
public void sessionIsntCreatedIfContextDoesntChange() throws Exception {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
    SecurityContext context = repo.loadContext(holder);
    assertThat(request.getSession(false)).isNull();
    repo.saveContext(context, holder.getRequest(), holder.getResponse());
    assertThat(request.getSession(false)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityContext(org.springframework.security.core.context.SecurityContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with SecurityContext

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

the class HttpSessionSecurityContextRepositoryTests method logoutInvalidateSessionFalseFails.

// SEC-3070
@Test
public void logoutInvalidateSessionFalseFails() throws Exception {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    SecurityContext ctxInSession = SecurityContextHolder.createEmptyContext();
    ctxInSession.setAuthentication(testToken);
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, ctxInSession);
    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, new MockHttpServletResponse());
    repo.loadContext(holder);
    ctxInSession.setAuthentication(null);
    repo.saveContext(ctxInSession, holder.getRequest(), holder.getResponse());
    assertThat(request.getSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityContext(org.springframework.security.core.context.SecurityContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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