|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -45,10 +45,14 @@ public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Coll
|
45 | 45 |
|
46 | 46 | private static final String DEFAULT_AUTHORITY_PREFIX = "SCOPE_";
|
47 | 47 |
|
| 48 | + private static final String DEFAULT_AUTHORITIES_SPLIT_REGEX = " "; |
| 49 | + |
48 | 50 | private static final Collection<String> WELL_KNOWN_AUTHORITIES_CLAIM_NAMES = Arrays.asList("scope", "scp");
|
49 | 51 |
|
50 | 52 | private String authorityPrefix = DEFAULT_AUTHORITY_PREFIX;
|
51 | 53 |
|
| 54 | + private String authoritiesSplitRegex = DEFAULT_AUTHORITIES_SPLIT_REGEX; |
| 55 | + |
52 | 56 | private String authoritiesClaimName;
|
53 | 57 |
|
54 | 58 | /**
|
@@ -77,6 +81,18 @@ public void setAuthorityPrefix(String authorityPrefix) {
|
77 | 81 | this.authorityPrefix = authorityPrefix;
|
78 | 82 | }
|
79 | 83 |
|
| 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 | + |
80 | 96 | /**
|
81 | 97 | * Sets the name of token claim to use for mapping {@link GrantedAuthority
|
82 | 98 | * authorities} by this converter. Defaults to
|
@@ -113,7 +129,7 @@ private Collection<String> getAuthorities(Jwt jwt) {
|
113 | 129 | Object authorities = jwt.getClaim(claimName);
|
114 | 130 | if (authorities instanceof String) {
|
115 | 131 | if (StringUtils.hasText((String) authorities)) {
|
116 |
| - return Arrays.asList(((String) authorities).split(" ")); |
| 132 | + return Arrays.asList(((String) authorities).split(this.authoritiesSplitRegex)); |
117 | 133 | }
|
118 | 134 | return Collections.emptyList();
|
119 | 135 | }
|
|
0 commit comments