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,10 @@ 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
+ }
104
+
98
105
@ Override
99
106
public Jwt encode (JwtEncoderParameters parameters ) throws JwtEncodingException {
100
107
Assert .notNull (parameters , "parameters cannot be null" );
@@ -124,6 +131,10 @@ private JWK selectJwk(JwsHeader headers) {
124
131
"Failed to select a JWK signing key -> " + ex .getMessage ()), ex );
125
132
}
126
133
134
+ if (null != this .jwkSelector ) {
135
+ return this .jwkSelector .convert (jwks );
136
+ }
137
+
127
138
if (jwks .size () > 1 ) {
128
139
throw new JwtEncodingException (String .format (ENCODING_ERROR_MESSAGE_TEMPLATE ,
129
140
"Found multiple JWK signing keys for algorithm '" + headers .getAlgorithm ().getName () + "'" ));
0 commit comments