Skip to content

Commit 1f6f0dc

Browse files
committed
Add getHandler in reactive AbstractHandlerMapping
1 parent 33c48e7 commit 1f6f0dc

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ protected void configureViewResolvers(ViewResolverRegistry registry) {
469469
private static final class EmptyHandlerMapping extends AbstractHandlerMapping {
470470

471471
@Override
472-
public Mono<Object> getHandler(ServerWebExchange exchange) {
472+
public Mono<Object> getHandlerInternal(ServerWebExchange exchange) {
473473
return Mono.empty();
474474
}
475475
}

spring-web-reactive/src/main/java/org/springframework/web/reactive/handler/AbstractHandlerMapping.java

+29-11
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,38 @@ public CorsProcessor getCorsProcessor() {
150150
}
151151

152152

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;
153+
@Override
154+
public Mono<Object> getHandler(ServerWebExchange exchange) {
155+
return getHandlerInternal(exchange).map(handler -> {
156+
if (CorsUtils.isCorsRequest(exchange.getRequest())) {
157+
CorsConfiguration configA = this.globalCorsConfigSource.getCorsConfiguration(exchange);
158+
CorsConfiguration configB = getCorsConfiguration(handler, exchange);
159+
CorsConfiguration config = (configA != null ? configA.combine(configB) : configB);
160+
161+
if (!getCorsProcessor().processRequest(config, exchange) ||
162+
CorsUtils.isPreFlightRequest(exchange.getRequest())) {
163+
return REQUEST_HANDLED_HANDLER;
164+
}
162165
}
163-
}
164-
return handler;
166+
return handler;
167+
});
165168
}
166169

170+
/**
171+
* Look up a handler for the given request, returning an empty {@code Mono}
172+
* if no specific one is found. This method is called by {@link #getHandler}.
173+
*
174+
* <p>On CORS pre-flight requests this method should return a match not for
175+
* the pre-flight request but for the expected actual request based on the URL
176+
* path, the HTTP methods from the "Access-Control-Request-Method" header, and
177+
* the headers from the "Access-Control-Request-Headers" header thus allowing
178+
* the CORS configuration to be obtained via {@link #getCorsConfigurations},
179+
*
180+
* @param exchange current exchange
181+
* @return {@code Mono} for the matching handler, if any
182+
*/
183+
protected abstract Mono<?> getHandlerInternal(ServerWebExchange exchange);
184+
167185
/**
168186
* Retrieve the CORS configuration for the given handler.
169187
* @param handler the handler to check (never {@code null}).

spring-web-reactive/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ public final Map<String, Object> getHandlerMap() {
9696

9797

9898
@Override
99-
public Mono<Object> getHandler(ServerWebExchange exchange) {
99+
public Mono<Object> getHandlerInternal(ServerWebExchange exchange) {
100100
String lookupPath = getPathHelper().getLookupPathForRequest(exchange);
101101
Object handler;
102102
try {
103103
handler = lookupHandler(lookupPath, exchange);
104-
handler = processCorsRequest(exchange, handler);
105104
}
106105
catch (Exception ex) {
107106
return Mono.error(ex);

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ protected void handlerMethodsInitialized(Map<T, HandlerMethod> handlerMethods) {
254254
* @param exchange the current exchange
255255
*/
256256
@Override
257-
public Mono<Object> getHandler(ServerWebExchange exchange) {
257+
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange exchange) {
258258
String lookupPath = getPathHelper().getLookupPathForRequest(exchange);
259259
if (logger.isDebugEnabled()) {
260260
logger.debug("Looking up handler method for path " + lookupPath);
@@ -279,7 +279,7 @@ public Mono<Object> getHandler(ServerWebExchange exchange) {
279279
if (handlerMethod != null) {
280280
handlerMethod = handlerMethod.createWithResolvedBean();
281281
}
282-
return Mono.justOrEmpty(processCorsRequest(exchange, handlerMethod));
282+
return Mono.justOrEmpty(handlerMethod);
283283
}
284284
finally {
285285
this.mappingRegistry.releaseReadLock();

0 commit comments

Comments
 (0)