Skip to content

Commit 25e6c51

Browse files
committed
Merge branch '6.0.x'
Closes spring-projectsgh-13254
2 parents d4c349d + 590e9e2 commit 25e6c51

File tree

4 files changed

+281
-78
lines changed

4 files changed

+281
-78
lines changed

test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

+118-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,10 +46,11 @@
4646
import org.springframework.security.core.userdetails.UserDetails;
4747
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
4848
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
49-
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
5049
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager;
5150
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
5251
import org.springframework.security.oauth2.client.registration.ClientRegistration;
52+
import org.springframework.security.oauth2.client.web.DefaultReactiveOAuth2AuthorizedClientManager;
53+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
5354
import org.springframework.security.oauth2.client.web.reactive.result.method.annotation.OAuth2AuthorizedClientArgumentResolver;
5455
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
5556
import org.springframework.security.oauth2.client.web.server.WebSessionServerOAuth2AuthorizedClientRepository;
@@ -214,8 +215,8 @@ public static OidcLoginMutator mockOidcLogin() {
214215
* tokens to be valid.
215216
*
216217
* <p>
217-
* The support works by associating the authorized client to the ServerWebExchange via
218-
* the {@link WebSessionServerOAuth2AuthorizedClientRepository}
218+
* The support works by associating the authorized client to the ServerWebExchange
219+
* using a {@link ServerOAuth2AuthorizedClientRepository}
219220
* </p>
220221
* @return the {@link OAuth2ClientMutator} to further configure or use
221222
* @since 5.3
@@ -230,8 +231,8 @@ public static OAuth2ClientMutator mockOAuth2Client() {
230231
* tokens to be valid.
231232
*
232233
* <p>
233-
* The support works by associating the authorized client to the ServerWebExchange via
234-
* the {@link WebSessionServerOAuth2AuthorizedClientRepository}
234+
* The support works by associating the authorized client to the ServerWebExchange
235+
* using a {@link ServerOAuth2AuthorizedClientRepository}
235236
* </p>
236237
* @param registrationId The registration id associated with the
237238
* {@link OAuth2AuthorizedClient}
@@ -715,8 +716,6 @@ public static final class OAuth2LoginMutator implements WebTestClientConfigurer,
715716

716717
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
717718

718-
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository = new WebSessionServerOAuth2AuthorizedClientRepository();
719-
720719
private OAuth2LoginMutator(OAuth2AccessToken accessToken) {
721720
this.accessToken = accessToken;
722721
this.clientRegistration = clientRegistrationBuilder().build();
@@ -776,12 +775,8 @@ public OAuth2LoginMutator oauth2User(OAuth2User oauth2User) {
776775
/**
777776
* Use the provided {@link ClientRegistration} as the client to authorize.
778777
* <p>
779-
* The supplied {@link ClientRegistration} will be registered into an
780-
* {@link WebSessionServerOAuth2AuthorizedClientRepository}. Tests relying on
781-
* {@link org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient}
782-
* annotations should register an
783-
* {@link WebSessionServerOAuth2AuthorizedClientRepository} bean to the
784-
* application context.
778+
* The supplied {@link ClientRegistration} will be registered into a
779+
* {@link ServerOAuth2AuthorizedClientRepository}.
785780
* @param clientRegistration the {@link ClientRegistration} to use
786781
* @return the {@link OAuth2LoginMutator} for further configuration
787782
*/
@@ -866,8 +861,6 @@ public static final class OidcLoginMutator implements WebTestClientConfigurer, M
866861

867862
private Collection<GrantedAuthority> authorities;
868863

869-
ServerOAuth2AuthorizedClientRepository authorizedClientRepository = new WebSessionServerOAuth2AuthorizedClientRepository();
870-
871864
private OidcLoginMutator(OAuth2AccessToken accessToken) {
872865
this.accessToken = accessToken;
873866
this.clientRegistration = clientRegistrationBuilder().build();
@@ -942,12 +935,8 @@ public OidcLoginMutator oidcUser(OidcUser oidcUser) {
942935
/**
943936
* Use the provided {@link ClientRegistration} as the client to authorize.
944937
* <p>
945-
* The supplied {@link ClientRegistration} will be registered into an
946-
* {@link WebSessionServerOAuth2AuthorizedClientRepository}. Tests relying on
947-
* {@link org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient}
948-
* annotations should register an
949-
* {@link WebSessionServerOAuth2AuthorizedClientRepository} bean to the
950-
* application context.
938+
* The supplied {@link ClientRegistration} will be registered into a
939+
* {@link ServerOAuth2AuthorizedClientRepository}.
951940
* @param clientRegistration the {@link ClientRegistration} to use
952941
* @return the {@link OidcLoginMutator} for further configuration
953942
*/
@@ -1037,8 +1026,6 @@ public static final class OAuth2ClientMutator implements WebTestClientConfigurer
10371026
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
10381027
"access-token", null, null, Collections.singleton("read"));
10391028

1040-
private ServerOAuth2AuthorizedClientRepository authorizedClientRepository = new WebSessionServerOAuth2AuthorizedClientRepository();
1041-
10421029
private OAuth2ClientMutator() {
10431030
}
10441031

@@ -1116,16 +1103,15 @@ public void afterConfigurerAdded(WebTestClient.Builder builder,
11161103
private Consumer<List<WebFilter>> addAuthorizedClientFilter() {
11171104
OAuth2AuthorizedClient client = getClient();
11181105
return (filters) -> filters.add(0, (exchange, chain) -> {
1119-
ReactiveOAuth2AuthorizedClientManager authorizationClientManager = OAuth2ClientServerTestUtils
1120-
.getOAuth2AuthorizedClientManager(exchange);
1121-
if (!(authorizationClientManager instanceof TestReactiveOAuth2AuthorizedClientManager)) {
1122-
authorizationClientManager = new TestReactiveOAuth2AuthorizedClientManager(
1123-
authorizationClientManager);
1124-
OAuth2ClientServerTestUtils.setOAuth2AuthorizedClientManager(exchange, authorizationClientManager);
1106+
ServerOAuth2AuthorizedClientRepository authorizedClientRepository = OAuth2ClientServerTestUtils
1107+
.getAuthorizedClientRepository(exchange);
1108+
if (!(authorizedClientRepository instanceof TestOAuth2AuthorizedClientRepository)) {
1109+
authorizedClientRepository = new TestOAuth2AuthorizedClientRepository(authorizedClientRepository);
1110+
OAuth2ClientServerTestUtils.setAuthorizedClientRepository(exchange, authorizedClientRepository);
11251111
}
1126-
TestReactiveOAuth2AuthorizedClientManager.enable(exchange);
1127-
exchange.getAttributes().put(TestReactiveOAuth2AuthorizedClientManager.TOKEN_ATTR_NAME, client);
1128-
return chain.filter(exchange);
1112+
TestOAuth2AuthorizedClientRepository.enable(exchange);
1113+
return authorizedClientRepository.saveAuthorizedClient(client, null, exchange)
1114+
.then(chain.filter(exchange));
11291115
});
11301116
}
11311117

@@ -1142,30 +1128,28 @@ private ClientRegistration.Builder clientRegistrationBuilder() {
11421128
}
11431129

11441130
/**
1145-
* Used to wrap the {@link OAuth2AuthorizedClientManager} to provide support for
1146-
* testing when the request is wrapped
1131+
* Used to wrap the {@link OAuth2AuthorizedClientRepository} to provide support
1132+
* for testing when the request is wrapped
11471133
*/
1148-
private static final class TestReactiveOAuth2AuthorizedClientManager
1149-
implements ReactiveOAuth2AuthorizedClientManager {
1134+
private static final class TestOAuth2AuthorizedClientManager implements ReactiveOAuth2AuthorizedClientManager {
11501135

1151-
static final String TOKEN_ATTR_NAME = TestReactiveOAuth2AuthorizedClientManager.class.getName()
1152-
.concat(".TOKEN");
1153-
1154-
static final String ENABLED_ATTR_NAME = TestReactiveOAuth2AuthorizedClientManager.class.getName()
1136+
static final String ENABLED_ATTR_NAME = TestOAuth2AuthorizedClientManager.class.getName()
11551137
.concat(".ENABLED");
11561138

11571139
private final ReactiveOAuth2AuthorizedClientManager delegate;
11581140

1159-
private TestReactiveOAuth2AuthorizedClientManager(ReactiveOAuth2AuthorizedClientManager delegate) {
1141+
private ServerOAuth2AuthorizedClientRepository authorizedClientRepository;
1142+
1143+
TestOAuth2AuthorizedClientManager(ReactiveOAuth2AuthorizedClientManager delegate) {
11601144
this.delegate = delegate;
11611145
}
11621146

11631147
@Override
11641148
public Mono<OAuth2AuthorizedClient> authorize(OAuth2AuthorizeRequest authorizeRequest) {
11651149
ServerWebExchange exchange = authorizeRequest.getAttribute(ServerWebExchange.class.getName());
11661150
if (isEnabled(exchange)) {
1167-
OAuth2AuthorizedClient client = exchange.getAttribute(TOKEN_ATTR_NAME);
1168-
return Mono.just(client);
1151+
return this.authorizedClientRepository.loadAuthorizedClient(
1152+
authorizeRequest.getClientRegistrationId(), authorizeRequest.getPrincipal(), exchange);
11691153
}
11701154
return this.delegate.authorize(authorizeRequest);
11711155
}
@@ -1180,6 +1164,62 @@ boolean isEnabled(ServerWebExchange exchange) {
11801164

11811165
}
11821166

1167+
/**
1168+
* Used to wrap the {@link OAuth2AuthorizedClientRepository} to provide support
1169+
* for testing when the request is wrapped
1170+
*/
1171+
static final class TestOAuth2AuthorizedClientRepository implements ServerOAuth2AuthorizedClientRepository {
1172+
1173+
static final String TOKEN_ATTR_NAME = TestOAuth2AuthorizedClientRepository.class.getName().concat(".TOKEN");
1174+
1175+
static final String ENABLED_ATTR_NAME = TestOAuth2AuthorizedClientRepository.class.getName()
1176+
.concat(".ENABLED");
1177+
1178+
private final ServerOAuth2AuthorizedClientRepository delegate;
1179+
1180+
TestOAuth2AuthorizedClientRepository(ServerOAuth2AuthorizedClientRepository delegate) {
1181+
this.delegate = delegate;
1182+
}
1183+
1184+
@Override
1185+
public <T extends OAuth2AuthorizedClient> Mono<T> loadAuthorizedClient(String clientRegistrationId,
1186+
Authentication principal, ServerWebExchange exchange) {
1187+
if (isEnabled(exchange)) {
1188+
return Mono.just(exchange.getAttribute(TOKEN_ATTR_NAME));
1189+
}
1190+
return this.delegate.loadAuthorizedClient(clientRegistrationId, principal, exchange);
1191+
}
1192+
1193+
@Override
1194+
public Mono<Void> saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal,
1195+
ServerWebExchange exchange) {
1196+
if (isEnabled(exchange)) {
1197+
exchange.getAttributes().put(TOKEN_ATTR_NAME, authorizedClient);
1198+
return Mono.empty();
1199+
}
1200+
return this.delegate.saveAuthorizedClient(authorizedClient, principal, exchange);
1201+
}
1202+
1203+
@Override
1204+
public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentication principal,
1205+
ServerWebExchange exchange) {
1206+
if (isEnabled(exchange)) {
1207+
exchange.getAttributes().remove(TOKEN_ATTR_NAME);
1208+
return Mono.empty();
1209+
}
1210+
return this.delegate.removeAuthorizedClient(clientRegistrationId, principal, exchange);
1211+
}
1212+
1213+
static void enable(ServerWebExchange exchange) {
1214+
exchange.getAttributes().put(ENABLED_ATTR_NAME, Boolean.TRUE);
1215+
}
1216+
1217+
boolean isEnabled(ServerWebExchange exchange) {
1218+
return Boolean.TRUE.equals(exchange.getAttribute(ENABLED_ATTR_NAME));
1219+
}
1220+
1221+
}
1222+
11831223
private static final class OAuth2ClientServerTestUtils {
11841224

11851225
private static final ServerOAuth2AuthorizedClientRepository DEFAULT_CLIENT_REPO = new WebSessionServerOAuth2AuthorizedClientRepository();
@@ -1188,14 +1228,47 @@ private OAuth2ClientServerTestUtils() {
11881228
}
11891229

11901230
/**
1191-
* Gets the {@link ReactiveOAuth2AuthorizedClientManager} for the specified
1231+
* Gets the {@link ServerOAuth2AuthorizedClientRepository} for the specified
11921232
* {@link ServerWebExchange}. If one is not found, one based off of
11931233
* {@link WebSessionServerOAuth2AuthorizedClientRepository} is used.
11941234
* @param exchange the {@link ServerWebExchange} to obtain the
11951235
* {@link ReactiveOAuth2AuthorizedClientManager}
11961236
* @return the {@link ReactiveOAuth2AuthorizedClientManager} for the specified
11971237
* {@link ServerWebExchange}
11981238
*/
1239+
static ServerOAuth2AuthorizedClientRepository getAuthorizedClientRepository(ServerWebExchange exchange) {
1240+
ReactiveOAuth2AuthorizedClientManager manager = getOAuth2AuthorizedClientManager(exchange);
1241+
if (manager == null) {
1242+
return DEFAULT_CLIENT_REPO;
1243+
}
1244+
if (manager instanceof DefaultReactiveOAuth2AuthorizedClientManager) {
1245+
return (ServerOAuth2AuthorizedClientRepository) ReflectionTestUtils.getField(manager,
1246+
"authorizedClientRepository");
1247+
}
1248+
if (manager instanceof TestOAuth2AuthorizedClientManager) {
1249+
return ((TestOAuth2AuthorizedClientManager) manager).authorizedClientRepository;
1250+
}
1251+
return DEFAULT_CLIENT_REPO;
1252+
}
1253+
1254+
static void setAuthorizedClientRepository(ServerWebExchange exchange,
1255+
ServerOAuth2AuthorizedClientRepository repository) {
1256+
ReactiveOAuth2AuthorizedClientManager manager = getOAuth2AuthorizedClientManager(exchange);
1257+
if (manager == null) {
1258+
return;
1259+
}
1260+
if (manager instanceof DefaultReactiveOAuth2AuthorizedClientManager) {
1261+
ReflectionTestUtils.setField(manager, "authorizedClientRepository", repository);
1262+
return;
1263+
}
1264+
if (!(manager instanceof TestOAuth2AuthorizedClientManager)) {
1265+
manager = new TestOAuth2AuthorizedClientManager(manager);
1266+
setOAuth2AuthorizedClientManager(exchange, manager);
1267+
}
1268+
TestOAuth2AuthorizedClientManager.enable(exchange);
1269+
((TestOAuth2AuthorizedClientManager) manager).authorizedClientRepository = repository;
1270+
}
1271+
11991272
static ReactiveOAuth2AuthorizedClientManager getOAuth2AuthorizedClientManager(ServerWebExchange exchange) {
12001273
OAuth2AuthorizedClientArgumentResolver resolver = findResolver(exchange,
12011274
OAuth2AuthorizedClientArgumentResolver.class);

0 commit comments

Comments
 (0)