使用 com.nimbusds.jose 和来自 Azure AD 的公共证书 url 验证 JWT 令牌

问题描述 投票:0回答:0

我正在使用 com.nimbusds.jose 实现,以便通过检索用于从 Microsoft 授权服务器配置(属性 jwks_uri)签署 JWT 的公钥来验证令牌。当我尝试使用下面编写的代码验证令牌时,我得到:

com.nimbusds.jose.RemoteKeySourceException:无法检索远程 JWK 集:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径。

由以下原因引起:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

代码:

 try {
        ConfigurableJWTProcessor jwtProcessor = new DefaultJWTProcessor();
        JWKSource keySource = null;
        try {
          ResourceRetriever jwkRetriever = new DefaultResourceRetriever(100000, 100000);
          JWKSetCache jwkSetCache = new DefaultJWKSetCache(1440,
              1430, TimeUnit.MINUTES);
          keySource = new RemoteJWKSet(new URL(
              "https://login.windows.net/36799f34-92fd-4612-8473-80173f2406e8/discovery/v2.0/keys"),jwkRetriever,jwkSetCache);
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }
        JWSAlgorithm expectedJWSAlg = JWSAlgorithm.RS256;
        JWSKeySelector keySelector = new JWSVerificationKeySelector(expectedJWSAlg,keySource);
        jwtProcessor.setJWSKeySelector(keySelector);

        JWTClaimsSet claimsSet = null;
        SecurityContext ctx = null; // optional context parameter, not required here
        try {
          claimsSet = jwtProcessor.process(token, ctx);
        } catch (ParseException e) {
          e.printStackTrace();
        } catch (BadJOSEException e) {
          e.printStackTrace();
        } catch (JOSEException e) {
          e.printStackTrace();
        }

我已经在信任库中添加了证书。请参阅下面来自 Spring application.properties 文件的配置:

    trust.store=myapp_dev.p12
trust.store.password=changeit

我做错了什么?

spring-security azure-active-directory bearer-token nimbus-jose-jwt
© www.soinside.com 2019 - 2024. All rights reserved.