Skip to content

Commit 9c9fd9f

Browse files
PatrickWalter214jzheaux
authored andcommitted
Add configurable authorities split regex
Before this commit splitting the authorities claim was done by a hardcoded regex " ". This commit allows to configure to set any regex to split the authorities claim while keeping the previously hardcoded regex as a default. Closes gh-12074
1 parent 67cde2d commit 9c9fd9f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -45,10 +45,14 @@ public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Coll
4545

4646
private static final String DEFAULT_AUTHORITY_PREFIX = "SCOPE_";
4747

48+
private static final String DEFAULT_AUTHORITIES_SPLIT_REGEX = " ";
49+
4850
private static final Collection<String> WELL_KNOWN_AUTHORITIES_CLAIM_NAMES = Arrays.asList("scope", "scp");
4951

5052
private String authorityPrefix = DEFAULT_AUTHORITY_PREFIX;
5153

54+
private String authoritiesSplitRegex = DEFAULT_AUTHORITIES_SPLIT_REGEX;
55+
5256
private String authoritiesClaimName;
5357

5458
/**
@@ -77,6 +81,18 @@ public void setAuthorityPrefix(String authorityPrefix) {
7781
this.authorityPrefix = authorityPrefix;
7882
}
7983

84+
/**
85+
* Sets the regex to use for splitting the value of the authorities claim into
86+
* {@link GrantedAuthority authorities}. Defaults to
87+
* {@link JwtGrantedAuthoritiesConverter#DEFAULT_AUTHORITIES_SPLIT_REGEX}.
88+
* @param authoritiesSplitRegex The regex used to split the authorities
89+
* @since 6.1
90+
*/
91+
public void setAuthoritiesSplitRegex(String authoritiesSplitRegex) {
92+
Assert.notNull(authoritiesSplitRegex, "authoritiesSplitRegex cannot be null");
93+
this.authoritiesSplitRegex = authoritiesSplitRegex;
94+
}
95+
8096
/**
8197
* Sets the name of token claim to use for mapping {@link GrantedAuthority
8298
* authorities} by this converter. Defaults to
@@ -113,7 +129,7 @@ private Collection<String> getAuthorities(Jwt jwt) {
113129
Object authorities = jwt.getClaim(claimName);
114130
if (authorities instanceof String) {
115131
if (StringUtils.hasText((String) authorities)) {
116-
return Arrays.asList(((String) authorities).split(" "));
132+
return Arrays.asList(((String) authorities).split(this.authoritiesSplitRegex));
117133
}
118134
return Collections.emptyList();
119135
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -256,4 +256,18 @@ public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTr
256256
assertThat(authorities).isEmpty();
257257
}
258258

259+
@Test
260+
public void convertWithCustomAuthoritiesSplitRegexWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
261+
// @formatter:off
262+
Jwt jwt = TestJwts.jwt()
263+
.claim("scope", "message:read,message:write")
264+
.build();
265+
// @formatter:on
266+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
267+
jwtGrantedAuthoritiesConverter.setAuthoritiesSplitRegex(",");
268+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
269+
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"),
270+
new SimpleGrantedAuthority("SCOPE_message:write"));
271+
}
272+
259273
}

0 commit comments

Comments
 (0)