提交 9a65eec3 编写于 作者: S Sebastien Deleuze

Avoid registering CorsConfiguration for methods without @CrossOrigin

Issue: SPR-12931
上级 e829b2aa
......@@ -271,13 +271,15 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
CrossOrigin typeAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
CrossOrigin methodAnnotation = AnnotationUtils.findAnnotation(method, CrossOrigin.class);
CorsConfiguration config = new CorsConfiguration();
if (typeAnnotation == null && methodAnnotation == null) {
return null;
}
CrossOrigin typeAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
CorsConfiguration config = new CorsConfiguration();
applyAnnotation(config, typeAnnotation);
CrossOrigin methodAnnotation = AnnotationUtils.findAnnotation(method, CrossOrigin.class);
applyAnnotation(config, methodAnnotation);
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
......
......@@ -66,7 +66,7 @@ public class CrossOriginTests {
}
@Test
public void noAnnotation() throws Exception {
public void noAnnotationWithoutOrigin() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/no");
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
......@@ -74,6 +74,25 @@ public class CrossOriginTests {
assertNull(config);
}
@Test // SPR-12931
public void noAnnotationWithOrigin() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
this.request.setRequestURI("/no");
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
CorsConfiguration config = getCorsConfiguration(chain, false);
assertNull(config);
}
@Test // SPR-12931
public void noAnnotationPostWithOrigin() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
this.request.setMethod("POST");
this.request.setRequestURI("/no");
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
CorsConfiguration config = getCorsConfiguration(chain, false);
assertNull(config);
}
@Test
public void defaultAnnotation() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
......@@ -203,6 +222,10 @@ public class CrossOriginTests {
public void noAnnotation() {
}
@RequestMapping(value = "/no", method = RequestMethod.POST)
public void noAnnotationPost() {
}
@CrossOrigin
@RequestMapping(value = "/default", method = RequestMethod.GET)
public void defaultAnnotation() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册