【问题标题】:Faster way to authenticate Congnito JWT token using JAVA使用 JAVA 更快地验证 Cognito JWT 令牌的方法
【发布时间】:2019-07-04 22:43:29
【问题描述】:

我目前正在做以下事情

String region = "us-east-1";
String poolID = "us-east-1_whatever";

public Boolean valid() {
    try {
        String cognitoUrl = String.format("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, poolID);
        JwkProvider provider = new UrlJwkProvider(new URL(cognitoUrl));
        // got the value from the cognito url
        Jwk jwk = provider.get("value");
        RSAPublicKey publicKey = (RSAPublicKey) jwk.getPublicKey();
        Algorithm algorithm = Algorithm.RSA256(publicKey, null);
        String iss = String.format("https://cognito-idp.%s.amazonaws.com/%s", region, poolID);
        JWTVerifier verifier = JWT.require(algorithm).withIssuer(iss).build();
        verifier.verify(token);

    } catch (Exception err) {
        return false;
    }
    return true;
}

但它似乎很滞后。如何更快地验证令牌? 我正在使用

compile group: 'com.auth0', name: 'jwks-rsa', version: '0.3.0'
compile 'com.auth0:java-jwt:3.8.1'

【问题讨论】:

  • 是否有必要每次都从那个 url 获取 jwk?当签名验证失败时,如何将其缓存在本地并仅更新它?
  • @jps 这正是我想要的。我认为 auth0 库中有一些东西可以帮助我。
  • 我不习惯 jwk。我可以只复制这些值吗?他们不是经常变化吗?

标签: java performance jwt


【解决方案1】:

Amazon 建议每次都使用 Well Known JWKS URL,正如他们的 Support Repository 中所述。

不建议使用缓存,因为值会发生变化。因此,我建议继续您当前的方法,以确保具有最佳安全性的相同功能。

【讨论】:

  • Jwk jwk = provider.get("value") 但在这里我仍然在这里硬编码'kid'值。这是正确的方法吗?
猜你喜欢
  • 2018-03-07
  • 2020-10-27
  • 2022-12-15
  • 2016-12-13
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2019-07-29
相关资源
最近更新 更多