Search in sources :

Example 1 with AssertableReactiveWebApplicationContext

use of org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext in project spring-boot by spring-projects.

the class ReactiveManagementWebSecurityAutoConfigurationTests method performFilter.

private ServerWebExchange performFilter(AssertableReactiveWebApplicationContext context, String path) {
    ServerWebExchange exchange = webHandler(context).createExchange(MockServerHttpRequest.get(path).build(), new MockServerHttpResponse());
    WebFilterChainProxy proxy = context.getBean(WebFilterChainProxy.class);
    proxy.filter(exchange, (serverWebExchange) -> Mono.empty()).block(Duration.ofSeconds(30));
    return exchange;
}
Also used : ReactiveUserDetailsServiceAutoConfiguration(org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration) HttpWebHandlerAdapter(org.springframework.web.server.adapter.HttpWebHandlerAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EnvironmentEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointAutoConfiguration) WebHandler(org.springframework.web.server.WebHandler) ReactiveOAuth2ResourceServerAutoConfiguration(org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HealthEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) Duration(java.time.Duration) HealthContributorAutoConfiguration(org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration) WebEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration) ReactiveAuthenticationManager(org.springframework.security.authentication.ReactiveAuthenticationManager) URI(java.net.URI) AssertableReactiveWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext) InfoEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ReactiveSecurityAutoConfiguration(org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) Customizer(org.springframework.security.config.Customizer) Mono(reactor.core.publisher.Mono) EndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) WebFluxAutoConfiguration(org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration) Bean(org.springframework.context.annotation.Bean) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Collections(java.util.Collections) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Example 2 with AssertableReactiveWebApplicationContext

use of org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext in project spring-boot by spring-projects.

the class ReactiveOAuth2ResourceServerAutoConfigurationTests method assertFilterConfiguredWithJwtAuthenticationManager.

private void assertFilterConfiguredWithJwtAuthenticationManager(AssertableReactiveWebApplicationContext context) {
    MatcherSecurityWebFilterChain filterChain = (MatcherSecurityWebFilterChain) context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
    Stream<WebFilter> filters = filterChain.getWebFilters().toStream();
    AuthenticationWebFilter webFilter = (AuthenticationWebFilter) filters.filter((f) -> f instanceof AuthenticationWebFilter).findFirst().orElse(null);
    ReactiveAuthenticationManagerResolver<?> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<?>) ReflectionTestUtils.getField(webFilter, "authenticationManagerResolver");
    Object authenticationManager = authenticationManagerResolver.resolve(null).block();
    assertThat(authenticationManager).isInstanceOf(JwtReactiveAuthenticationManager.class);
}
Also used : AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) OAuth2TokenValidator(org.springframework.security.oauth2.core.OAuth2TokenValidator) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) JwtIssuerValidator(org.springframework.security.oauth2.jwt.JwtIssuerValidator) OpaqueTokenReactiveAuthenticationManager(org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenReactiveAuthenticationManager) EnableWebFluxSecurity(org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity) WebFilter(org.springframework.web.server.WebFilter) Map(java.util.Map) MatcherSecurityWebFilterChain(org.springframework.security.web.server.MatcherSecurityWebFilterChain) MockWebServer(okhttp3.mockwebserver.MockWebServer) NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) ReactiveAuthenticationManagerResolver(org.springframework.security.authentication.ReactiveAuthenticationManagerResolver) Jwt(org.springframework.security.oauth2.jwt.Jwt) ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) AssertableReactiveWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext) ReactiveOpaqueTokenIntrospector(org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector) DelegatingOAuth2TokenValidator(org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) JwtReactiveAuthenticationManager(org.springframework.security.oauth2.server.resource.authentication.JwtReactiveAuthenticationManager) HttpHeaders(org.springframework.http.HttpHeaders) BeanIds(org.springframework.security.config.BeanIds) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) MapReactiveUserDetailsService(org.springframework.security.core.userdetails.MapReactiveUserDetailsService) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Bean(org.springframework.context.annotation.Bean) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) MockResponse(okhttp3.mockwebserver.MockResponse) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) WebFilter(org.springframework.web.server.WebFilter) MatcherSecurityWebFilterChain(org.springframework.security.web.server.MatcherSecurityWebFilterChain) AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) ReactiveAuthenticationManagerResolver(org.springframework.security.authentication.ReactiveAuthenticationManagerResolver)

