|
| 1 | +/* |
| 2 | + * Copyright 2026-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.reactive; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.context.annotation.Import; |
| 30 | +import org.springframework.security.authentication.ReactiveAuthenticationManager; |
| 31 | +import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver; |
| 32 | +import org.springframework.security.config.test.SpringTestContext; |
| 33 | +import org.springframework.security.config.test.SpringTestContextExtension; |
| 34 | +import org.springframework.security.config.users.ReactiveAuthenticationTestConfiguration; |
| 35 | +import org.springframework.security.config.web.server.ServerHttpSecurity; |
| 36 | +import org.springframework.security.core.Authentication; |
| 37 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 38 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 39 | +import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken; |
| 40 | +import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; |
| 41 | +import org.springframework.security.test.web.reactive.server.WebTestClientBuilder; |
| 42 | +import org.springframework.security.web.server.SecurityWebFilterChain; |
| 43 | +import org.springframework.security.web.server.WebFilterChainProxy; |
| 44 | +import org.springframework.security.web.server.WebFilterExchange; |
| 45 | +import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler; |
| 46 | +import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler; |
| 47 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 48 | +import org.springframework.test.web.reactive.server.FluxExchangeResult; |
| 49 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 50 | +import org.springframework.web.server.ServerWebExchange; |
| 51 | +import static org.mockito.ArgumentMatchers.*; |
| 52 | +import static org.mockito.Mockito.*; |
| 53 | + |
| 54 | +/** |
| 55 | + * @author Iain Henderson |
| 56 | + * @since 7.1 |
| 57 | + */ |
| 58 | +@ExtendWith({ SpringExtension.class, SpringTestContextExtension.class }) |
| 59 | +@SecurityTestExecutionListeners |
| 60 | +public class ReactiveOauth2ResourceServerTests { |
| 61 | + |
| 62 | + public final SpringTestContext spring = new SpringTestContext(this); |
| 63 | + |
| 64 | + private static final String authorizedToken = UUID.randomUUID().toString(); |
| 65 | + |
| 66 | + private static final String authorizationFailureToken = UUID.randomUUID().toString(); |
| 67 | + |
| 68 | + private static final String unauthorizedToken = UUID.randomUUID().toString(); |
| 69 | + |
| 70 | + @Autowired |
| 71 | + WebFilterChainProxy springSecurityFilterChain; |
| 72 | + |
| 73 | + @Autowired |
| 74 | + ServerAuthenticationFailureHandler failureHandler; |
| 75 | + |
| 76 | + @Autowired |
| 77 | + ServerAuthenticationSuccessHandler successHandler; |
| 78 | + |
| 79 | + @Mock |
| 80 | + Jwt jwt = mock(Jwt.class); |
| 81 | + |
| 82 | + @Test |
| 83 | + public void authenticate() { |
| 84 | + this.spring.register(ReactiveOauth2ResourceServerConfig.class).autowire(); |
| 85 | + |
| 86 | + // @formatter:off |
| 87 | + WebTestClient client = WebTestClientBuilder |
| 88 | + .bindToWebFilters(this.springSecurityFilterChain) |
| 89 | + .build(); |
| 90 | + FluxExchangeResult<String> result = client.get() |
| 91 | + .headers((headers) -> headers.setBearerAuth(authorizedToken)) |
| 92 | + .exchange() |
| 93 | + .expectStatus().isOk() |
| 94 | + .returnResult(String.class); |
| 95 | + // @formatter:on |
| 96 | + |
| 97 | + verify(failureHandler, never()).onAuthenticationFailure(any(), any()); |
| 98 | + verify(successHandler).onAuthenticationSuccess(any(), any()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void authenticateWithoutAuthorization() { |
| 103 | + this.spring.register(ReactiveOauth2ResourceServerConfig.class).autowire(); |
| 104 | + |
| 105 | + // @formatter:off |
| 106 | + WebTestClient client = WebTestClientBuilder |
| 107 | + .bindToWebFilters(this.springSecurityFilterChain) |
| 108 | + .build(); |
| 109 | + FluxExchangeResult<String> result = client.get() |
| 110 | + .headers((headers) -> headers.setBearerAuth(unauthorizedToken)) |
| 111 | + .exchange() |
| 112 | + .expectStatus().isUnauthorized() |
| 113 | + .returnResult(String.class); |
| 114 | + // @formatter:on |
| 115 | + |
| 116 | + verify(failureHandler, never()).onAuthenticationFailure(any(), any()); |
| 117 | + verify(successHandler).onAuthenticationSuccess(any(), any()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void authenticationFails() { |
| 122 | + this.spring.register(ReactiveOauth2ResourceServerConfig.class).autowire(); |
| 123 | + |
| 124 | + // @formatter:off |
| 125 | + WebTestClient client = WebTestClientBuilder |
| 126 | + .bindToWebFilters(this.springSecurityFilterChain) |
| 127 | + .build(); |
| 128 | + FluxExchangeResult<String> result = client.get() |
| 129 | + .headers((headers) -> headers.setBearerAuth(authorizationFailureToken)) |
| 130 | + .exchange() |
| 131 | + .expectStatus().isUnauthorized() |
| 132 | + .returnResult(String.class); |
| 133 | + // @formatter:on |
| 134 | + |
| 135 | + verify(failureHandler).onAuthenticationFailure(any(), any()); |
| 136 | + verify(successHandler, never()).onAuthenticationSuccess(any(), any()); |
| 137 | + } |
| 138 | + |
| 139 | + @Configuration |
| 140 | + @EnableWebFluxSecurity |
| 141 | + @Import(ReactiveAuthenticationTestConfiguration.class) |
| 142 | + static class ReactiveOauth2ResourceServerConfig { |
| 143 | + |
| 144 | + @Bean |
| 145 | + ServerAuthenticationFailureHandler failureHandler() { |
| 146 | + ServerAuthenticationFailureHandler failureHandler = mock(ServerAuthenticationFailureHandler.class); |
| 147 | + when(failureHandler.onAuthenticationFailure(any(), any())).thenAnswer(input -> { |
| 148 | + WebFilterExchange exchange = input.getArgument(0, WebFilterExchange.class); |
| 149 | + return exchange.getChain().filter(exchange.getExchange()); |
| 150 | + }); |
| 151 | + return failureHandler; |
| 152 | + } |
| 153 | + |
| 154 | + @Bean |
| 155 | + ServerAuthenticationSuccessHandler successHandler() { |
| 156 | + ServerAuthenticationSuccessHandler successHandler = mock(ServerAuthenticationSuccessHandler.class); |
| 157 | + when(successHandler.onAuthenticationSuccess(any(), any())).thenAnswer(input -> { |
| 158 | + WebFilterExchange webFilterExchange = input.getArgument(0, WebFilterExchange.class); |
| 159 | + Authentication authentication = input.getArgument(1, Authentication.class); |
| 160 | + if (authentication instanceof BearerTokenAuthenticationToken token) { |
| 161 | + token.setAuthenticated(token.getToken().equals(authorizedToken)); |
| 162 | + } |
| 163 | + return webFilterExchange.getChain().filter(webFilterExchange.getExchange()); |
| 164 | + }); |
| 165 | + return successHandler; |
| 166 | + } |
| 167 | + |
| 168 | + @Bean |
| 169 | + SecurityWebFilterChain httpSecurity(ServerHttpSecurity http, ServerAuthenticationFailureHandler failureHandler, |
| 170 | + ServerAuthenticationSuccessHandler successHandler) { |
| 171 | + // @formatter:off |
| 172 | + return http.authorizeExchange(authorize -> authorize.anyExchange().authenticated()) |
| 173 | + .oauth2ResourceServer(oauth2 -> oauth2 |
| 174 | + .authenticationManagerResolver(new Oauth2AutenticaitonManagerResolver()) |
| 175 | + .authenticationFailureHandler(failureHandler) |
| 176 | + .authenticationSuccessHandler(successHandler)) |
| 177 | + .build(); |
| 178 | + // @formatter:on |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + static class Oauth2AutenticaitonManagerResolver |
| 184 | + implements ReactiveAuthenticationManagerResolver<ServerWebExchange>, ReactiveAuthenticationManager { |
| 185 | + |
| 186 | + @Override |
| 187 | + public Mono<ReactiveAuthenticationManager> resolve(ServerWebExchange context) { |
| 188 | + return Mono.just(this::authenticate); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Mono<Authentication> authenticate(Authentication authentication) { |
| 193 | + if (authentication instanceof BearerTokenAuthenticationToken token) { |
| 194 | + if (token.getToken().equals(authorizationFailureToken)) { |
| 195 | + return Mono.error(new OAuth2AuthenticationException("401") { |
| 196 | + }); |
| 197 | + } |
| 198 | + return Mono.just(authentication); |
| 199 | + } |
| 200 | + return Mono.empty(); |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments