Skip to content

Commit dca5d90

Browse files
author
douxf
committed
closes gh-16170
Signed-off-by: douxiaofeng99 <[email protected]>
1 parent 27cb115 commit dca5d90

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

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

+9
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,9 @@ 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+
}
98104
@Override
99105
public Jwt encode(JwtEncoderParameters parameters) throws JwtEncodingException {
100106
Assert.notNull(parameters, "parameters cannot be null");
@@ -123,6 +129,9 @@ private JWK selectJwk(JwsHeader headers) {
123129
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
124130
"Failed to select a JWK signing key -> " + ex.getMessage()), ex);
125131
}
132+
if (null != this.jwkSelector) {
133+
return this.jwkSelector.convert(jwks);
134+
}
126135

127136
if (jwks.size() > 1) {
128137
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,

0 commit comments

Comments
 (0)