|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configuration;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.concurrent.Callable;
|
20 | 21 |
|
21 | 22 | import javax.servlet.http.HttpServletRequest;
|
22 | 23 |
|
23 | 24 | import com.google.common.net.HttpHeaders;
|
24 | 25 | import org.junit.jupiter.api.Test;
|
25 | 26 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.MockedStatic; |
| 29 | +import org.mockito.junit.jupiter.MockitoExtension; |
26 | 30 |
|
27 | 31 | import org.springframework.beans.factory.BeanCreationException;
|
28 | 32 | import org.springframework.beans.factory.annotation.Autowired;
|
29 | 33 | import org.springframework.context.annotation.Bean;
|
30 | 34 | import org.springframework.context.annotation.Configuration;
|
| 35 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
31 | 36 | import org.springframework.mock.web.MockHttpSession;
|
32 | 37 | import org.springframework.security.access.AccessDeniedException;
|
33 | 38 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
34 | 39 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
| 40 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
35 | 41 | import org.springframework.security.config.test.SpringTestContext;
|
36 | 42 | import org.springframework.security.config.test.SpringTestContextExtension;
|
37 | 43 | import org.springframework.security.core.context.SecurityContextHolder;
|
|
67 | 73 | *
|
68 | 74 | * @author Eleftheria Stein
|
69 | 75 | */
|
70 |
| -@ExtendWith(SpringTestContextExtension.class) |
| 76 | +@ExtendWith({ MockitoExtension.class, SpringTestContextExtension.class }) |
71 | 77 | public class HttpSecurityConfigurationTests {
|
72 | 78 |
|
73 | 79 | public final SpringTestContext spring = new SpringTestContext(this);
|
74 | 80 |
|
75 | 81 | @Autowired
|
76 | 82 | private MockMvc mockMvc;
|
77 | 83 |
|
| 84 | + @Mock |
| 85 | + private MockedStatic<SpringFactoriesLoader> springFactoriesLoader; |
| 86 | + |
78 | 87 | @Test
|
79 | 88 | public void postWhenDefaultFilterChainBeanThenRespondsWithForbidden() throws Exception {
|
80 | 89 | this.spring.register(DefaultWithFilterChainConfig.class).autowire();
|
@@ -220,6 +229,17 @@ public void configureWhenAuthorizeHttpRequestsAfterAuthorizeRequestThenException
|
220 | 229 | "authorizeHttpRequests cannot be used in conjunction with authorizeRequests. Please select just one.");
|
221 | 230 | }
|
222 | 231 |
|
| 232 | + @Test |
| 233 | + public void configureWhenDefaultConfigurerAsSpringFactoryThenDefaultConfigurerApplied() { |
| 234 | + DefaultConfigurer configurer = new DefaultConfigurer(); |
| 235 | + this.springFactoriesLoader.when( |
| 236 | + () -> SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, getClass().getClassLoader())) |
| 237 | + .thenReturn(Arrays.asList(configurer)); |
| 238 | + this.spring.register(DefaultWithFilterChainConfig.class).autowire(); |
| 239 | + assertThat(configurer.init).isTrue(); |
| 240 | + assertThat(configurer.configure).isTrue(); |
| 241 | + } |
| 242 | + |
223 | 243 | @RestController
|
224 | 244 | static class NameController {
|
225 | 245 |
|
@@ -349,4 +369,22 @@ void user(HttpServletRequest request) {
|
349 | 369 |
|
350 | 370 | }
|
351 | 371 |
|
| 372 | + static class DefaultConfigurer extends AbstractHttpConfigurer<DefaultConfigurer, HttpSecurity> { |
| 373 | + |
| 374 | + boolean init; |
| 375 | + |
| 376 | + boolean configure; |
| 377 | + |
| 378 | + @Override |
| 379 | + public void init(HttpSecurity builder) { |
| 380 | + this.init = true; |
| 381 | + } |
| 382 | + |
| 383 | + @Override |
| 384 | + public void configure(HttpSecurity builder) { |
| 385 | + this.configure = true; |
| 386 | + } |
| 387 | + |
| 388 | + } |
| 389 | + |
352 | 390 | }
|
0 commit comments