Search in sources :

Example 1 with FilteringWebHandler

use of org.springframework.web.server.handler.FilteringWebHandler in project spring-boot by spring-projects.

the class HttpHandlerAutoConfigurationTests method shouldConfigureWebFiltersAnnotation.

@Test
public void shouldConfigureWebFiltersAnnotation() {
    load(AnnotationConfigWithWebFilters.class);
    HttpHandler handler = this.context.getBean(HttpHandler.class);
    assertThat(handler).isInstanceOf(WebHandler.class);
    WebHandler webHandler = (WebHandler) handler;
    while (webHandler instanceof WebHandlerDecorator) {
        if (webHandler instanceof FilteringWebHandler) {
            FilteringWebHandler filteringWebHandler = (FilteringWebHandler) webHandler;
            assertThat(filteringWebHandler.getFilters()).containsExactly(this.context.getBean("firstWebFilter", WebFilter.class), this.context.getBean("aWebFilter", WebFilter.class), this.context.getBean("lastWebFilter", WebFilter.class));
            return;
        }
        webHandler = ((WebHandlerDecorator) webHandler).getDelegate();
    }
    fail("Did not find any FilteringWebHandler");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) WebHandlerDecorator(org.springframework.web.server.handler.WebHandlerDecorator) WebHandler(org.springframework.web.server.WebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) Test(org.junit.Test)

Example 2 with FilteringWebHandler

use of org.springframework.web.server.handler.FilteringWebHandler in project spring-security by spring-projects.

the class OAuth2AuthorizationRequestRedirectWebFilterTests method filterWhenExceptionThenRedirected.

@Test
public void filterWhenExceptionThenRedirected() {
    given(this.clientRepository.findByRegistrationId(this.registration.getRegistrationId())).willReturn(Mono.just(this.registration));
    given(this.authzRequestRepository.saveAuthorizationRequest(any(), any())).willReturn(Mono.empty());
    FilteringWebHandler webHandler = new FilteringWebHandler((e) -> Mono.error(new ClientAuthorizationRequiredException(this.registration.getRegistrationId())), Arrays.asList(this.filter));
    // @formatter:off
    this.client = WebTestClient.bindToWebHandler(webHandler).build();
    FluxExchangeResult<String> result = this.client.get().uri("https://example.com/foo").exchange().expectStatus().is3xxRedirection().returnResult(String.class);
// @formatter:on
}
Also used : FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) ClientAuthorizationRequiredException(org.springframework.security.oauth2.client.ClientAuthorizationRequiredException) Test(org.junit.jupiter.api.Test)

Example 3 with FilteringWebHandler

use of org.springframework.web.server.handler.FilteringWebHandler in project spring-security by spring-projects.

the class OAuth2AuthorizationRequestRedirectWebFilterTests method setup.

@BeforeEach
public void setup() {
    this.filter = new OAuth2AuthorizationRequestRedirectWebFilter(this.clientRepository);
    this.filter.setAuthorizationRequestRepository(this.authzRequestRepository);
    FilteringWebHandler webHandler = new FilteringWebHandler((e) -> e.getResponse().setComplete(), Arrays.asList(this.filter));
    this.client = WebTestClient.bindToWebHandler(webHandler).build();
}
Also used : FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with FilteringWebHandler

use of org.springframework.web.server.handler.FilteringWebHandler in project spring-framework by spring-projects.

the class WebHttpHandlerBuilder method build.

/**
 * Build the {@link HttpHandler}.
 */
public HttpHandler build() {
    WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
    decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers);
    HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
    if (this.sessionManager != null) {
        adapted.setSessionManager(this.sessionManager);
    }
    if (this.codecConfigurer != null) {
        adapted.setCodecConfigurer(this.codecConfigurer);
    }
    if (this.localeContextResolver != null) {
        adapted.setLocaleContextResolver(this.localeContextResolver);
    }
    if (this.forwardedHeaderTransformer != null) {
        adapted.setForwardedHeaderTransformer(this.forwardedHeaderTransformer);
    }
    if (this.applicationContext != null) {
        adapted.setApplicationContext(this.applicationContext);
    }
    adapted.afterPropertiesSet();
    return (this.httpHandlerDecorator != null ? this.httpHandlerDecorator.apply(adapted) : adapted);
}
Also used : ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) WebHandler(org.springframework.web.server.WebHandler) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler)

Example 5 with FilteringWebHandler

use of org.springframework.web.server.handler.FilteringWebHandler in project spring-boot by spring-projects.

the class HttpHandlerAutoConfigurationTests method shouldConfigureWebFiltersFunctional.

@Test
public void shouldConfigureWebFiltersFunctional() {
    load(FunctionalConfigWithWebFilters.class);
    assertThat(this.context.getBeansOfType(HttpHandler.class).size()).isEqualTo(1);
    HttpHandler handler = this.context.getBean(HttpHandler.class);
    assertThat(handler).isInstanceOf(WebHandler.class);
    WebHandler webHandler = (WebHandler) handler;
    while (webHandler instanceof WebHandlerDecorator) {
        if (webHandler instanceof FilteringWebHandler) {
            FilteringWebHandler filteringWebHandler = (FilteringWebHandler) webHandler;
            assertThat(filteringWebHandler.getFilters()).containsExactly(this.context.getBean("customWebFilter", WebFilter.class));
            return;
        }
        webHandler = ((WebHandlerDecorator) webHandler).getDelegate();
    }
    fail("Did not find any FilteringWebHandler");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) WebHandlerDecorator(org.springframework.web.server.handler.WebHandlerDecorator) WebHandler(org.springframework.web.server.WebHandler) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) Test(org.junit.Test)

Aggregations

FilteringWebHandler (org.springframework.web.server.handler.FilteringWebHandler)8 Test (org.junit.jupiter.api.Test)4 WebHandler (org.springframework.web.server.WebHandler)3 Test (org.junit.Test)2 HttpHandler (org.springframework.http.server.reactive.HttpHandler)2 ClientAuthorizationRequiredException (org.springframework.security.oauth2.client.ClientAuthorizationRequiredException)2 PathPatternParserServerWebExchangeMatcher (org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher)2 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)2 WebFilter (org.springframework.web.server.WebFilter)2 WebHandlerDecorator (org.springframework.web.server.handler.WebHandlerDecorator)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ExceptionHandlingWebHandler (org.springframework.web.server.handler.ExceptionHandlingWebHandler)1