Search in sources :

Example 41 with ServerHttpRequest

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;
    }));
}
Also used : UsernameThreadContext(de.spring.example.context.UsernameThreadContext) WebFilter(org.springframework.web.server.WebFilter) MDC(org.slf4j.MDC) Context(reactor.util.context.Context) Mono(reactor.core.publisher.Mono) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) UsernameThreadContext(de.spring.example.context.UsernameThreadContext) Context(reactor.util.context.Context) UsernameContext(de.spring.example.context.UsernameContext) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 42 with ServerHttpRequest

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 43 with ServerHttpRequest

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);
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 44 with ServerHttpRequest

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();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 45 with ServerHttpRequest

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();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)71 HttpHeaders (org.springframework.http.HttpHeaders)26 Test (org.junit.jupiter.api.Test)25 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)20 ServerWebExchange (org.springframework.web.server.ServerWebExchange)19 URI (java.net.URI)17 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)17 Mono (reactor.core.publisher.Mono)13 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)10 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)9 HttpMethod (org.springframework.http.HttpMethod)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Map (java.util.Map)6 HttpStatus (org.springframework.http.HttpStatus)6 MediaType (org.springframework.http.MediaType)6 Flux (reactor.core.publisher.Flux)6 Principal (java.security.Principal)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5