Skip to content

Commit a19be75

Browse files
committed
DefaultCorsProcessor checks for existing CORS response before attempting to compare origin
Issue: SPR-14080 (cherry picked from commit abe7345)
1 parent 8994498 commit a19be75

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

spring-web/src/main/java/org/springframework/web/cors/DefaultCorsProcessor.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@ public class DefaultCorsProcessor implements CorsProcessor {
5858

5959

6060
@Override
61+
@SuppressWarnings("resource")
6162
public boolean processRequest(CorsConfiguration config, HttpServletRequest request, HttpServletResponse response)
6263
throws IOException {
6364

@@ -66,14 +67,14 @@ public boolean processRequest(CorsConfiguration config, HttpServletRequest reque
6667
}
6768

6869
ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
69-
ServletServerHttpRequest serverRequest = new ServletServerHttpRequest(request);
70-
71-
if (WebUtils.isSameOrigin(serverRequest)) {
72-
logger.debug("Skip CORS processing, request is a same-origin one");
70+
if (responseHasCors(serverResponse)) {
71+
logger.debug("Skip CORS processing: response already contains \"Access-Control-Allow-Origin\" header");
7372
return true;
7473
}
75-
if (responseHasCors(serverResponse)) {
76-
logger.debug("Skip CORS processing, response already contains \"Access-Control-Allow-Origin\" header");
74+
75+
ServletServerHttpRequest serverRequest = new ServletServerHttpRequest(request);
76+
if (WebUtils.isSameOrigin(serverRequest)) {
77+
logger.debug("Skip CORS processing: request is from same origin");
7778
return true;
7879
}
7980

@@ -92,14 +93,13 @@ public boolean processRequest(CorsConfiguration config, HttpServletRequest reque
9293
}
9394

9495
private boolean responseHasCors(ServerHttpResponse response) {
95-
boolean hasAllowOrigin = false;
9696
try {
97-
hasAllowOrigin = (response.getHeaders().getAccessControlAllowOrigin() != null);
97+
return (response.getHeaders().getAccessControlAllowOrigin() != null);
9898
}
9999
catch (NullPointerException npe) {
100100
// SPR-11919 and https://issues.jboss.org/browse/WFLY-3474
101+
return false;
101102
}
102-
return hasAllowOrigin;
103103
}
104104

105105
/**
@@ -163,7 +163,7 @@ protected boolean handleInternal(ServerHttpRequest request, ServerHttpResponse r
163163
/**
164164
* Check the origin and determine the origin for the response. The default
165165
* implementation simply delegates to
166-
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}
166+
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}.
167167
*/
168168
protected String checkOrigin(CorsConfiguration config, String requestOrigin) {
169169
return config.checkOrigin(requestOrigin);
@@ -172,7 +172,7 @@ protected String checkOrigin(CorsConfiguration config, String requestOrigin) {
172172
/**
173173
* Check the HTTP method and determine the methods for the response of a
174174
* pre-flight request. The default implementation simply delegates to
175-
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}
175+
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}.
176176
*/
177177
protected List<HttpMethod> checkMethods(CorsConfiguration config, HttpMethod requestMethod) {
178178
return config.checkHttpMethod(requestMethod);
@@ -185,7 +185,7 @@ private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight
185185
/**
186186
* Check the headers and determine the headers for the response of a
187187
* pre-flight request. The default implementation simply delegates to
188-
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}
188+
* {@link org.springframework.web.cors.CorsConfiguration#checkOrigin(String)}.
189189
*/
190190
protected List<String> checkHeaders(CorsConfiguration config, List<String> requestHeaders) {
191191
return config.checkHeaders(requestHeaders);

0 commit comments

Comments
 (0)