Skip to content

Commit 9cc4bd8

Browse files
committed
Fix issue with suffix pattern match
Spring Framework 3.2 M2 added the ability to map requests using only file extensions registered through the configured through a ContentNeotiationManager, as opposed to allowing any file extension (i.e. ".*"). The MVC namespace the MVC Java config automatically register extensions such as ".json" and ".xml" depending on libraries found on the classpath. That in turn causes issues in cases where additional extensions are in use but not registered (e.g. ".html"). This change ensures that matching with registered file extensions only works only if explicitly enabled through a property on RequestMappingHandlerMapping. Issue: SPR-10061, SPR-8474
1 parent fb05c7b commit 9cc4bd8

File tree

2 files changed

+128
-6
lines changed

2 files changed

+128
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,47 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
5050

5151
private boolean useSuffixPatternMatch = true;
5252

53+
private boolean useRegisteredSuffixPatternMatch = false;
54+
5355
private boolean useTrailingSlashMatch = true;
5456

5557
private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();
5658

57-
private final List<String> contentNegotiationFileExtensions = new ArrayList<String>();
59+
private final List<String> fileExtensions = new ArrayList<String>();
5860

5961
/**
6062
* Whether to use suffix pattern match (".*") when matching patterns to
6163
* requests. If enabled a method mapped to "/users" also matches to "/users.*".
6264
* <p>The default value is {@code true}.
65+
* <p>Also see {@link #setUseRegisteredSuffixPatternMatch(boolean)} for
66+
* more fine-grained control over specific suffices to allow.
6367
*/
6468
public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
6569
this.useSuffixPatternMatch = useSuffixPatternMatch;
6670
}
6771

72+
/**
73+
* Whether to use suffix pattern match for registered file extensions only
74+
* when matching patterns to requests.
75+
*
76+
* <p>If enabled, a controller method mapped to "/users" also matches to
77+
* "/users.json" assuming ".json" is a file extension registered with the
78+
* provided {@link #setContentNegotiationManager(ContentNegotiationManager)
79+
* contentNegotiationManager}. This can be useful for allowing only specific
80+
* URL extensions to be used as well as in cases where a "." in the URL path
81+
* can lead to ambiguous interpretation of path variable content, (e.g. given
82+
* "/users/{user}" and incoming URLs such as "/users/john.j.joe" and
83+
* "/users/john.j.joe.json").
84+
*
85+
* <p>If enabled, this flag also enables
86+
* {@link #setUseSuffixPatternMatch(boolean) useSuffixPatternMatch}. The
87+
* default value is {@code false}.
88+
*/
89+
public void setUseRegisteredSuffixPatternMatch(boolean useRegsiteredSuffixPatternMatch) {
90+
this.useRegisteredSuffixPatternMatch = useRegsiteredSuffixPatternMatch;
91+
this.useSuffixPatternMatch = useRegsiteredSuffixPatternMatch ? true : this.useSuffixPatternMatch;
92+
}
93+
6894
/**
6995
* Whether to match to URLs irrespective of the presence of a trailing slash.
7096
* If enabled a method mapped to "/users" also matches to "/users/".
@@ -81,7 +107,6 @@ public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
81107
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
82108
Assert.notNull(contentNegotiationManager);
83109
this.contentNegotiationManager = contentNegotiationManager;
84-
this.contentNegotiationFileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
85110
}
86111

87112
/**
@@ -90,6 +115,14 @@ public void setContentNegotiationManager(ContentNegotiationManager contentNegoti
90115
public boolean useSuffixPatternMatch() {
91116
return this.useSuffixPatternMatch;
92117
}
118+
119+
/**
120+
* Whether to use registered suffixes for pattern matching.
121+
*/
122+
public boolean useRegisteredSuffixPatternMatch() {
123+
return useRegisteredSuffixPatternMatch;
124+
}
125+
93126
/**
94127
* Whether to match to URLs irrespective of the presence of a trailing slash.
95128
*/
@@ -105,10 +138,18 @@ public ContentNegotiationManager getContentNegotiationManager() {
105138
}
106139

107140
/**
108-
* Return the known file extensions for content negotiation.
141+
* Return the file extensions to use for suffix pattern matching.
109142
*/
110-
public List<String> getContentNegotiationFileExtensions() {
111-
return this.contentNegotiationFileExtensions;
143+
public List<String> getFileExtensions() {
144+
return fileExtensions;
145+
}
146+
147+
@Override
148+
public void afterPropertiesSet() {
149+
super.afterPropertiesSet();
150+
if (this.useRegisteredSuffixPatternMatch) {
151+
this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
152+
}
112153
}
113154

114155
/**
@@ -187,7 +228,7 @@ protected RequestCondition<?> getCustomMethodCondition(Method method) {
187228
private RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition) {
188229
return new RequestMappingInfo(
189230
new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(),
190-
this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.contentNegotiationFileExtensions),
231+
this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions),
191232
new RequestMethodsRequestCondition(annotation.method()),
192233
new ParamsRequestCondition(annotation.params()),
193234
new HeadersRequestCondition(annotation.headers()),
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2002-2012 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+
* http://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+
package org.springframework.web.servlet.mvc.method.annotation;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.Map;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.springframework.http.MediaType;
27+
import org.springframework.web.accept.ContentNegotiationManager;
28+
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
29+
import org.springframework.web.context.support.StaticWebApplicationContext;
30+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
31+
32+
/**
33+
* Tests for {@link RequestMappingHandlerMapping}.
34+
*
35+
* @author Rossen Stoyanchev
36+
*/
37+
public class RequestMappingHandlerMappingTests {
38+
39+
private RequestMappingHandlerMapping handlerMapping;
40+
41+
@Before
42+
public void setup() {
43+
this.handlerMapping = new RequestMappingHandlerMapping();
44+
this.handlerMapping.setApplicationContext(new StaticWebApplicationContext());
45+
}
46+
47+
@Test
48+
public void useRegsiteredSuffixPatternMatch() {
49+
assertTrue(this.handlerMapping.useSuffixPatternMatch());
50+
assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());
51+
52+
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
53+
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
54+
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
55+
56+
this.handlerMapping.setContentNegotiationManager(manager);
57+
this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
58+
this.handlerMapping.afterPropertiesSet();
59+
60+
assertTrue(this.handlerMapping.useSuffixPatternMatch());
61+
assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
62+
assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
63+
}
64+
65+
@Test
66+
public void useSuffixPatternMatch() {
67+
assertTrue(this.handlerMapping.useSuffixPatternMatch());
68+
69+
this.handlerMapping.setUseSuffixPatternMatch(false);
70+
assertFalse(this.handlerMapping.useSuffixPatternMatch());
71+
72+
this.handlerMapping.setUseRegisteredSuffixPatternMatch(false);
73+
assertFalse("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch",
74+
this.handlerMapping.useSuffixPatternMatch());
75+
76+
this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
77+
assertTrue("'true' registeredSuffixPatternMatch should enable suffixPatternMatch",
78+
this.handlerMapping.useSuffixPatternMatch());
79+
}
80+
81+
}

0 commit comments

Comments
 (0)