Skip to content

Commit

Permalink
closes gh-16170
Browse files Browse the repository at this point in the history
Signed-off-by: douxiaofeng99 <[email protected]>
  • Loading branch information
douxiaofeng99 committed Feb 11, 2025
1 parent 27cb115 commit d2c72a9
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;

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

private final JWKSource<SecurityContext> jwkSource;

private Converter<List<JWK>, JWK> jwkSelector;

/**
* Constructs a {@code NimbusJwtEncoder} using the provided parameters.
* @param jwkSource the {@code com.nimbusds.jose.jwk.source.JWKSource}
Expand All @@ -95,6 +98,10 @@ public NimbusJwtEncoder(JWKSource<SecurityContext> jwkSource) {
this.jwkSource = jwkSource;
}

public void setJwkSelector(Converter<List<JWK>, JWK> jwkSelector) {
this.jwkSelector = jwkSelector;
}

@Override
public Jwt encode(JwtEncoderParameters parameters) throws JwtEncodingException {
Assert.notNull(parameters, "parameters cannot be null");
Expand Down Expand Up @@ -124,6 +131,10 @@ private JWK selectJwk(JwsHeader headers) {
"Failed to select a JWK signing key -> " + ex.getMessage()), ex);
}

if (null != this.jwkSelector) {
return this.jwkSelector.convert(jwks);
}

if (jwks.size() > 1) {
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
"Found multiple JWK signing keys for algorithm '" + headers.getAlgorithm().getName() + "'"));
Expand Down

0 comments on commit d2c72a9

Please sign in to comment.