Skip to content

Commit 5811212

Browse files
committed
Add PreFlightRequestWebFilter
Closes gh-26885
1 parent d79e33b commit 5811212

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

spring-web/src/main/java/org/springframework/web/cors/reactive/PreFlightRequestHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*
2626
* @author Rossen Stoyanchev
2727
* @since 5.3.4
28+
* @see PreFlightRequestWebFilter
2829
*/
2930
public interface PreFlightRequestHandler {
3031

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.cors.reactive;
17+
18+
import reactor.core.publisher.Mono;
19+
20+
import org.springframework.util.Assert;
21+
import org.springframework.web.server.ServerWebExchange;
22+
import org.springframework.web.server.WebFilter;
23+
import org.springframework.web.server.WebFilterChain;
24+
25+
/**
26+
* WebFilter that handles pre-flight requests through a
27+
* {@link PreFlightRequestHandler} and bypasses the rest of the chain.
28+
*
29+
* <p>A WebFlux application can simply inject PreFlightRequestHandler and use
30+
* it to create an instance of this WebFilter since {@code @EnableWebFlux}
31+
* declares {@code DispatcherHandler} as a bean and that is a
32+
* PreFlightRequestHandler.
33+
*
34+
* @author Rossen Stoyanchev
35+
* @since 5.3.7
36+
*/
37+
public class PreFlightRequestWebFilter implements WebFilter {
38+
39+
private final PreFlightRequestHandler handler;
40+
41+
42+
/**
43+
* Create an instance that will delegate to the given handler.
44+
*/
45+
public PreFlightRequestWebFilter(PreFlightRequestHandler handler) {
46+
Assert.notNull(handler, "PreFlightRequestHandler is required");
47+
this.handler = handler;
48+
}
49+
50+
51+
@Override
52+
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
53+
return (CorsUtils.isPreFlightRequest(exchange.getRequest()) ?
54+
this.handler.handlePreFlight(exchange) : chain.filter(exchange));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)