diff --git a/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java b/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java index 24bbaf8a28..299a82f9fb 100644 --- a/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java +++ b/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java @@ -701,12 +701,13 @@ public class SecurityMockServerConfigurers { * @since 5.3 */ public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer { + private final String nameAttributeKey = "sub"; + private ClientRegistration clientRegistration; private OAuth2AccessToken accessToken; private Supplier> authorities = this::defaultAuthorities; private Supplier> attributes = this::defaultAttributes; - private String nameAttributeKey = "sub"; private Supplier oauth2User = this::defaultPrincipal; private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository = @@ -752,28 +753,14 @@ public class SecurityMockServerConfigurers { public OAuth2LoginMutator attributes(Consumer> attributesConsumer) { Assert.notNull(attributesConsumer, "attributesConsumer cannot be null"); this.attributes = () -> { - Map attrs = new HashMap<>(); - attrs.put(this.nameAttributeKey, "test-subject"); - attributesConsumer.accept(attrs); - return attrs; + Map attributes = defaultAttributes(); + attributesConsumer.accept(attributes); + return attributes; }; this.oauth2User = this::defaultPrincipal; return this; } - /** - * Use the provided key for the attribute containing the principal's name - * - * @param nameAttributeKey The attribute key to use - * @return the {@link OAuth2LoginMutator} for further configuration - */ - public OAuth2LoginMutator nameAttributeKey(String nameAttributeKey) { - Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null"); - this.nameAttributeKey = nameAttributeKey; - this.oauth2User = this::defaultPrincipal; - return this; - } - /** * Use the provided {@link OAuth2User} as the authenticated user. * @@ -856,7 +843,9 @@ public class SecurityMockServerConfigurers { } private Map defaultAttributes() { - return Collections.singletonMap(this.nameAttributeKey, "test-subject"); + Map attributes = new HashMap<>(); + attributes.put(this.nameAttributeKey, "test-subject"); + return attributes; } private OAuth2User defaultPrincipal() { diff --git a/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java b/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java index 4e9887a930..ae6f1b7dfc 100644 --- a/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java +++ b/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java @@ -1319,12 +1319,13 @@ public final class SecurityMockMvcRequestPostProcessors { * @since 5.3 */ public final static class OAuth2LoginRequestPostProcessor implements RequestPostProcessor { + private final String nameAttributeKey = "sub"; + private ClientRegistration clientRegistration; private OAuth2AccessToken accessToken; private Supplier> authorities = this::defaultAuthorities; private Supplier> attributes = this::defaultAttributes; - private String nameAttributeKey = "sub"; private Supplier oauth2User = this::defaultPrincipal; private OAuth2LoginRequestPostProcessor(OAuth2AccessToken accessToken) { @@ -1367,28 +1368,14 @@ public final class SecurityMockMvcRequestPostProcessors { public OAuth2LoginRequestPostProcessor attributes(Consumer> attributesConsumer) { Assert.notNull(attributesConsumer, "attributesConsumer cannot be null"); this.attributes = () -> { - Map attrs = new HashMap<>(); - attrs.put(this.nameAttributeKey, "test-subject"); - attributesConsumer.accept(attrs); - return attrs; + Map attributes = defaultAttributes(); + attributesConsumer.accept(attributes); + return attributes; }; this.oauth2User = this::defaultPrincipal; return this; } - /** - * Use the provided key for the attribute containing the principal's name - * - * @param nameAttributeKey The attribute key to use - * @return the {@link OAuth2LoginRequestPostProcessor} for further configuration - */ - public OAuth2LoginRequestPostProcessor nameAttributeKey(String nameAttributeKey) { - Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null"); - this.nameAttributeKey = nameAttributeKey; - this.oauth2User = this::defaultPrincipal; - return this; - } - /** * Use the provided {@link OAuth2User} as the authenticated user. * @@ -1447,7 +1434,9 @@ public final class SecurityMockMvcRequestPostProcessors { } private Map defaultAttributes() { - return Collections.singletonMap(this.nameAttributeKey, "test-subject"); + Map attributes = new HashMap<>(); + attributes.put(this.nameAttributeKey, "test-subject"); + return attributes; } private OAuth2User defaultPrincipal() { diff --git a/test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java b/test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java index 2ba4cb600e..1937faf561 100644 --- a/test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java +++ b/test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java @@ -119,12 +119,16 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2LoginTests { @Test public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception { + OAuth2User oauth2User = new DefaultOAuth2User( + AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_user"), + Collections.singletonMap("custom-attribute", "test-subject"), + "custom-attribute"); this.mvc.perform(get("/attributes/custom-attribute") - .with(oauth2Login().nameAttributeKey("custom-attribute"))) + .with(oauth2Login().oauth2User(oauth2User))) .andExpect(content().string("test-subject")); this.mvc.perform(get("/name") - .with(oauth2Login().nameAttributeKey("custom-attribute"))) + .with(oauth2Login().oauth2User(oauth2User))) .andExpect(content().string("test-subject")); }