|
29 | 29 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
30 | 30 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
31 | 31 | import org.springframework.security.config.test.SpringTestRule;
|
| 32 | +import org.springframework.security.core.Authentication; |
32 | 33 | import org.springframework.security.core.userdetails.PasswordEncodedUser;
|
33 | 34 | import org.springframework.security.web.access.AccessDeniedHandler;
|
| 35 | +import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; |
34 | 36 | import org.springframework.security.web.csrf.CsrfTokenRepository;
|
35 | 37 | import org.springframework.security.web.csrf.DefaultCsrfToken;
|
36 | 38 | import org.springframework.security.web.firewall.StrictHttpFirewall;
|
|
60 | 62 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
61 | 63 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
62 | 64 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
|
63 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
64 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
65 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.head; |
66 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; |
67 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
68 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
69 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
70 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; |
| 65 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
71 | 66 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
72 | 67 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
73 | 68 |
|
|
76 | 71 | *
|
77 | 72 | * @author Rob Winch
|
78 | 73 | * @author Eleftheria Stein
|
| 74 | + * @author Michael Vitz |
| 75 | + * @author Sam Simmons |
79 | 76 | */
|
80 | 77 | public class CsrfConfigurerTests {
|
81 | 78 | @Rule
|
@@ -684,6 +681,66 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
684 | 681 | }
|
685 | 682 | }
|
686 | 683 |
|
| 684 | + @EnableWebSecurity |
| 685 | + static class NullAuthenticationStrategy extends WebSecurityConfigurerAdapter { |
| 686 | + @Override |
| 687 | + protected void configure(HttpSecurity http) throws Exception { |
| 688 | + // @formatter:off |
| 689 | + http |
| 690 | + .csrf() |
| 691 | + .sessionAuthenticationStrategy(null); |
| 692 | + // @formatter:on |
| 693 | + } |
| 694 | + } |
| 695 | + |
| 696 | + @Test |
| 697 | + public void getWhenNullAuthenticationStrategyThenException() { |
| 698 | + assertThatThrownBy(() -> this.spring.register(NullAuthenticationStrategy.class).autowire()) |
| 699 | + .isInstanceOf(BeanCreationException.class) |
| 700 | + .hasRootCauseInstanceOf(IllegalArgumentException.class); |
| 701 | + } |
| 702 | + |
| 703 | + @EnableWebSecurity |
| 704 | + static class CsrfAuthenticationStrategyConfig extends WebSecurityConfigurerAdapter { |
| 705 | + static SessionAuthenticationStrategy STRATEGY; |
| 706 | + |
| 707 | + @Override |
| 708 | + protected void configure(HttpSecurity http) throws Exception { |
| 709 | + // @formatter:off |
| 710 | + http |
| 711 | + .formLogin() |
| 712 | + .and() |
| 713 | + .csrf() |
| 714 | + .sessionAuthenticationStrategy(STRATEGY); |
| 715 | + // @formatter:on |
| 716 | + } |
| 717 | + |
| 718 | + @Override |
| 719 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 720 | + // @formatter:off |
| 721 | + auth |
| 722 | + .inMemoryAuthentication() |
| 723 | + .withUser(PasswordEncodedUser.user()); |
| 724 | + // @formatter:on |
| 725 | + } |
| 726 | + } |
| 727 | + |
| 728 | + @Test |
| 729 | + public void csrfAuthenticationStrategyConfiguredThenStrategyUsed() throws Exception { |
| 730 | + CsrfAuthenticationStrategyConfig.STRATEGY = mock(SessionAuthenticationStrategy.class); |
| 731 | + |
| 732 | + this.spring.register(CsrfAuthenticationStrategyConfig.class).autowire(); |
| 733 | + |
| 734 | + this.mvc.perform(post("/login") |
| 735 | + .with(csrf()) |
| 736 | + .param("username", "user") |
| 737 | + .param("password", "password")) |
| 738 | + .andExpect(redirectedUrl("/")); |
| 739 | + |
| 740 | + verify(CsrfAuthenticationStrategyConfig.STRATEGY, atLeastOnce()) |
| 741 | + .onAuthentication(any(Authentication.class), any(HttpServletRequest.class), any(HttpServletResponse.class)); |
| 742 | + } |
| 743 | + |
687 | 744 | @RestController
|
688 | 745 | static class BasicController {
|
689 | 746 | @GetMapping("/")
|
|
0 commit comments