|
| 1 | +package org.baeldung.security; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.mock.web.MockHttpSession; |
| 8 | +import org.springframework.security.core.userdetails.UserDetails; |
| 9 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 10 | +import org.springframework.test.context.ContextConfiguration; |
| 11 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 12 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.test.web.servlet.MvcResult; |
| 15 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 16 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 17 | +import org.springframework.web.context.WebApplicationContext; |
| 18 | + |
| 19 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 20 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
| 21 | +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
| 22 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 23 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 24 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 26 | + |
| 27 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 28 | +@ContextConfiguration({ "/RedirectionWebSecurityConfig.xml", "/mvc-servlet.xml" }) |
| 29 | +@WebAppConfiguration |
| 30 | +public class RedirectionSecurityIntegrationTest { |
| 31 | + |
| 32 | + @Autowired private WebApplicationContext context; |
| 33 | + |
| 34 | + @Autowired private UserDetailsService userDetailsService; |
| 35 | + |
| 36 | + private MockMvc mvc; |
| 37 | + private UserDetails userDetails; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setup() { |
| 41 | + mvc = MockMvcBuilders |
| 42 | + .webAppContextSetup(context) |
| 43 | + .apply(springSecurity()) |
| 44 | + .build(); |
| 45 | + userDetails = userDetailsService.loadUserByUsername("user1"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenSecuredResource_whenAccessUnauthenticated_thenRequiresAuthentication() throws Exception { |
| 50 | + mvc |
| 51 | + .perform(get("/secured")) |
| 52 | + .andExpect(status().is3xxRedirection()) |
| 53 | + .andExpect(redirectedUrlPattern("**/login")); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenCredentials_whenAccessSecuredResource_thenSuccess() throws Exception { |
| 59 | + mvc |
| 60 | + .perform(get("/secured").with(user(userDetails))) |
| 61 | + .andExpect(status().isOk()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenAccessSecuredResource_whenAuthenticated_thenRedirectedBack() throws Exception { |
| 66 | + MockHttpServletRequestBuilder securedResourceAccess = get("/secured"); |
| 67 | + MvcResult unauthenticatedResult = mvc |
| 68 | + .perform(securedResourceAccess) |
| 69 | + .andExpect(status().is3xxRedirection()) |
| 70 | + .andReturn(); |
| 71 | + |
| 72 | + MockHttpSession session = (MockHttpSession) unauthenticatedResult |
| 73 | + .getRequest() |
| 74 | + .getSession(); |
| 75 | + String loginUrl = unauthenticatedResult |
| 76 | + .getResponse() |
| 77 | + .getRedirectedUrl(); |
| 78 | + mvc |
| 79 | + .perform(post(loginUrl) |
| 80 | + .param("username", userDetails.getUsername()) |
| 81 | + .param("password", userDetails.getPassword()) |
| 82 | + .session(session) |
| 83 | + .with(csrf())) |
| 84 | + .andExpect(status().is3xxRedirection()) |
| 85 | + .andExpect(redirectedUrlPattern("**/secured")) |
| 86 | + .andReturn(); |
| 87 | + |
| 88 | + mvc |
| 89 | + .perform(securedResourceAccess.session(session)) |
| 90 | + .andExpect(status().isOk()); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments