Search in sources :

Example 16 with WebFilter

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

the class WebFilterTests method testWebFilter.

@Test
public void testWebFilter() throws Exception {
    WebFilter filter = (exchange, chain) -> {
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
        buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
        return exchange.getResponse().writeWith(Mono.just(buffer));
    };
    WebTestClient client = WebTestClient.bindToWebHandler(exchange -> Mono.empty()).webFilter(filter).build();
    client.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("It works!");
}
Also used : Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) WebFilter(org.springframework.web.server.WebFilter) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) WebFilter(org.springframework.web.server.WebFilter) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with WebFilter

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

the class ReactorContextWebFilterTests method filterWhenMainContextThenDoesNotOverride.

@Test
public // gh-4962
void filterWhenMainContextThenDoesNotOverride() {
    given(this.repository.load(any())).willReturn(this.securityContext.mono());
    String contextKey = "main";
    WebFilter mainContextWebFilter = (e, c) -> c.filter(e).subscriberContext(Context.of(contextKey, true));
    WebFilterChain chain = new DefaultWebFilterChain((e) -> Mono.empty(), List.of(mainContextWebFilter, this.filter));
    Mono<Void> filter = chain.filter(MockServerWebExchange.from(this.exchange.build()));
    StepVerifier.create(filter).expectAccessibleContext().hasKey(contextKey).then().verifyComplete();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) WebTestHandler(org.springframework.security.test.web.reactive.server.WebTestHandler) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Mock(org.mockito.Mock) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SecurityContext(org.springframework.security.core.context.SecurityContext) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Authentication(org.springframework.security.core.Authentication) WebFilterChain(org.springframework.web.server.WebFilterChain) WebFilter(org.springframework.web.server.WebFilter) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) WebFilterChain(org.springframework.web.server.WebFilterChain) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) Test(org.junit.jupiter.api.Test)

Example 18 with WebFilter

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

the class WebFilterChainProxyTests method filterWhenNoMatchThenContinuesChainAnd404.

// gh-4668
@Test
public void filterWhenNoMatchThenContinuesChainAnd404() {
    List<WebFilter> filters = Arrays.asList(new Http200WebFilter());
    ServerWebExchangeMatcher notMatch = (exchange) -> MatchResult.notMatch();
    MatcherSecurityWebFilterChain chain = new MatcherSecurityWebFilterChain(notMatch, filters);
    WebFilterChainProxy filter = new WebFilterChainProxy(chain);
    WebTestClient.bindToController(new Object()).webFilter(filter).build().get().exchange().expectStatus().isNotFound();
}
Also used : Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) HttpStatus(org.springframework.http.HttpStatus) Arrays(java.util.Arrays) List(java.util.List) ServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher) WebFilter(org.springframework.web.server.WebFilter) Mono(reactor.core.publisher.Mono) MatchResult(org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher.MatchResult) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) WebFilter(org.springframework.web.server.WebFilter) ServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher) 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