Search in sources :

Example 51 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.

the class EnableWebFluxSecurityTests method formLoginWorks.

@Test
public void formLoginWorks() {
    this.spring.register(Config.class).autowire();
    // @formatter:off
    WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain, writePrincipalWebFilter()).build();
    // @formatter:on
    MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
    data.add("username", "user");
    data.add("password", "password");
    // @formatter:off
    client.mutateWith(csrf()).post().uri("/login").body(BodyInserters.fromFormData(data)).exchange().expectStatus().is3xxRedirection().expectHeader().valueMatches("Location", "/");
// @formatter:on
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 52 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.

the class EnableWebFluxSecurityTests method passwordEncoderBeanIsUsed.

@Test
public void passwordEncoderBeanIsUsed() {
    this.spring.register(CustomPasswordEncoderConfig.class).autowire();
    // @formatter:off
    WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain, writePrincipalWebFilter()).build();
    client.get().uri("/").headers((headers) -> headers.setBasicAuth("user", "password")).exchange().expectStatus().isOk().expectBody(String.class).consumeWith((result) -> assertThat(result.getResponseBody()).isEqualTo("user"));
// @formatter:on
}
Also used : BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticationPrincipalArgumentResolver(org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) User(org.springframework.security.core.userdetails.User) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ReactiveUserDetailsService(org.springframework.security.core.userdetails.ReactiveUserDetailsService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) WebFilter(org.springframework.web.server.WebFilter) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebSessionServerSecurityContextRepository(org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository) ReactiveAuthenticationTestConfiguration(org.springframework.security.config.users.ReactiveAuthenticationTestConfiguration) MediaType(org.springframework.http.MediaType) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) SecurityContext(org.springframework.security.core.context.SecurityContext) WithMockUser(org.springframework.security.test.context.support.WithMockUser) BodyInserters(org.springframework.web.reactive.function.BodyInserters) Authentication(org.springframework.security.core.Authentication) CsrfRequestDataValueProcessor(org.springframework.security.web.reactive.result.view.CsrfRequestDataValueProcessor) Ordered(org.springframework.core.Ordered) SecurityMockServerConfigurers.csrf(org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) UserDetails(org.springframework.security.core.userdetails.UserDetails) GetMapping(org.springframework.web.bind.annotation.GetMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebTestClientBuilder(org.springframework.security.test.web.reactive.server.WebTestClientBuilder) MockServletContext(org.springframework.mock.web.MockServletContext) Order(org.springframework.core.annotation.Order) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityTestExecutionListeners(org.springframework.security.test.context.annotation.SecurityTestExecutionListeners) Import(org.springframework.context.annotation.Import) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) DelegatingWebFluxConfiguration(org.springframework.web.reactive.config.DelegatingWebFluxConfiguration) FluxExchangeResult(org.springframework.test.web.reactive.server.FluxExchangeResult) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) MapReactiveUserDetailsService(org.springframework.security.core.userdetails.MapReactiveUserDetailsService) SpringTestContext(org.springframework.security.config.test.SpringTestContext) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) AbstractView(org.springframework.web.reactive.result.view.AbstractView) Bean(org.springframework.context.annotation.Bean) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 53 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.

the class EnableWebFluxSecurityTests method authenticationPrincipalArgumentResolverWhenSpelThenWorks.

