liveBook · Manning

JWKSource

As explained in Spring in Action, Sixth Edition

JWKSource is an API abstraction that supplies JSON Web Keys for cryptographic operations.

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

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows bean and RSA key-generation helpers, while deferring further resource-server verification details.
Examples
Uses a Taco Cloud authorization server bean that selects keys from a JWK set.
What you can do after
Apply it via Spring Security to configure an RSA-backed signing key source.
How it connects
Connects JSON Web Keys, RSA key pairs, JWT signatures, and resource server verification.

Also explained in

Compare how 2 books explain JWKSource →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.