为了验证 JWT,我使用 jose4j 从 url 获取证书,在这种情况下,来自 google:
HttpsJwks httpsJkws = new HttpsJwks("https://www.googleapis.com/oauth2/v3/certs");
HttpsJwksVerificationKeyResolver httpsJwksKeyResolver = new HttpsJwksVerificationKeyResolver(httpsJkws);
//httpsJkws.setSimpleHttpGet(simpleHttpGet);
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
.setVerificationKeyResolver(httpsJwksKeyResolver)
.build(); // create the JwtConsumer instance
但是,这给我一个证书错误:
PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
好的,是的,我可以使用一些脚本将它添加到 JVM 的 trustore 中,但我不想(基本上,它不是自签名证书,并且可以通过常规浏览器正常工作)。大多数时候,我使用 Apache HTTP 客户端 4.x,由于某种原因,调用确实可以正常工作:
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpResponse httpResponse = httpClient.execute(new HttpGet("https://www.googleapis.com/oauth2/v3/certs"));
String response = (httpResponse.getEntity() != null) ? EntityUtils.toString(httpResponse.getEntity()) : null;
log.debug(response);
} catch (IOException e) {
log.error("I/O Error when retrieving content from '" + jwksEndpointUrl + "': " + e.getMessage());
}
我也尝试过使用 vanilla java,比如new URL(jwksEndpointUrl).openStream()
,在这里我得到了相同的证书问题。
那么,Apache HttpComponents 客户端有什么不同,我怎样才能通过 jose4j 实现标准 Java HTTP GET 呢?