|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
30 | 30 | import org.springframework.context.annotation.Configuration;
|
31 | 31 | import org.springframework.http.HttpMethod;
|
32 | 32 | import org.springframework.mock.web.MockHttpSession;
|
| 33 | +import org.springframework.security.config.Customizer; |
33 | 34 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
34 | 35 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
35 | 36 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
38 | 39 | import org.springframework.security.config.test.SpringTestContextExtension;
|
39 | 40 | import org.springframework.security.core.Authentication;
|
40 | 41 | import org.springframework.security.core.userdetails.PasswordEncodedUser;
|
| 42 | +import org.springframework.security.web.SecurityFilterChain; |
41 | 43 | import org.springframework.security.web.access.AccessDeniedHandler;
|
42 | 44 | import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
| 45 | +import org.springframework.security.web.csrf.CsrfToken; |
43 | 46 | import org.springframework.security.web.csrf.CsrfTokenRepository;
|
| 47 | +import org.springframework.security.web.csrf.CsrfTokenRequestProcessor; |
44 | 48 | import org.springframework.security.web.csrf.DefaultCsrfToken;
|
45 | 49 | import org.springframework.security.web.firewall.StrictHttpFirewall;
|
46 | 50 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
55 | 59 |
|
56 | 60 | import static org.assertj.core.api.Assertions.assertThat;
|
57 | 61 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 62 | +import static org.hamcrest.Matchers.containsString; |
58 | 63 | import static org.mockito.ArgumentMatchers.any;
|
| 64 | +import static org.mockito.ArgumentMatchers.eq; |
59 | 65 | import static org.mockito.ArgumentMatchers.isNull;
|
60 | 66 | import static org.mockito.BDDMockito.given;
|
61 | 67 | import static org.mockito.Mockito.atLeastOnce;
|
62 | 68 | import static org.mockito.Mockito.mock;
|
| 69 | +import static org.mockito.Mockito.times; |
63 | 70 | import static org.mockito.Mockito.verify;
|
| 71 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
64 | 72 | import static org.springframework.security.config.Customizer.withDefaults;
|
65 | 73 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
66 | 74 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
|
74 | 82 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
75 | 83 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
76 | 84 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;
|
| 85 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
77 | 86 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
78 | 87 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
79 | 88 |
|
|
84 | 93 | * @author Eleftheria Stein
|
85 | 94 | * @author Michael Vitz
|
86 | 95 | * @author Sam Simmons
|
| 96 | + * @author Steve Riesenberg |
87 | 97 | */
|
88 | 98 | @ExtendWith(SpringTestContextExtension.class)
|
89 | 99 | public class CsrfConfigurerTests {
|
@@ -407,6 +417,47 @@ public void csrfAuthenticationStrategyConfiguredThenStrategyUsed() throws Except
|
407 | 417 | any(HttpServletRequest.class), any(HttpServletResponse.class));
|
408 | 418 | }
|
409 | 419 |
|
| 420 | + @Test |
| 421 | + public void getLoginWhenCsrfTokenRequestProcessorSetThenRespondsWithNormalCsrfToken() throws Exception { |
| 422 | + CsrfTokenRepository csrfTokenRepository = mock(CsrfTokenRepository.class); |
| 423 | + CsrfToken csrfToken = new DefaultCsrfToken("X-CSRF-TOKEN", "_csrf", "token"); |
| 424 | + given(csrfTokenRepository.generateToken(any(HttpServletRequest.class))).willReturn(csrfToken); |
| 425 | + CsrfTokenRequestProcessorConfig.REPO = csrfTokenRepository; |
| 426 | + CsrfTokenRequestProcessorConfig.PROCESSOR = new CsrfTokenRequestProcessor(); |
| 427 | + this.spring.register(CsrfTokenRequestProcessorConfig.class, BasicController.class).autowire(); |
| 428 | + this.mvc.perform(get("/login")).andExpect(status().isOk()) |
| 429 | + .andExpect(content().string(containsString(csrfToken.getToken()))); |
| 430 | + verify(csrfTokenRepository).loadToken(any(HttpServletRequest.class)); |
| 431 | + verify(csrfTokenRepository).generateToken(any(HttpServletRequest.class)); |
| 432 | + verify(csrfTokenRepository).saveToken(eq(csrfToken), any(HttpServletRequest.class), |
| 433 | + any(HttpServletResponse.class)); |
| 434 | + verifyNoMoreInteractions(csrfTokenRepository); |
| 435 | + } |
| 436 | + |
| 437 | + @Test |
| 438 | + public void loginWhenCsrfTokenRequestProcessorSetAndNormalCsrfTokenThenSuccess() throws Exception { |
| 439 | + CsrfToken csrfToken = new DefaultCsrfToken("X-CSRF-TOKEN", "_csrf", "token"); |
| 440 | + CsrfTokenRepository csrfTokenRepository = mock(CsrfTokenRepository.class); |
| 441 | + given(csrfTokenRepository.loadToken(any(HttpServletRequest.class))).willReturn(csrfToken); |
| 442 | + given(csrfTokenRepository.generateToken(any(HttpServletRequest.class))).willReturn(csrfToken); |
| 443 | + CsrfTokenRequestProcessorConfig.REPO = csrfTokenRepository; |
| 444 | + CsrfTokenRequestProcessorConfig.PROCESSOR = new CsrfTokenRequestProcessor(); |
| 445 | + this.spring.register(CsrfTokenRequestProcessorConfig.class, BasicController.class).autowire(); |
| 446 | + // @formatter:off |
| 447 | + MockHttpServletRequestBuilder loginRequest = post("/login") |
| 448 | + .header(csrfToken.getHeaderName(), csrfToken.getToken()) |
| 449 | + .param("username", "user") |
| 450 | + .param("password", "password"); |
| 451 | + // @formatter:on |
| 452 | + this.mvc.perform(loginRequest).andExpect(redirectedUrl("/")); |
| 453 | + verify(csrfTokenRepository, times(2)).loadToken(any(HttpServletRequest.class)); |
| 454 | + verify(csrfTokenRepository).saveToken(isNull(), any(HttpServletRequest.class), any(HttpServletResponse.class)); |
| 455 | + verify(csrfTokenRepository).generateToken(any(HttpServletRequest.class)); |
| 456 | + verify(csrfTokenRepository).saveToken(eq(csrfToken), any(HttpServletRequest.class), |
| 457 | + any(HttpServletResponse.class)); |
| 458 | + verifyNoMoreInteractions(csrfTokenRepository); |
| 459 | + } |
| 460 | + |
410 | 461 | @Configuration
|
411 | 462 | static class AllowHttpMethodsFirewallConfig {
|
412 | 463 |
|
@@ -748,6 +799,43 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
748 | 799 |
|
749 | 800 | }
|
750 | 801 |
|
| 802 | + @Configuration |
| 803 | + @EnableWebSecurity |
| 804 | + static class CsrfTokenRequestProcessorConfig { |
| 805 | + |
| 806 | + static CsrfTokenRepository REPO; |
| 807 | + |
| 808 | + static CsrfTokenRequestProcessor PROCESSOR; |
| 809 | + |
| 810 | + @Bean |
| 811 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 812 | + // @formatter:off |
| 813 | + http |
| 814 | + .authorizeHttpRequests((authorize) -> authorize |
| 815 | + .anyRequest().authenticated() |
| 816 | + ) |
| 817 | + .formLogin(Customizer.withDefaults()) |
| 818 | + .csrf((csrf) -> csrf |
| 819 | + .csrfTokenRepository(REPO) |
| 820 | + .csrfTokenRequestAttributeHandler(PROCESSOR) |
| 821 | + .csrfTokenRequestResolver(PROCESSOR) |
| 822 | + ); |
| 823 | + // @formatter:on |
| 824 | + |
| 825 | + return http.build(); |
| 826 | + } |
| 827 | + |
| 828 | + @Autowired |
| 829 | + void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 830 | + // @formatter:off |
| 831 | + auth |
| 832 | + .inMemoryAuthentication() |
| 833 | + .withUser(PasswordEncodedUser.user()); |
| 834 | + // @formatter:on |
| 835 | + } |
| 836 | + |
| 837 | + } |
| 838 | + |
751 | 839 | @RestController
|
752 | 840 | static class BasicController {
|
753 | 841 |
|
|
0 commit comments