|
24 | 24 | import org.junit.jupiter.api.Test;
|
25 | 25 | import org.junit.jupiter.api.extension.ExtendWith;
|
26 | 26 |
|
| 27 | +import org.springframework.beans.factory.BeanCreationException; |
27 | 28 | import org.springframework.beans.factory.annotation.Autowired;
|
28 | 29 | import org.springframework.context.annotation.Bean;
|
29 | 30 | import org.springframework.context.annotation.Configuration;
|
|
47 | 48 | import org.springframework.web.bind.annotation.RestController;
|
48 | 49 |
|
49 | 50 | import static org.assertj.core.api.Assertions.assertThat;
|
| 51 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
50 | 52 | import static org.springframework.security.config.Customizer.withDefaults;
|
51 | 53 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
|
52 | 54 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
@@ -200,6 +202,24 @@ public void loginWhenUsingDefaultsThenDefaultLogoutSuccessPageGenerated() throws
|
200 | 202 | this.mockMvc.perform(get("/login?logout")).andExpect(status().isOk());
|
201 | 203 | }
|
202 | 204 |
|
| 205 | + @Test |
| 206 | + public void configureWhenAuthorizeHttpRequestsBeforeAuthorizeRequestThenException() { |
| 207 | + assertThatExceptionOfType(BeanCreationException.class) |
| 208 | + .isThrownBy( |
| 209 | + () -> this.spring.register(AuthorizeHttpRequestsBeforeAuthorizeRequestsConfig.class).autowire()) |
| 210 | + .withMessageContaining( |
| 211 | + "authorizeHttpRequests cannot be used in conjunction with authorizeRequests. Please select just one."); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void configureWhenAuthorizeHttpRequestsAfterAuthorizeRequestThenException() { |
| 216 | + assertThatExceptionOfType(BeanCreationException.class) |
| 217 | + .isThrownBy( |
| 218 | + () -> this.spring.register(AuthorizeHttpRequestsAfterAuthorizeRequestsConfig.class).autowire()) |
| 219 | + .withMessageContaining( |
| 220 | + "authorizeHttpRequests cannot be used in conjunction with authorizeRequests. Please select just one."); |
| 221 | + } |
| 222 | + |
203 | 223 | @RestController
|
204 | 224 | static class NameController {
|
205 | 225 |
|
@@ -270,6 +290,44 @@ UserDetailsService userDetailsService() {
|
270 | 290 |
|
271 | 291 | }
|
272 | 292 |
|
| 293 | + @EnableWebSecurity |
| 294 | + static class AuthorizeHttpRequestsBeforeAuthorizeRequestsConfig { |
| 295 | + |
| 296 | + @Bean |
| 297 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 298 | + // @formatter:off |
| 299 | + return http |
| 300 | + .authorizeHttpRequests((requests) -> requests |
| 301 | + .anyRequest().authenticated() |
| 302 | + ) |
| 303 | + .authorizeRequests((requests) -> requests |
| 304 | + .anyRequest().authenticated() |
| 305 | + ) |
| 306 | + .build(); |
| 307 | + // @formatter:on |
| 308 | + } |
| 309 | + |
| 310 | + } |
| 311 | + |
| 312 | + @EnableWebSecurity |
| 313 | + static class AuthorizeHttpRequestsAfterAuthorizeRequestsConfig { |
| 314 | + |
| 315 | + @Bean |
| 316 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 317 | + // @formatter:off |
| 318 | + return http |
| 319 | + .authorizeRequests((requests) -> requests |
| 320 | + .anyRequest().authenticated() |
| 321 | + ) |
| 322 | + .authorizeHttpRequests((requests) -> requests |
| 323 | + .anyRequest().authenticated() |
| 324 | + ) |
| 325 | + .build(); |
| 326 | + // @formatter:on |
| 327 | + } |
| 328 | + |
| 329 | + } |
| 330 | + |
273 | 331 | @RestController
|
274 | 332 | static class BaseController {
|
275 | 333 |
|
|
0 commit comments