按照文档 here和 there ,我设法设置了一个授权服务器,它发出用非对称 key 签名的 JWT 访问 token ,资源服务器使用公钥的本地副本在本地验证这些 token 。到目前为止一切顺利。
我的最终目标是让资源服务器使用授权服务器上的 JWKS 端点,并使用 JWT 中的“kid” header 在 JWKS 中查找正确的 key 并在本地验证,支持 key 轮换。 我找到了 how to make the Authorization Server expose a JWKS endpoint , 还有 how to specify the key-set-uri for the resource server .
不过好像也没有办法
- 在 JWKS 中发布 kid(key id)值
- 在 JWT 中包含 kid header
有办法吗?
最佳答案
我找到了一种在 jwks 端点中设置 child 的方法:
@FrameworkEndpoint
public class JwkSetEndpoint {
private final KeyPair keyPair;
public JwkSetEndpoint(KeyPair keyPair) {
this.keyPair = keyPair;
}
@GetMapping("/.well-known/jwks.json")
@ResponseBody
public Map<String, Object> getKey() {
RSAPublicKey publicKey = (RSAPublicKey) this.keyPair.getPublic();
RSAKey key = new RSAKey.Builder(publicKey)
.keyID("YOUR_KID_HERE")
.keyUse(KeyUse.SIGNATURE).build();
return new JWKSet(key).toJSONObject();
}
}
我没有找到在JWT header中设置它的方法。
关于jwt - 将 spring-security-oauth2 授权服务器与 kid 和 JWKS 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982192/