Skip to content

Commit d2c72a9

Browse files
committed
closes gh-16170
Signed-off-by: douxiaofeng99 <18600127780@163.com>
1 parent 27cb115 commit d2c72a9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtEncoder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.nimbusds.jwt.JWTClaimsSet;
4747
import com.nimbusds.jwt.SignedJWT;
4848

49+
import org.springframework.core.convert.converter.Converter;
4950
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
5051
import org.springframework.util.Assert;
5152
import org.springframework.util.CollectionUtils;
@@ -86,6 +87,8 @@ public final class NimbusJwtEncoder implements JwtEncoder {
8687

8788
private final JWKSource<SecurityContext> jwkSource;
8889

90+
private Converter<List<JWK>, JWK> jwkSelector;
91+
8992
/**
9093
* Constructs a {@code NimbusJwtEncoder} using the provided parameters.
9194
* @param jwkSource the {@code com.nimbusds.jose.jwk.source.JWKSource}
@@ -95,6 +98,10 @@ public NimbusJwtEncoder(JWKSource<SecurityContext> jwkSource) {
9598
this.jwkSource = jwkSource;
9699
}
97100

101+
public void setJwkSelector(Converter<List<JWK>, JWK> jwkSelector) {
102+
this.jwkSelector = jwkSelector;
103+
}
104+
98105
@Override
99106
public Jwt encode(JwtEncoderParameters parameters) throws JwtEncodingException {
100107
Assert.notNull(parameters, "parameters cannot be null");
@@ -124,6 +131,10 @@ private JWK selectJwk(JwsHeader headers) {
124131
"Failed to select a JWK signing key -> " + ex.getMessage()), ex);
125132
}
126133

134+
if (null != this.jwkSelector) {
135+
return this.jwkSelector.convert(jwks);
136+
}
137+
127138
if (jwks.size() > 1) {
128139
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
129140
"Found multiple JWK signing keys for algorithm '" + headers.getAlgorithm().getName() + "'"));

0 commit comments

Comments
 (0)