use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-security by spring-projects.
the class WebMvcSecurityConfigurationTests method csrfToken.
@Test
public void csrfToken() throws Exception {
CsrfToken csrfToken = new DefaultCsrfToken("headerName", "paramName", "token");
MockHttpServletRequestBuilder request = get("/csrf").requestAttr(CsrfToken.class.getName(), csrfToken);
mockMvc.perform(request).andExpect(assertResult(csrfToken));
}
use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-security by spring-projects.
the class ConcurrentSessionManagementTests method maxConcurrentLoginsValueIsRespected.
@Test
public void maxConcurrentLoginsValueIsRespected() throws Exception {
final MockHttpSession session1 = new MockHttpSession();
final MockHttpSession session2 = new MockHttpSession();
MockMvc mockMvc = createMockMvc("classpath:/spring/http-security-concurrency.xml", "classpath:/spring/in-memory-provider.xml", "classpath:/spring/testapp-servlet.xml");
mockMvc.perform(get("secure/index").session(session1)).andExpect(status().is3xxRedirection());
MockHttpServletRequestBuilder login1 = login().session(session1);
mockMvc.perform(login1).andExpect(authenticated().withUsername("jimi"));
MockHttpServletRequestBuilder login2 = login().session(session2);
mockMvc.perform(login2).andExpect(redirectedUrl("/login.jsp?login_error=true"));
Exception exception = (Exception) session2.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
assertThat(exception).isNotNull();
assertThat(exception.getMessage()).contains("Maximum sessions of 1 for this principal exceeded");
// Now logout to kill first session
mockMvc.perform(post("/logout").with(csrf())).andExpect(status().is3xxRedirection()).andDo(new ResultHandler() {
@SuppressWarnings("serial")
@Override
public void handle(MvcResult result) throws Exception {
context.publishEvent(new SessionDestroyedEvent(session1) {
@Override
public List<SecurityContext> getSecurityContexts() {
return Collections.emptyList();
}
@Override
public String getId() {
return session1.getId();
}
});
}
});
// Try second session again
login2 = login().session(session2);
mockMvc.perform(login2).andExpect(authenticated().withUsername("jimi"));
mockMvc.perform(get("/secure/index").session(session2)).andExpect(content().string(containsString("A Secure Page")));
}
use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-security by spring-projects.
the class SecurityMockMvcRequestPostProcessorsCsrfTests method csrfWhenUsedThenDoesNotImpactOriginalRepository.
// gh-4016
@Test
public void csrfWhenUsedThenDoesNotImpactOriginalRepository() throws Exception {
// @formatter:off
this.mockMvc.perform(post("/").with(csrf()));
MockHttpServletRequest request = new MockHttpServletRequest();
HttpSessionCsrfTokenRepository repo = new HttpSessionCsrfTokenRepository();
CsrfToken token = repo.generateToken(request);
repo.saveToken(token, request, new MockHttpServletResponse());
MockHttpServletRequestBuilder requestWithCsrf = post("/").param(token.getParameterName(), token.getToken()).session((MockHttpSession) request.getSession());
this.mockMvc.perform(requestWithCsrf).andExpect(status().isOk());
// @formatter:on
}
use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project hub-alert by blackducksoftware.
the class CommonDistributionConfigControllerTestIT method testDeleteConfig.
@Test
@Override
public void testDeleteConfig() throws Exception {
entityRepository.deleteAll();
final CommonDistributionConfigEntity savedEntity = entityRepository.save(entity);
final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.delete(restUrl).with(SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN"));
restModel.setId(String.valueOf(savedEntity.getId()));
request.content(gson.toJson(restModel));
request.contentType(contentType);
mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isAccepted());
}
use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project hub-alert by blackducksoftware.
the class HomeControllerTestIT method testVerifyNoToken.
@Test
@WithMockUser(roles = "ADMIN")
public void testVerifyNoToken() throws Exception {
final HttpHeaders headers = new HttpHeaders();
headers.add("X-CSRF-TOKEN", UUID.randomUUID().toString());
final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get(homeVerifyUrl).with(SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN"));
request.headers(headers);
mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isUnauthorized());
}
Aggregations