Skip to content

Commit e3249df

Browse files
committed
Improve authoritiesClaimName validation in JwtGrantedAuthoritiesConverter
Use StringUtils.hasText() instead of null check to properly handle empty strings and whitespace-only strings. Signed-off-by: chanbinme <[email protected]>
1 parent e1d8033 commit e3249df

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void setAuthoritiesClaimName(String authoritiesClaimName) {
106106
}
107107

108108
private String getAuthoritiesClaimName(Jwt jwt) {
109-
if (this.authoritiesClaimName != null) {
109+
if (StringUtils.hasText(this.authoritiesClaimName)) {
110110
return this.authoritiesClaimName;
111111
}
112112
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverterTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
import java.util.Collections;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.NullSource;
26+
import org.junit.jupiter.params.provider.ValueSource;
2427

2528
import org.springframework.security.core.GrantedAuthority;
2629
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2730
import org.springframework.security.oauth2.jwt.Jwt;
2831
import org.springframework.security.oauth2.jwt.TestJwts;
32+
import org.springframework.test.util.ReflectionTestUtils;
2933

3034
import static org.assertj.core.api.Assertions.assertThat;
3135
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -270,4 +274,21 @@ public void convertWithCustomAuthoritiesSplitRegexWhenTokenHasScopeAttributeThen
270274
new SimpleGrantedAuthority("SCOPE_message:write"));
271275
}
272276

277+
@ParameterizedTest
278+
@ValueSource(strings = { "", " " })
279+
@NullSource
280+
public void convertWhenAuthoritiesClaimNameIsBlankThenUsesWellKnownClaims(String invalidClaimName)
281+
throws Exception {
282+
// @formatter:off
283+
Jwt jwt = TestJwts.jwt()
284+
.claim("scope", "message:read message:write")
285+
.build();
286+
// @formatter:on
287+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
288+
ReflectionTestUtils.setField(jwtGrantedAuthoritiesConverter, "authoritiesClaimName", invalidClaimName);
289+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
290+
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"),
291+
new SimpleGrantedAuthority("SCOPE_message:write"));
292+
}
293+
273294
}

0 commit comments

Comments
 (0)