@@ -81,7 +81,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
81
81
82
82
private final List <HandlerInterceptor > adaptedInterceptors = new ArrayList <>();
83
83
84
- private final UrlBasedCorsConfigurationSource globalCorsConfigSource = new UrlBasedCorsConfigurationSource ();
84
+ private CorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource ();
85
85
86
86
private CorsProcessor corsProcessor = new DefaultCorsProcessor ();
87
87
@@ -115,7 +115,9 @@ public Object getDefaultHandler() {
115
115
*/
116
116
public void setAlwaysUseFullPath (boolean alwaysUseFullPath ) {
117
117
this .urlPathHelper .setAlwaysUseFullPath (alwaysUseFullPath );
118
- this .globalCorsConfigSource .setAlwaysUseFullPath (alwaysUseFullPath );
118
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
119
+ ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).setAlwaysUseFullPath (alwaysUseFullPath );
120
+ }
119
121
}
120
122
121
123
/**
@@ -124,7 +126,9 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
124
126
*/
125
127
public void setUrlDecode (boolean urlDecode ) {
126
128
this .urlPathHelper .setUrlDecode (urlDecode );
127
- this .globalCorsConfigSource .setUrlDecode (urlDecode );
129
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
130
+ ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).setUrlDecode (urlDecode );
131
+ }
128
132
}
129
133
130
134
/**
@@ -133,7 +137,9 @@ public void setUrlDecode(boolean urlDecode) {
133
137
*/
134
138
public void setRemoveSemicolonContent (boolean removeSemicolonContent ) {
135
139
this .urlPathHelper .setRemoveSemicolonContent (removeSemicolonContent );
136
- this .globalCorsConfigSource .setRemoveSemicolonContent (removeSemicolonContent );
140
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
141
+ ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).setRemoveSemicolonContent (removeSemicolonContent );
142
+ }
137
143
}
138
144
139
145
/**
@@ -145,7 +151,9 @@ public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
145
151
public void setUrlPathHelper (UrlPathHelper urlPathHelper ) {
146
152
Assert .notNull (urlPathHelper , "UrlPathHelper must not be null" );
147
153
this .urlPathHelper = urlPathHelper ;
148
- this .globalCorsConfigSource .setUrlPathHelper (urlPathHelper );
154
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
155
+ ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).setUrlPathHelper (urlPathHelper );
156
+ }
149
157
}
150
158
151
159
/**
@@ -163,7 +171,9 @@ public UrlPathHelper getUrlPathHelper() {
163
171
public void setPathMatcher (PathMatcher pathMatcher ) {
164
172
Assert .notNull (pathMatcher , "PathMatcher must not be null" );
165
173
this .pathMatcher = pathMatcher ;
166
- this .globalCorsConfigSource .setPathMatcher (pathMatcher );
174
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
175
+ ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).setPathMatcher (pathMatcher );
176
+ }
167
177
}
168
178
169
179
/**
@@ -189,20 +199,45 @@ public void setInterceptors(Object... interceptors) {
189
199
}
190
200
191
201
/**
192
- * Set "global" CORS configuration based on URL patterns. By default the first
193
- * matching URL pattern is combined with the CORS configuration for the
194
- * handler, if any.
202
+ * Set the "global" CORS configurations based on URL patterns. By default the first
203
+ * matching URL pattern is combined with the CORS configuration for the handler, if any.
195
204
* @since 4.2
205
+ * @see #setCorsConfigurationSource(CorsConfigurationSource)
196
206
*/
197
207
public void setCorsConfigurations (Map <String , CorsConfiguration > corsConfigurations ) {
198
- this .globalCorsConfigSource .setCorsConfigurations (corsConfigurations );
208
+ Assert .notNull (corsConfigurations , "corsConfigurations must not be null" );
209
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
210
+ source .setCorsConfigurations (corsConfigurations );
211
+ source .setPathMatcher (this .pathMatcher );
212
+ source .setUrlPathHelper (this .urlPathHelper );
213
+ this .corsConfigurationSource = source ;
214
+ }
215
+
216
+ /**
217
+ * Set the "global" CORS configuration source. By default the first matching URL
218
+ * pattern is combined with the CORS configuration for the handler, if any.
219
+ * @since 5.1
220
+ * @see #setCorsConfigurations(Map)
221
+ */
222
+ public void setCorsConfigurationSource (CorsConfigurationSource corsConfigurationSource ) {
223
+ Assert .notNull (corsConfigurationSource , "corsConfigurationSource must not be null" );
224
+ this .corsConfigurationSource = corsConfigurationSource ;
199
225
}
200
226
201
227
/**
202
- * Get the "global" CORS configuration.
228
+ * Get the "global" CORS configurations.
229
+ * @deprecated as of 5.1 since it is now possible to set a {@link CorsConfigurationSource} which is not a
230
+ * {@link UrlBasedCorsConfigurationSource}. Expected to be removed in 5.2.
203
231
*/
232
+ @ Deprecated
204
233
public Map <String , CorsConfiguration > getCorsConfigurations () {
205
- return this .globalCorsConfigSource .getCorsConfigurations ();
234
+ if (this .corsConfigurationSource instanceof UrlBasedCorsConfigurationSource ) {
235
+ return ((UrlBasedCorsConfigurationSource )this .corsConfigurationSource ).getCorsConfigurations ();
236
+ }
237
+ else {
238
+ throw new IllegalStateException ("No CORS configurations available when the source " +
239
+ "is not an UrlBasedCorsConfigurationSource" );
240
+ }
206
241
}
207
242
208
243
/**
@@ -286,7 +321,8 @@ protected void detectMappedInterceptors(List<HandlerInterceptor> mappedIntercept
286
321
287
322
/**
288
323
* Initialize the specified interceptors, checking for {@link MappedInterceptor MappedInterceptors} and
289
- * adapting {@link HandlerInterceptor}s and {@link WebRequestInterceptor HandlerInterceptor}s and {@link WebRequestInterceptors} if necessary.
324
+ * adapting {@link HandlerInterceptor}s and {@link WebRequestInterceptor HandlerInterceptor}s and
325
+ * {@link WebRequestInterceptor}s if necessary.
290
326
* @see #setInterceptors
291
327
* @see #adaptInterceptor
292
328
*/
@@ -385,7 +421,7 @@ else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(Dispatch
385
421
}
386
422
387
423
if (CorsUtils .isCorsRequest (request )) {
388
- CorsConfiguration globalConfig = this .globalCorsConfigSource .getCorsConfiguration (request );
424
+ CorsConfiguration globalConfig = this .corsConfigurationSource .getCorsConfiguration (request );
389
425
CorsConfiguration handlerConfig = getCorsConfiguration (handler , request );
390
426
CorsConfiguration config = (globalConfig != null ? globalConfig .combine (handlerConfig ) : handlerConfig );
391
427
executionChain = getCorsHandlerExecutionChain (request , executionChain , config );
@@ -402,7 +438,7 @@ else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(Dispatch
402
438
* the pre-flight request but for the expected actual request based on the URL
403
439
* path, the HTTP methods from the "Access-Control-Request-Method" header, and
404
440
* the headers from the "Access-Control-Request-Headers" header thus allowing
405
- * the CORS configuration to be obtained via {@link #getCorsConfigurations },
441
+ * the CORS configuration to be obtained via {@link #getCorsConfiguration(Object, HttpServletRequest) },
406
442
* <p>Note: This method may also return a pre-built {@link HandlerExecutionChain},
407
443
* combining a handler object with dynamically determined interceptors.
408
444
* Statically specified interceptors will get merged into such an existing chain.
0 commit comments