From 39a94923e52663e01e5ecb888e3f5ce65bcce909 Mon Sep 17 00:00:00 2001 From: jyx-07 Date: Mon, 24 Aug 2026 23:27:43 +0900 Subject: [PATCH] Add regression test for FactorGrantedAuthority Jackson support Add a test to OAuth2AuthorizationServerJackson2ModuleTests and its Jackson 3 counterpart that round-trips an OAuth2Authorization's Principal attribute when the principal's authorities include a FactorGrantedAuthority, using the same ObjectMapper/JsonMapper setup JdbcOAuth2AuthorizationService builds internally. FactorGrantedAuthorityMixin already exists and is registered in CoreJackson2Module/CoreJacksonModule (added together with FactorGrantedAuthority itself), so this round trip already succeeds on main. This test only locks in that existing, previously uncovered behavior so a future regression is caught before release. Closes gh-18771 Signed-off-by: jyx-07 --- ...uth2AuthorizationServerJacksonModuleTests.java | 15 +++++++++++++++ ...th2AuthorizationServerJackson2ModuleTests.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson/OAuth2AuthorizationServerJacksonModuleTests.java b/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson/OAuth2AuthorizationServerJacksonModuleTests.java index baa8b47cd4a..fba1942a8ba 100644 --- a/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson/OAuth2AuthorizationServerJacksonModuleTests.java +++ b/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson/OAuth2AuthorizationServerJacksonModuleTests.java @@ -27,6 +27,8 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.jackson.SecurityJacksonModules; import org.springframework.security.oauth2.jose.jws.MacAlgorithm; import org.springframework.security.oauth2.jwt.JwtClaimNames; @@ -71,6 +73,19 @@ public void readValueWhenOAuth2AuthorizationAttributesThenSuccess() { assertThat(this.mapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes); } + @Test + public void readValueWhenOAuth2AuthorizationAttributesWithFactorGrantedAuthorityThenSuccess() { + Authentication principal = UsernamePasswordAuthenticationToken.authenticated("principal", "credentials", + List.of(new SimpleGrantedAuthority("ROLE_USER"), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY))); + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization() + .attributes((attrs) -> attrs.put(Principal.class.getName(), principal)) + .build(); + Map attributes = authorization.getAttributes(); + String json = this.mapper.writeValueAsString(attributes); + assertThat(this.mapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes); + } + @Test public void readValueWhenOAuth2AccessTokenMetadataThenSuccess() { OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build(); diff --git a/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java b/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java index f06d3f81f1f..b25b4127d74 100644 --- a/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java +++ b/oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java @@ -28,6 +28,8 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.jackson2.SecurityJackson2Modules; import org.springframework.security.oauth2.jose.jws.MacAlgorithm; import org.springframework.security.oauth2.jwt.JwtClaimNames; @@ -74,6 +76,19 @@ public void readValueWhenOAuth2AuthorizationAttributesThenSuccess() throws Excep assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes); } + @Test + public void readValueWhenOAuth2AuthorizationAttributesWithFactorGrantedAuthorityThenSuccess() throws Exception { + Authentication principal = UsernamePasswordAuthenticationToken.authenticated("principal", "credentials", + List.of(new SimpleGrantedAuthority("ROLE_USER"), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY))); + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization() + .attributes((attrs) -> attrs.put(Principal.class.getName(), principal)) + .build(); + Map attributes = authorization.getAttributes(); + String json = this.objectMapper.writeValueAsString(attributes); + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes); + } + @Test public void readValueWhenOAuth2AccessTokenMetadataThenSuccess() throws Exception { OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build();