Skip to content

Commit beaf2b0

Browse files
committed
WebSecurityCustomizer beans are excluded by WebMvcTest
Add WebSecurityCustomizer to optional includes. Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 7defa59 commit beaf2b0

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
* <li>{@code Converter}</li>
5757
* <li>{@code DelegatingFilterProxyRegistrationBean}</li>
5858
* <li>{@code ErrorAttributes}</li>
59-
* <li>{@code Filter}</li>
6059
* <li>{@code FilterRegistrationBean}</li>
60+
* <li>{@code Filter}</li>
6161
* <li>{@code GenericConverter}</li>
6262
* <li>{@code HandlerInterceptor}</li>
6363
* <li>{@code HandlerMethodArgumentResolver}</li>
@@ -68,6 +68,7 @@
6868
* <li>{@code WebMvcConfigurer}</li>
6969
* <li>{@code WebMvcRegistrations}</li>
7070
* <li>{@code WebSecurityConfigurer}</li>
71+
* <li>{@code WebSecurityCustomizer}</li>
7172
* </ul>
7273
* <p>
7374
* By default, tests annotated with {@code @WebMvcTest} will also auto-configure Spring

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizabl
5252
private static final Class<?>[] NO_CONTROLLERS = {};
5353

5454
private static final String[] OPTIONAL_INCLUDES = { "com.fasterxml.jackson.databind.Module",
55+
"org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer",
5556
"org.springframework.security.config.annotation.web.WebSecurityConfigurer",
5657
"org.springframework.security.web.SecurityFilterChain", "org.thymeleaf.dialect.IDialect" };
5758

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.springframework.core.type.classreading.MetadataReaderFactory;
3030
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
3131
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
32+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
33+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
3234
import org.springframework.security.web.SecurityFilterChain;
3335
import org.springframework.stereotype.Controller;
3436
import org.springframework.stereotype.Repository;
@@ -64,6 +66,7 @@ void matchWhenHasNoControllers() throws Exception {
6466
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
6567
assertThat(excludes(filter, ExampleModule.class)).isFalse();
6668
assertThat(excludes(filter, ExampleDialect.class)).isFalse();
69+
assertThat(excludes(filter, ExampleWebSecurityCustomizer.class)).isFalse();
6770
}
6871

6972
@Test
@@ -81,6 +84,7 @@ void matchWhenHasController() throws Exception {
8184
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
8285
assertThat(excludes(filter, ExampleModule.class)).isFalse();
8386
assertThat(excludes(filter, ExampleDialect.class)).isFalse();
87+
assertThat(excludes(filter, ExampleWebSecurityCustomizer.class)).isFalse();
8488
}
8589

8690
@Test
@@ -98,6 +102,7 @@ void matchNotUsingDefaultFilters() throws Exception {
98102
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isTrue();
99103
assertThat(excludes(filter, ExampleModule.class)).isTrue();
100104
assertThat(excludes(filter, ExampleDialect.class)).isTrue();
105+
assertThat(excludes(filter, ExampleWebSecurityCustomizer.class)).isTrue();
101106
}
102107

103108
@Test
@@ -114,6 +119,7 @@ void matchWithIncludeFilter() throws Exception {
114119
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
115120
assertThat(excludes(filter, ExampleModule.class)).isFalse();
116121
assertThat(excludes(filter, ExampleDialect.class)).isFalse();
122+
assertThat(excludes(filter, ExampleWebSecurityCustomizer.class)).isFalse();
117123
}
118124

119125
@Test
@@ -131,6 +137,7 @@ void matchWithExcludeFilter() throws Exception {
131137
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
132138
assertThat(excludes(filter, ExampleModule.class)).isFalse();
133139
assertThat(excludes(filter, ExampleDialect.class)).isFalse();
140+
assertThat(excludes(filter, ExampleWebSecurityCustomizer.class)).isFalse();
134141
}
135142

136143
private boolean excludes(WebMvcTypeExcludeFilter filter, Class<?> type) throws IOException {
@@ -217,4 +224,13 @@ public String getName() {
217224

218225
}
219226

227+
static class ExampleWebSecurityCustomizer implements WebSecurityCustomizer {
228+
229+
@Override
230+
public void customize(WebSecurity web) {
231+
232+
}
233+
234+
}
235+
220236
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
20+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* Example {@link WebSecurityCustomizer} used with {@code @WebMvcTest} tests, particularly
25+
* to verify its discovery.
26+
*
27+
* @author Dmytro Nosan
28+
*/
29+
@Component
30+
class ExampleWebSecurityCustomizer implements WebSecurityCustomizer {
31+
32+
@Override
33+
public void customize(WebSecurity web) {
34+
web.ignoring().requestMatchers("/three/aaaa");
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.boot.test.context.TestConfiguration;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
26+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
27+
import org.springframework.security.web.SecurityFilterChain;
28+
import org.springframework.test.web.servlet.assertj.MockMvcTester;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link WebMvcTest @WebMvcTest} to validate {@link WebSecurityCustomizer} are
34+
* discovered.
35+
*
36+
* @author Dmytro Nosan
37+
*/
38+
@WebMvcTest(controllers = ExampleController3.class)
39+
class WebMvcTestWebSecurityCustomizerIntegrationTests {
40+
41+
@Autowired
42+
private MockMvcTester mvc;
43+
44+
@Test
45+
void shouldIncludesWebSecurityCustomizers() {
46+
assertThat(this.mvc.get().uri("/three")).hasStatus4xxClientError();
47+
assertThat(this.mvc.get().uri("/three/abcd")).hasStatus4xxClientError();
48+
assertThat(this.mvc.get().uri("/three/aaaa")).hasStatusOk().bodyText().isEqualTo("Hello aaaa");
49+
}
50+
51+
/**
52+
* Test security configuration to ensure that all endpoints are secured.
53+
*/
54+
@TestConfiguration(proxyBeanMethods = false)
55+
static class SecurityConfig {
56+
57+
@Bean
58+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
59+
return http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated()).build();
60+
}
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)