Skip to content

Commit d91b153

Browse files
committed
Explicitly set useSuffixPatternMatch for Tests
Spring MVC changed their default behavior in spring-projects/spring-framework#23915 This causes failures in some of Spring Security's tests. This explicitly sets useSuffixPatternMatch=true to ensure that Spring Security still works if users have modified their defaults. Closes spring-projectsgh-8493
1 parent 6d5d883 commit d91b153

File tree

8 files changed

+69
-15
lines changed

8 files changed

+69
-15
lines changed

Jenkinsfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ try {
8989
"GRADLE_ENTERPRISE_CACHE_USERNAME=${GRADLE_ENTERPRISE_CACHE_USERNAME}",
9090
"GRADLE_ENTERPRISE_CACHE_PASSWORD=${GRADLE_ENTERPRISE_CACHE_PASSWORD}",
9191
"GRADLE_ENTERPRISE_ACCESS_KEY=${GRADLE_ENTERPRISE_ACCESS_KEY}"]) {
92-
sh "./gradlew test -PforceMavenRepositories=snapshot -PspringVersion='5.2.+' -PreactorVersion=Dysprosium-BUILD-SNAPSHOT -PspringDataVersion=Lovelace-BUILD-SNAPSHOT -PlocksDisabled --stacktrace"
92+
sh "./gradlew test -PforceMavenRepositories=snapshot -PspringVersion='5.+' -PreactorVersion=Dysprosium-BUILD-SNAPSHOT -PspringDataVersion=Lovelace-BUILD-SNAPSHOT -PlocksDisabled --stacktrace"
9393
}
9494
}
9595
} catch(Exception e) {

config/src/test/java/org/springframework/security/config/annotation/web/builders/WebSecurityTests.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.web.bind.annotation.RestController;
3737
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3838
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
39+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
40+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3941

4042
import static org.assertj.core.api.Assertions.assertThat;
4143

@@ -69,7 +71,7 @@ public void cleanup() {
6971

7072
@Test
7173
public void ignoringMvcMatcher() throws Exception {
72-
loadConfig(MvcMatcherConfig.class);
74+
loadConfig(MvcMatcherConfig.class, LegacyMvcMatchingConfig.class);
7375

7476
this.request.setRequestURI("/path");
7577
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -141,7 +143,7 @@ public String path() {
141143

142144
@Test
143145
public void ignoringMvcMatcherServletPath() throws Exception {
144-
loadConfig(MvcMatcherServletPathConfig.class);
146+
loadConfig(MvcMatcherServletPathConfig.class, LegacyMvcMatchingConfig.class);
145147

146148
this.request.setServletPath("/spring");
147149
this.request.setRequestURI("/spring/path");
@@ -216,6 +218,14 @@ public String path() {
216218
}
217219
}
218220

221+
@Configuration
222+
static class LegacyMvcMatchingConfig implements WebMvcConfigurer {
223+
@Override
224+
public void configurePathMatch(PathMatchConfigurer configurer) {
225+
configurer.setUseSuffixPatternMatch(true);
226+
}
227+
}
228+
219229
public void loadConfig(Class<?>... configs) {
220230
this.context = new AnnotationConfigWebApplicationContext();
221231
this.context.register(configs);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeRequestsTests.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import org.springframework.web.bind.annotation.RestController;
4747
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
4848
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
49+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
50+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4951

5052
import static org.assertj.core.api.Assertions.assertThat;
5153
import static org.mockito.Mockito.spy;
@@ -292,7 +294,7 @@ public RoleHierarchy roleHiearchy() {
292294

293295
@Test
294296
public void mvcMatcher() throws Exception {
295-
loadConfig(MvcMatcherConfig.class);
297+
loadConfig(MvcMatcherConfig.class, LegacyMvcMatchingConfig.class);
296298

297299
this.request.setRequestURI("/path");
298300
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -350,7 +352,7 @@ public String path() {
350352

351353
@Test
352354
public void requestWhenMvcMatcherDenyAllThenRespondsWithUnauthorized() throws Exception {
353-
loadConfig(MvcMatcherInLambdaConfig.class);
355+
loadConfig(MvcMatcherInLambdaConfig.class, LegacyMvcMatchingConfig.class);
354356

355357
this.request.setRequestURI("/path");
356358
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -410,7 +412,7 @@ public String path() {
410412

411413
@Test
412414
public void mvcMatcherServletPath() throws Exception {
413-
loadConfig(MvcMatcherServletPathConfig.class);
415+
loadConfig(MvcMatcherServletPathConfig.class, LegacyMvcMatchingConfig.class);
414416

415417
this.request.setServletPath("/spring");
416418
this.request.setRequestURI("/spring/path");
@@ -487,7 +489,7 @@ public String path() {
487489

488490
@Test
489491
public void requestWhenMvcMatcherServletPathDenyAllThenMatchesOnServletPath() throws Exception {
490-
loadConfig(MvcMatcherServletPathInLambdaConfig.class);
492+
loadConfig(MvcMatcherServletPathInLambdaConfig.class, LegacyMvcMatchingConfig.class);
491493

492494
this.request.setServletPath("/spring");
493495
this.request.setRequestURI("/spring/path");
@@ -697,6 +699,14 @@ public String path() {
697699
}
698700
}
699701

702+
@Configuration
703+
static class LegacyMvcMatchingConfig implements WebMvcConfigurer {
704+
@Override
705+
public void configurePathMatch(PathMatchConfigurer configurer) {
706+
configurer.setUseSuffixPatternMatch(true);
707+
}
708+
}
709+
700710
public void loadConfig(Class<?>... configs) {
701711
this.context = new AnnotationConfigWebApplicationContext();
702712
this.context.register(configs);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecurityRequestMatchersTests.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.web.bind.annotation.RestController;
3737
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3838
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
39+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
40+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3941

4042
import static org.assertj.core.api.Assertions.assertThat;
4143
import static org.springframework.security.config.Customizer.withDefaults;
@@ -71,7 +73,7 @@ public void cleanup() {
7173

7274
@Test
7375
public void mvcMatcher() throws Exception {
74-
loadConfig(MvcMatcherConfig.class);
76+
loadConfig(MvcMatcherConfig.class, LegacyMvcMatchingConfig.class);
7577

7678
this.request.setServletPath("/path");
7779
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -137,7 +139,7 @@ public String path() {
137139

138140
@Test
139141
public void requestMatchersMvcMatcher() throws Exception {
140-
loadConfig(RequestMatchersMvcMatcherConfig.class);
142+
loadConfig(RequestMatchersMvcMatcherConfig.class, LegacyMvcMatchingConfig.class);
141143

142144
this.request.setServletPath("/path");
143145
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -198,7 +200,7 @@ public String path() {
198200

199201
@Test
200202
public void requestMatchersWhenMvcMatcherInLambdaThenPathIsSecured() throws Exception {
201-
loadConfig(RequestMatchersMvcMatcherInLambdaConfig.class);
203+
loadConfig(RequestMatchersMvcMatcherInLambdaConfig.class, LegacyMvcMatchingConfig.class);
202204

203205
this.request.setServletPath("/path");
204206
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -377,6 +379,14 @@ public String path() {
377379
}
378380
}
379381

382+
@Configuration
383+
static class LegacyMvcMatchingConfig implements WebMvcConfigurer {
384+
@Override
385+
public void configurePathMatch(PathMatchConfigurer configurer) {
386+
configurer.setUseSuffixPatternMatch(true);
387+
}
388+
}
389+
380390
public void loadConfig(Class<?>... configs) {
381391
this.context = new AnnotationConfigWebApplicationContext();
382392
this.context.register(configs);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/UrlAuthorizationConfigurerTests.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.web.bind.annotation.RestController;
3737
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3838
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
39+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
40+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3941

4042
import static org.assertj.core.api.Assertions.assertThat;
4143

@@ -71,7 +73,7 @@ public void cleanup() {
7173

7274
@Test
7375
public void mvcMatcher() throws Exception {
74-
loadConfig(MvcMatcherConfig.class);
76+
loadConfig(MvcMatcherConfig.class, LegacyMvcMatchingConfig.class);
7577

7678
this.request.setRequestURI("/path");
7779
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
@@ -129,7 +131,7 @@ public String path() {
129131

130132
@Test
131133
public void mvcMatcherServletPath() throws Exception {
132-
loadConfig(MvcMatcherServletPathConfig.class);
134+
loadConfig(MvcMatcherServletPathConfig.class, LegacyMvcMatchingConfig.class);
133135

134136
this.request.setServletPath("/spring");
135137
this.request.setRequestURI("/spring/path");
@@ -222,6 +224,14 @@ public void configure(HttpSecurity http) throws Exception {
222224
}
223225
}
224226

227+
@Configuration
228+
static class LegacyMvcMatchingConfig implements WebMvcConfigurer {
229+
@Override
230+
public void configurePathMatch(PathMatchConfigurer configurer) {
231+
configurer.setUseSuffixPatternMatch(true);
232+
}
233+
}
234+
225235
public void loadConfig(Class<?>... configs) {
226236
this.context = new AnnotationConfigWebApplicationContext();
227237
this.context.register(configs);

config/src/test/kotlin/org/springframework/security/config/web/servlet/AuthorizeRequestsDslTests.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.junit.Rule
2020
import org.junit.Test
2121
import org.springframework.beans.factory.annotation.Autowired
2222
import org.springframework.context.annotation.Bean
23+
import org.springframework.context.annotation.Configuration
2324
import org.springframework.http.HttpMethod
2425
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2526
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
@@ -42,6 +43,8 @@ import org.springframework.web.bind.annotation.PathVariable
4243
import org.springframework.web.bind.annotation.RequestMapping
4344
import org.springframework.web.bind.annotation.RestController
4445
import org.springframework.web.servlet.config.annotation.EnableWebMvc
46+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
47+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
4548

4649
/**
4750
* Tests for [AuthorizeRequestsDsl]
@@ -128,7 +131,7 @@ class AuthorizeRequestsDslTests {
128131

129132
@Test
130133
fun `request when allowed by mvc then responds with OK`() {
131-
this.spring.register(AuthorizeRequestsByMvcConfig::class.java).autowire()
134+
this.spring.register(AuthorizeRequestsByMvcConfig::class.java, LegacyMvcMatchingConfig::class.java).autowire()
132135

133136
this.mockMvc.get("/path")
134137
.andExpect {
@@ -166,6 +169,13 @@ class AuthorizeRequestsDslTests {
166169
}
167170
}
168171

172+
@Configuration
173+
open class LegacyMvcMatchingConfig : WebMvcConfigurer {
174+
override fun configurePathMatch(configurer: PathMatchConfigurer) {
175+
configurer.setUseSuffixPatternMatch(true)
176+
}
177+
}
178+
169179
@Test
170180
fun `request when secured by mvc path variables then responds based on path variable value`() {
171181
this.spring.register(MvcMatcherPathVariablesConfig::class.java).autowire()

config/src/test/resources/org/springframework/security/config/http/InterceptUrlConfigTests-MvcMatchers.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
<http-basic/>
3333
</http>
3434

35-
<mvc:annotation-driven/>
35+
<mvc:annotation-driven>
36+
<mvc:path-matching suffix-pattern="true"/>
37+
</mvc:annotation-driven>
3638

3739
<b:bean name="path" class="org.springframework.security.config.http.InterceptUrlConfigTests.PathController"/>
3840

config/src/test/resources/org/springframework/security/config/http/InterceptUrlConfigTests-MvcMatchersServletPath.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
<http-basic/>
3333
</http>
3434

35-
<mvc:annotation-driven/>
35+
<mvc:annotation-driven>
36+
<mvc:path-matching suffix-pattern="true"/>
37+
</mvc:annotation-driven>
3638

3739
<b:bean name="path" class="org.springframework.security.config.http.InterceptUrlConfigTests.PathController"/>
3840

0 commit comments

Comments
 (0)