@Test
@WithMockUser
public void authenticationPrincipalArgumentResolverWhenSpelThenWorks() {
    this.spring.register(AuthenticationPrincipalConfig.class).autowire();
    // @formatter:off
    WebTestClient client = WebTestClient.bindToApplicationContext(this.spring.getContext()).build();
    client.get().uri("/spel").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("user");
// @formatter:on
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 54 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.

the class EnableWebFluxSecurityTests method passwordUpdateManagerUsed.

@Test
public void passwordUpdateManagerUsed() {
    this.spring.register(MapReactiveUserDetailsServiceConfig.class).autowire();
    // @formatter:off
    WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build();
    client.get().uri("/").headers((h) -> h.setBasicAuth("user", "password")).exchange().expectStatus().isOk();
    // @formatter:on
    ReactiveUserDetailsService users = this.spring.getContext().getBean(ReactiveUserDetailsService.class);
    assertThat(users.findByUsername("user").block().getPassword()).startsWith("{bcrypt}");
}
Also used : BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticationPrincipalArgumentResolver(org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) User(org.springframework.security.core.userdetails.User) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ReactiveUserDetailsService(org.springframework.security.core.userdetails.ReactiveUserDetailsService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) WebFilter(org.springframework.web.server.WebFilter) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebSessionServerSecurityContextRepository(org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository) ReactiveAuthenticationTestConfiguration(org.springframework.security.config.users.ReactiveAuthenticationTestConfiguration) MediaType(org.springframework.http.MediaType) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) SecurityContext(org.springframework.security.core.context.SecurityContext) WithMockUser(org.springframework.security.test.context.support.WithMockUser) BodyInserters(org.springframework.web.reactive.function.BodyInserters) Authentication(org.springframework.security.core.Authentication) CsrfRequestDataValueProcessor(org.springframework.security.web.reactive.result.view.CsrfRequestDataValueProcessor) Ordered(org.springframework.core.Ordered) SecurityMockServerConfigurers.csrf(org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) UserDetails(org.springframework.security.core.userdetails.UserDetails) GetMapping(org.springframework.web.bind.annotation.GetMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebTestClientBuilder(org.springframework.security.test.web.reactive.server.WebTestClientBuilder) MockServletContext(org.springframework.mock.web.MockServletContext) Order(org.springframework.core.annotation.Order) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityTestExecutionListeners(org.springframework.security.test.context.annotation.SecurityTestExecutionListeners) Import(org.springframework.context.annotation.Import) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) DelegatingWebFluxConfiguration(org.springframework.web.reactive.config.DelegatingWebFluxConfiguration) FluxExchangeResult(org.springframework.test.web.reactive.server.FluxExchangeResult) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) MapReactiveUserDetailsService(org.springframework.security.core.userdetails.MapReactiveUserDetailsService) SpringTestContext(org.springframework.security.config.test.SpringTestContext) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) AbstractView(org.springframework.web.reactive.result.view.AbstractView) Bean(org.springframework.context.annotation.Bean) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) ReactiveUserDetailsService(org.springframework.security.core.userdetails.ReactiveUserDetailsService) MapReactiveUserDetailsService(org.springframework.security.core.userdetails.MapReactiveUserDetailsService) Test(org.junit.jupiter.api.Test)

Example 55 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project pact-jvm by DiUS.

the class WebTestClientPactTest method setup.

@BeforeEach
void setup(PactVerificationContext context) {
    Handler handler = new Handler();
    WebTestClient webTestClient = WebTestClient.bindToRouterFunction(new Router().route(handler)).build();
    context.setTarget(new WebTestClientTarget(webTestClient));
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)165 Test (org.junit.jupiter.api.Test)159 SecurityWebFilterChain (org.springframework.security.web.server.SecurityWebFilterChain)44 WebTestClientBuilder (org.springframework.security.test.web.reactive.server.WebTestClientBuilder)34 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)31 Authentication (org.springframework.security.core.Authentication)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 WebDriver (org.openqa.selenium.WebDriver)26 Mono (reactor.core.publisher.Mono)25 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)23 ServerSecurityContextRepository (org.springframework.security.web.server.context.ServerSecurityContextRepository)22 ReactiveAuthenticationManager (org.springframework.security.authentication.ReactiveAuthenticationManager)21 WebFilterChainProxy (org.springframework.security.web.server.WebFilterChainProxy)21 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)20 BDDMockito.given (org.mockito.BDDMockito.given)20 Mockito.verify (org.mockito.Mockito.verify)20 GetMapping (org.springframework.web.bind.annotation.GetMapping)19 WebFilter (org.springframework.web.server.WebFilter)18 SecurityContext (org.springframework.security.core.context.SecurityContext)17 RestController (org.springframework.web.bind.annotation.RestController)17