Skip to content

Commit 9a65eec

Browse files
committed
Avoid registering CorsConfiguration for methods without @crossorigin
Issue: SPR-12931
1 parent e829b2a commit 9a65eec

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,15 @@ protected String[] resolveEmbeddedValuesInPatterns(String[] patterns) {
271271
@Override
272272
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
273273
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
274+
CrossOrigin typeAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
275+
CrossOrigin methodAnnotation = AnnotationUtils.findAnnotation(method, CrossOrigin.class);
274276

275-
CorsConfiguration config = new CorsConfiguration();
277+
if (typeAnnotation == null && methodAnnotation == null) {
278+
return null;
279+
}
276280

277-
CrossOrigin typeAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
281+
CorsConfiguration config = new CorsConfiguration();
278282
applyAnnotation(config, typeAnnotation);
279-
280-
CrossOrigin methodAnnotation = AnnotationUtils.findAnnotation(method, CrossOrigin.class);
281283
applyAnnotation(config, methodAnnotation);
282284

283285
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,33 @@ public void setUp() {
6666
}
6767

6868
@Test
69-
public void noAnnotation() throws Exception {
69+
public void noAnnotationWithoutOrigin() throws Exception {
7070
this.handlerMapping.registerHandler(new MethodLevelController());
7171
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/no");
7272
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
7373
CorsConfiguration config = getCorsConfiguration(chain, false);
7474
assertNull(config);
7575
}
7676

77+
@Test // SPR-12931
78+
public void noAnnotationWithOrigin() throws Exception {
79+
this.handlerMapping.registerHandler(new MethodLevelController());
80+
this.request.setRequestURI("/no");
81+
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
82+
CorsConfiguration config = getCorsConfiguration(chain, false);
83+
assertNull(config);
84+
}
85+
86+
@Test // SPR-12931
87+
public void noAnnotationPostWithOrigin() throws Exception {
88+
this.handlerMapping.registerHandler(new MethodLevelController());
89+
this.request.setMethod("POST");
90+
this.request.setRequestURI("/no");
91+
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
92+
CorsConfiguration config = getCorsConfiguration(chain, false);
93+
assertNull(config);
94+
}
95+
7796
@Test
7897
public void defaultAnnotation() throws Exception {
7998
this.handlerMapping.registerHandler(new MethodLevelController());
@@ -203,6 +222,10 @@ private static class MethodLevelController {
203222
public void noAnnotation() {
204223
}
205224

225+
@RequestMapping(value = "/no", method = RequestMethod.POST)
226+
public void noAnnotationPost() {
227+
}
228+
206229
@CrossOrigin
207230
@RequestMapping(value = "/default", method = RequestMethod.GET)
208231
public void defaultAnnotation() {

0 commit comments

Comments
 (0)