Search in sources :

Example 6 with WebFilter

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

the class ServerHttpSecurityTests method x509WhenDefaultsThenAddsX509Filter.

@Test
public void x509WhenDefaultsThenAddsX509Filter() {
    this.http.x509(withDefaults());
    SecurityWebFilterChain securityWebFilterChain = this.http.build();
    WebFilter x509WebFilter = securityWebFilterChain.getWebFilters().filter(this::isX509Filter).blockFirst();
    assertThat(x509WebFilter).isNotNull();
}
Also used : LogoutWebFilter(org.springframework.security.web.server.authentication.logout.LogoutWebFilter) WebFilter(org.springframework.web.server.WebFilter) SecurityContextServerWebExchangeWebFilter(org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter) OAuth2LoginAuthenticationWebFilter(org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter) CsrfWebFilter(org.springframework.security.web.server.csrf.CsrfWebFilter) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) Test(org.junit.jupiter.api.Test)

Example 7 with WebFilter

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

the class RouterFunctionsTests method toHttpHandlerWebFilter.

@Test
public void toHttpHandlerWebFilter() {
    AtomicBoolean filterInvoked = new AtomicBoolean();
    WebFilter webFilter = (exchange, chain) -> {
        filterInvoked.set(true);
        return chain.filter(exchange);
    };
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HandlerStrategies handlerStrategies = HandlerStrategies.builder().webFilter(webFilter).build();
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
    assertThat(filterInvoked.get()).isTrue();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 8 with WebFilter

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

the class ReactiveOAuth2ClientAutoConfigurationTests method hasFilter.

@SuppressWarnings("unchecked")
private boolean hasFilter(AssertableReactiveWebApplicationContext context, Class<? extends WebFilter> filter) {
    SecurityWebFilterChain filterChain = (SecurityWebFilterChain) context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
    List<WebFilter> filters = (List<WebFilter>) ReflectionTestUtils.getField(filterChain, "filters");
    return filters.stream().anyMatch(filter::isInstance);
}
Also used : WebFilter(org.springframework.web.server.WebFilter) OAuth2AuthorizationCodeGrantWebFilter(org.springframework.security.oauth2.client.web.server.OAuth2AuthorizationCodeGrantWebFilter) OAuth2LoginAuthenticationWebFilter(org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter) ArrayList(java.util.ArrayList) List(java.util.List) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain)

Example 9 with WebFilter

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

the class WebTestClientAutoConfigurationTests method shouldApplySpringSecurityConfigurer.

@Test
@SuppressWarnings("unchecked")
void shouldApplySpringSecurityConfigurer() {
    this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> {
        WebTestClient webTestClient = context.getBean(WebTestClient.class);
        WebTestClient.Builder builder = (WebTestClient.Builder) ReflectionTestUtils.getField(webTestClient, "builder");
        WebHttpHandlerBuilder httpHandlerBuilder = (WebHttpHandlerBuilder) ReflectionTestUtils.getField(builder, "httpHandlerBuilder");
        List<WebFilter> filters = (List<WebFilter>) ReflectionTestUtils.getField(httpHandlerBuilder, "filters");
        assertThat(filters.get(0).getClass().getName()).isEqualTo("org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers$MutatorFilter");
    });
}
Also used : WebFilter(org.springframework.web.server.WebFilter) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 10 with WebFilter

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

the class WebTestClientAutoConfigurationTests method shouldNotApplySpringSecurityConfigurerWhenSpringSecurityNotOnClassPath.

@Test
@SuppressWarnings("unchecked")
void shouldNotApplySpringSecurityConfigurerWhenSpringSecurityNotOnClassPath() {
    FilteredClassLoader classLoader = new FilteredClassLoader(SecurityMockServerConfigurers.class);
    this.contextRunner.withUserConfiguration(BaseConfiguration.class).withClassLoader(classLoader).run((context) -> {
        WebTestClient webTestClient = context.getBean(WebTestClient.class);
        WebTestClient.Builder builder = (WebTestClient.Builder) ReflectionTestUtils.getField(webTestClient, "builder");
        WebHttpHandlerBuilder httpHandlerBuilder = (WebHttpHandlerBuilder) ReflectionTestUtils.getField(builder, "httpHandlerBuilder");
        List<WebFilter> filters = (List<WebFilter>) ReflectionTestUtils.getField(httpHandlerBuilder, "filters");
        assertThat(filters).isEmpty();
    });
}
Also used : WebFilter(org.springframework.web.server.WebFilter) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) List(java.util.List) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Aggregations

WebFilter (org.springframework.web.server.WebFilter)18 Test (org.junit.jupiter.api.Test)15 SecurityWebFilterChain (org.springframework.security.web.server.SecurityWebFilterChain)10 List (java.util.List)7 OAuth2LoginAuthenticationWebFilter (org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter)7 LogoutWebFilter (org.springframework.security.web.server.authentication.logout.LogoutWebFilter)6 SecurityContextServerWebExchangeWebFilter (org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter)6 CsrfWebFilter (org.springframework.security.web.server.csrf.CsrfWebFilter)6 Mono (reactor.core.publisher.Mono)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 HttpStatus (org.springframework.http.HttpStatus)4 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Collections (java.util.Collections)3 Mockito.mock (org.mockito.Mockito.mock)3 FilteredClassLoader (org.springframework.boot.test.context.FilteredClassLoader)3 Bean (org.springframework.context.annotation.Bean)3 Configuration (org.springframework.context.annotation.Configuration)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 HttpHeaders (org.springframework.http.HttpHeaders)3