Search in sources :

Example 1 with PathPatternParserServerWebExchangeMatcher

use of org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher in project spring-security by spring-projects.

the class FormLoginTests method formLoginWhenCustomRequiresAuthenticationMatcherThenUsed.

@Test
public void formLoginWhenCustomRequiresAuthenticationMatcherThenUsed() {
    // @formatter:off
    SecurityWebFilterChain securityWebFilter = this.http.authorizeExchange().pathMatchers("/login", "/sign-in").permitAll().anyExchange().authenticated().and().formLogin().requiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/sign-in")).and().build();
    WebTestClient webTestClient = WebTestClientBuilder.bindToWebFilters(securityWebFilter).build();
    WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();
    // @formatter:on
    driver.get("http://localhost/sign-in");
    assertThat(driver.getCurrentUrl()).endsWith("/login?error");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) Test(org.junit.jupiter.api.Test)

Example 2 with PathPatternParserServerWebExchangeMatcher

use of org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher in project spring-security by spring-projects.

the class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolverTests method resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager.

@Test
public void resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager() {
    ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver resolver = ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.builder().add(new PathPatternParserServerWebExchangeMatcher("/one/**"), this.one).add(new PathPatternParserServerWebExchangeMatcher("/two/**"), this.two).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/wrong/location").build();
    ReactiveAuthenticationManager authenticationManager = resolver.resolve(MockServerWebExchange.from(request)).block();
    Authentication authentication = new TestingAuthenticationToken("principal", "creds");
    assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> authenticationManager.authenticate(authentication).block());
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) ReactiveAuthenticationManager(org.springframework.security.authentication.ReactiveAuthenticationManager) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) Test(org.junit.jupiter.api.Test)

Example 3 with PathPatternParserServerWebExchangeMatcher

use of org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher in project spring-security by spring-projects.

the class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolverTests method resolveWhenMatchesThenReturnsReactiveAuthenticationManager.

@Test
public void resolveWhenMatchesThenReturnsReactiveAuthenticationManager() {
    ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver resolver = ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.builder().add(new PathPatternParserServerWebExchangeMatcher("/one/**"), this.one).add(new PathPatternParserServerWebExchangeMatcher("/two/**"), this.two).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/one/location").build();
    assertThat(resolver.resolve(MockServerWebExchange.from(request)).block()).isEqualTo(this.one);
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with PathPatternParserServerWebExchangeMatcher

use of org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher in project spring-security by spring-projects.

the class ExchangeMatcherRedirectWebFilterTests method filterWhenRequestMatchThenRedirectToSpecifiedUrl.

@Test
public void filterWhenRequestMatchThenRedirectToSpecifiedUrl() {
    ExchangeMatcherRedirectWebFilter filter = new ExchangeMatcherRedirectWebFilter(new PathPatternParserServerWebExchangeMatcher("/context"), "/test");
    FilteringWebHandler handler = new FilteringWebHandler((e) -> e.getResponse().setComplete(), Collections.singletonList(filter));
    WebTestClient client = WebTestClient.bindToWebHandler(handler).build();
    client.get().uri("/context").exchange().expectStatus().isFound().expectHeader().valueEquals(HttpHeaders.LOCATION, "/test");
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) Test(org.junit.jupiter.api.Test)

Example 5 with PathPatternParserServerWebExchangeMatcher

use of org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher in project spring-security by spring-projects.

the class ExchangeMatcherRedirectWebFilterTests method filterWhenRequestNotMatchThenNextFilter.

@Test
public void filterWhenRequestNotMatchThenNextFilter() {
    ExchangeMatcherRedirectWebFilter filter = new ExchangeMatcherRedirectWebFilter(new PathPatternParserServerWebExchangeMatcher("/context"), "/test");
    FilteringWebHandler handler = new FilteringWebHandler((e) -> e.getResponse().setComplete(), Collections.singletonList(filter));
    WebTestClient client = WebTestClient.bindToWebHandler(handler).build();
    client.get().uri("/test").exchange().expectStatus().isOk();
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) FilteringWebHandler(org.springframework.web.server.handler.FilteringWebHandler) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 PathPatternParserServerWebExchangeMatcher (org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher)5 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)3 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)2 FilteringWebHandler (org.springframework.web.server.handler.FilteringWebHandler)2 WebDriver (org.openqa.selenium.WebDriver)1 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)1 ReactiveAuthenticationManager (org.springframework.security.authentication.ReactiveAuthenticationManager)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 SecurityWebFilterChain (org.springframework.security.web.server.SecurityWebFilterChain)1