JWKSource supplies JSON Web Keys (JWKs) for cryptographic operations. In an authorization server, it can supply the keys used to sign JWTs. The server signs tokens with a private key; a resource server can obtain the public key to verify them. section 8.2
In the Taco Cloud authorization server, a JWKSource bean generates a 2048-bit RSA key pair and wraps it in a JWK set. Its selector returns matching keys from that set. Helper methods generate the pair and build the RSA key with a unique key ID. section 8.2
@Bean
public JWKSource<SecurityContext> jwkSource()
throws NoSuchAlgorithmException {
RSAKey rsaKey = generateRsa();
JWKSet jwkSet = new JWKSet(rsaKey);
return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
}
private static RSAKey generateRsa() throws NoSuchAlgorithmException {
KeyPair keyPair = generateRsaKey();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
return new RSAKey.Builder(publicKey)
.privateKey(privateKey)
.keyID(UUID.randomUUID().toString())
.build();
}
private static KeyPair generateRsaKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
@Bean
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
See the bean, key selector, and RSA key-generation helpers. section 8.2

