【问题标题】:Unable to validate signature of JWT token generated using .NetCore in a Java springboot application无法验证在 Java Spring Boot 应用程序中使用 .Net Core 生成的 JWT 令牌的签名
【发布时间】:2018-06-24 15:47:34
【问题描述】:

我们在 .NET Core 中生成了一个 Bearer JWT 令牌,并使用使用 Powershell 生成的 X509 自签名证书的 pfx 对令牌进行了签名。

我们需要在 Springboot Java 应用程序中验证令牌签名。 为此,我们使用 Java 中的密钥工具将 pfx 导入到 java 密钥库 (jks)。

keytool -importkeystore -srckeystore "certificate.pfx" -srcstoretype pkcs12 -destkeystore "clientcert.jks" -deststoretype JKS

我们正在一个资源服务器中验证令牌签名,我们在其中配置了一个 JwtAccessTokenConverter,如下所示。

JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
InputStream inJksStream = null;
InputStream inputJksStream = null;
inJksStream = new FileInputStream("clientcert.jks");
inputJksStream = new BufferedInputStream(inJksStream);
KeyStore trustStore  = KeyStore.getInstance("jks");         
trustStore.load(inputJksStream, "password".toCharArray());
Key trustStorePrivateKey = trustStore.getKey("alias-value", "password".toCharArray());
converter.setSigningKey(Base64.getEncoder().encodeToString(trustStorePrivateKey.getEncoded()));     

应用程序中的控制器用以下元素装饰。 @PreAuthorize("isAuthenticated()")

但是,当有效的 JWT 令牌与请求一起传递给 Springboot 应用程序时,我们会遇到签名无效异常。

这是在 Springboot 中验证 Bearer JWT 令牌的适当方法吗?

【问题讨论】:

    标签: java spring-boot .net-core jwt


    【解决方案1】:

    我刚刚在使用 IdentiyServer4 发布 JWT 时处理了类似的问题。如果我能看到您的错误输出,我可能会提供帮助。

    【讨论】:

    • 我在使用邮递员时收到带有 500 状态码的 unable verify the first certificate。在 chrome 中只有 500 个状态码,没有任何响应
    【解决方案2】:

    我可以建议使用 auth0 库来验证您的 JWT。我刚刚在一个项目中实现了这个,它比其他库更容易并且涉及的代码更少。

    maven: com.auth0 / java-jwt / 3.0.1

    可以在下面找到验证 JWT 的简单方法...

    public static DecodedJWT decodeJWT(String jwtString) {
        try {
            String secret = //get your secret key
            return JWT.require(Algorithm.HMAC256(secret))
                    .withIssuer("foo")
                    .build()
                    .verify(jwtString);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-04
      • 2017-10-03
      • 2020-02-12
      • 2021-11-16
      • 2021-07-03
      • 2020-10-24
      • 2021-12-07
      • 2021-06-27
      相关资源
      最近更新 更多