4646import com .nimbusds .jwt .JWTClaimsSet ;
4747import com .nimbusds .jwt .SignedJWT ;
4848
49+ import org .springframework .core .convert .converter .Converter ;
4950import org .springframework .security .oauth2 .jose .jws .SignatureAlgorithm ;
5051import org .springframework .util .Assert ;
5152import 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