Example 3 with AssertableReactiveWebApplicationContext

use of org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext in project spring-boot by spring-projects.

the class ReactiveOAuth2ResourceServerAutoConfigurationTests method assertFilterConfiguredWithOpaqueTokenAuthenticationManager.

private void assertFilterConfiguredWithOpaqueTokenAuthenticationManager(AssertableReactiveWebApplicationContext context) {
    MatcherSecurityWebFilterChain filterChain = (MatcherSecurityWebFilterChain) context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
    Stream<WebFilter> filters = filterChain.getWebFilters().toStream();
    AuthenticationWebFilter webFilter = (AuthenticationWebFilter) filters.filter((f) -> f instanceof AuthenticationWebFilter).findFirst().orElse(null);
    ReactiveAuthenticationManagerResolver<?> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<?>) ReflectionTestUtils.getField(webFilter, "authenticationManagerResolver");
    Object authenticationManager = authenticationManagerResolver.resolve(null).block();
    assertThat(authenticationManager).isInstanceOf(OpaqueTokenReactiveAuthenticationManager.class);
}
Also used : AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) OAuth2TokenValidator(org.springframework.security.oauth2.core.OAuth2TokenValidator) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) JwtIssuerValidator(org.springframework.security.oauth2.jwt.JwtIssuerValidator) OpaqueTokenReactiveAuthenticationManager(org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenReactiveAuthenticationManager) EnableWebFluxSecurity(org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity) WebFilter(org.springframework.web.server.WebFilter) Map(java.util.Map) MatcherSecurityWebFilterChain(org.springframework.security.web.server.MatcherSecurityWebFilterChain) MockWebServer(okhttp3.mockwebserver.MockWebServer) NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) ReactiveAuthenticationManagerResolver(org.springframework.security.authentication.ReactiveAuthenticationManagerResolver) Jwt(org.springframework.security.oauth2.jwt.Jwt) ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) AssertableReactiveWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext) ReactiveOpaqueTokenIntrospector(org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector) DelegatingOAuth2TokenValidator(org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) JwtReactiveAuthenticationManager(org.springframework.security.oauth2.server.resource.authentication.JwtReactiveAuthenticationManager) HttpHeaders(org.springframework.http.HttpHeaders) BeanIds(org.springframework.security.config.BeanIds) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) MapReactiveUserDetailsService(org.springframework.security.core.userdetails.MapReactiveUserDetailsService) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Bean(org.springframework.context.annotation.Bean) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) MockResponse(okhttp3.mockwebserver.MockResponse) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) WebFilter(org.springframework.web.server.WebFilter) MatcherSecurityWebFilterChain(org.springframework.security.web.server.MatcherSecurityWebFilterChain) AuthenticationWebFilter(org.springframework.security.web.server.authentication.AuthenticationWebFilter) ReactiveAuthenticationManagerResolver(org.springframework.security.authentication.ReactiveAuthenticationManagerResolver)

Aggregations

Collections (java.util.Collections)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 Mockito.mock (org.mockito.Mockito.mock)3 AutoConfigurations (org.springframework.boot.autoconfigure.AutoConfigurations)3 AssertableReactiveWebApplicationContext (org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext)3 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)3 Bean (org.springframework.context.annotation.Bean)3 Configuration (org.springframework.context.annotation.Configuration)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ServerHttpSecurity (org.springframework.security.config.web.server.ServerHttpSecurity)3 SecurityWebFilterChain (org.springframework.security.web.server.SecurityWebFilterChain)3 Mono (reactor.core.publisher.Mono)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2