@@ -51,9 +51,10 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
51
51
52
52
private PathMatcher pathMatcher = new AntPathMatcher ();
53
53
54
- protected CorsProcessor corsProcessor = new DefaultCorsProcessor ();
54
+ private final UrlBasedCorsConfigurationSource globalCorsConfigSource = new UrlBasedCorsConfigurationSource ();
55
+
56
+ private CorsProcessor corsProcessor = new DefaultCorsProcessor ();
55
57
56
- protected final UrlBasedCorsConfigurationSource corsConfigSource = new UrlBasedCorsConfigurationSource ();
57
58
58
59
/**
59
60
* Specify the order value for this HandlerMapping bean.
@@ -104,7 +105,7 @@ public HttpRequestPathHelper getPathHelper() {
104
105
public void setPathMatcher (PathMatcher pathMatcher ) {
105
106
Assert .notNull (pathMatcher , "PathMatcher must not be null" );
106
107
this .pathMatcher = pathMatcher ;
107
- this .corsConfigSource .setPathMatcher (pathMatcher );
108
+ this .globalCorsConfigSource .setPathMatcher (pathMatcher );
108
109
}
109
110
110
111
/**
@@ -115,9 +116,26 @@ public PathMatcher getPathMatcher() {
115
116
return this .pathMatcher ;
116
117
}
117
118
119
+ /**
120
+ * Set "global" CORS configuration based on URL patterns. By default the
121
+ * first matching URL pattern is combined with handler-level CORS
122
+ * configuration if any.
123
+ */
124
+ public void setCorsConfigurations (Map <String , CorsConfiguration > corsConfigurations ) {
125
+ this .globalCorsConfigSource .setCorsConfigurations (corsConfigurations );
126
+ }
127
+
128
+ /**
129
+ * Return the "global" CORS configuration.
130
+ */
131
+ public Map <String , CorsConfiguration > getCorsConfigurations () {
132
+ return this .globalCorsConfigSource .getCorsConfigurations ();
133
+ }
134
+
118
135
/**
119
136
* Configure a custom {@link CorsProcessor} to use to apply the matched
120
- * {@link CorsConfiguration} for a request. By default {@link DefaultCorsProcessor} is used.
137
+ * {@link CorsConfiguration} for a request.
138
+ * <p>By default an instance of {@link DefaultCorsProcessor} is used.
121
139
*/
122
140
public void setCorsProcessor (CorsProcessor corsProcessor ) {
123
141
Assert .notNull (corsProcessor , "CorsProcessor must not be null" );
@@ -131,46 +149,35 @@ public CorsProcessor getCorsProcessor() {
131
149
return this .corsProcessor ;
132
150
}
133
151
134
- /**
135
- * Set "global" CORS configuration based on URL patterns. By default the first
136
- * matching URL pattern is combined with the CORS configuration for the
137
- * handler, if any.
138
- */
139
- public void setCorsConfigurations (Map <String , CorsConfiguration > corsConfigurations ) {
140
- this .corsConfigSource .setCorsConfigurations (corsConfigurations );
152
+
153
+ protected Object processCorsRequest (ServerWebExchange exchange , Object handler ) {
154
+ if (CorsUtils .isCorsRequest (exchange .getRequest ())) {
155
+ CorsConfiguration configA = this .globalCorsConfigSource .getCorsConfiguration (exchange );
156
+ CorsConfiguration configB = getCorsConfiguration (handler , exchange );
157
+ CorsConfiguration config = (configA != null ? configA .combine (configB ) : configB );
158
+
159
+ if (!getCorsProcessor ().processRequest (config , exchange ) ||
160
+ CorsUtils .isPreFlightRequest (exchange .getRequest ())) {
161
+ return REQUEST_HANDLED_HANDLER ;
162
+ }
163
+ }
164
+ return handler ;
141
165
}
142
166
143
167
/**
144
- * Get the CORS configuration.
168
+ * Retrieve the CORS configuration for the given handler.
169
+ * @param handler the handler to check (never {@code null}).
170
+ * @param exchange the current exchange
171
+ * @return the CORS configuration for the handler or {@code null}.
145
172
*/
146
- public Map <String , CorsConfiguration > getCorsConfigurations () {
147
- return this .corsConfigSource .getCorsConfigurations ();
148
- }
149
-
150
173
protected CorsConfiguration getCorsConfiguration (Object handler , ServerWebExchange exchange ) {
151
174
if (handler != null && handler instanceof CorsConfigurationSource ) {
152
175
return ((CorsConfigurationSource ) handler ).getCorsConfiguration (exchange );
153
176
}
154
177
return null ;
155
178
}
156
179
157
- protected Object processCorsRequest (ServerWebExchange exchange , Object handler ) {
158
- if (CorsUtils .isCorsRequest (exchange .getRequest ())) {
159
- CorsConfiguration globalConfig = this .corsConfigSource .getCorsConfiguration (exchange );
160
- CorsConfiguration handlerConfig = getCorsConfiguration (handler , exchange );
161
- CorsConfiguration config = (globalConfig != null ? globalConfig .combine (handlerConfig ) : handlerConfig );
162
- if (!corsProcessor .processRequest (config , exchange ) || CorsUtils .isPreFlightRequest (exchange .getRequest ())) {
163
- return new NoOpHandler ();
164
- }
165
- }
166
- return handler ;
167
- }
168
180
169
- private class NoOpHandler implements WebHandler {
170
- @ Override
171
- public Mono <Void > handle (ServerWebExchange exchange ) {
172
- return Mono .empty ();
173
- }
174
- }
181
+ private static final WebHandler REQUEST_HANDLED_HANDLER = exchange -> Mono .empty ();
175
182
176
- }
183
+ }
0 commit comments