46
46
import com .nimbusds .jwt .JWTClaimsSet ;
47
47
import com .nimbusds .jwt .SignedJWT ;
48
48
49
+ import org .springframework .core .convert .converter .Converter ;
49
50
import org .springframework .security .oauth2 .jose .jws .SignatureAlgorithm ;
50
51
import org .springframework .util .Assert ;
51
52
import org .springframework .util .CollectionUtils ;
@@ -86,6 +87,8 @@ public final class NimbusJwtEncoder implements JwtEncoder {
86
87
87
88
private final JWKSource <SecurityContext > jwkSource ;
88
89
90
+ private Converter <List <JWK >, JWK > jwkSelector ;
91
+
89
92
/**
90
93
* Constructs a {@code NimbusJwtEncoder} using the provided parameters.
91
94
* @param jwkSource the {@code com.nimbusds.jose.jwk.source.JWKSource}
@@ -95,6 +98,9 @@ public NimbusJwtEncoder(JWKSource<SecurityContext> jwkSource) {
95
98
this .jwkSource = jwkSource ;
96
99
}
97
100
101
+ public void setJwkSelector (Converter <List <JWK >, JWK > jwkSelector ) {
102
+ this .jwkSelector = jwkSelector ;
103
+ }
98
104
@ Override
99
105
public Jwt encode (JwtEncoderParameters parameters ) throws JwtEncodingException {
100
106
Assert .notNull (parameters , "parameters cannot be null" );
@@ -123,6 +129,9 @@ private JWK selectJwk(JwsHeader headers) {
123
129
throw new JwtEncodingException (String .format (ENCODING_ERROR_MESSAGE_TEMPLATE ,
124
130
"Failed to select a JWK signing key -> " + ex .getMessage ()), ex );
125
131
}
132
+ if (null != this .jwkSelector ) {
133
+ return this .jwkSelector .convert (jwks );
134
+ }
126
135
127
136
if (jwks .size () > 1 ) {
128
137
throw new JwtEncodingException (String .format (ENCODING_ERROR_MESSAGE_TEMPLATE ,
0 commit comments