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);
}
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");
}
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;
}
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();
}
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();
}
Aggregations