use of org.springframework.http.server.reactive.ServerHttpRequest in project JavaForFun by gumartinm.
the class UsernameFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
if (!request.getHeaders().containsKey(UsernameThreadContext.USERNAME_HEADER)) {
return chain.filter(exchange);
}
String username = request.getHeaders().get(UsernameThreadContext.USERNAME_HEADER).get(0);
return chain.filter(exchange).compose(function -> function.then(Mono.subscriberContext()).doOnSubscribe(onSubscribe -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).doOnError(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).onErrorMap(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
return throwable;
}).doFinally(onFinally -> {
MDC.remove(UsernameThreadContext.USERNAME_HEADER);
}).flatMap(context -> {
Mono<Void> continuation = Mono.empty();
return continuation;
}).subscriberContext(context -> {
Context updatedContext = context;
if (!context.hasKey(UsernameContext.class)) {
updatedContext = context.put(UsernameContext.class, new UsernameContext(username));
}
return updatedContext;
}));
}
use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.
the class DefaultCorsProcessor method process.
@Override
public boolean process(@Nullable CorsConfiguration config, ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
HttpHeaders responseHeaders = response.getHeaders();
List<String> varyHeaders = responseHeaders.get(HttpHeaders.VARY);
if (varyHeaders == null) {
responseHeaders.addAll(HttpHeaders.VARY, VARY_HEADERS);
} else {
for (String header : VARY_HEADERS) {
if (!varyHeaders.contains(header)) {
responseHeaders.add(HttpHeaders.VARY, header);
}
}
}
if (!CorsUtils.isCorsRequest(request)) {
return true;
}
if (responseHeaders.getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) != null) {
logger.trace("Skip: response already contains \"Access-Control-Allow-Origin\"");
return true;
}
boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
if (config == null) {
if (preFlightRequest) {
rejectRequest(response);
return false;
} else {
return true;
}
}
return handleInternal(exchange, config, preFlightRequest);
}
use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.
the class CorsWebFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(exchange);
boolean isValid = this.processor.process(corsConfiguration, exchange);
if (!isValid || CorsUtils.isPreFlightRequest(request)) {
return Mono.empty();
}
return chain.filter(exchange);
}
use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.
the class CorsUtilsTests method isNotCorsRequest.
@Test
public void isNotCorsRequest() {
ServerHttpRequest request = get("/").build();
assertThat(CorsUtils.isCorsRequest(request)).isFalse();
}
use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.
the class CorsUtilsTests method testWithForwardedHeader.
@SuppressWarnings("deprecation")
private void testWithForwardedHeader(String serverName, int port, String forwardedHeader, String originHeader) {
String url = "http://" + serverName;
if (port != -1) {
url = url + ":" + port;
}
MockServerHttpRequest.BaseBuilder<?> builder = get(url).header("Forwarded", forwardedHeader).header(HttpHeaders.ORIGIN, originHeader);
ServerHttpRequest request = adaptFromForwardedHeaders(builder);
assertThat(CorsUtils.isSameOrigin(request)).isTrue();
}
Aggregations