为了验证 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 path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
好的,是的,我可以使用一些脚本将其添加到 JVM 的信任库中,但我不想这样做(因为基本上,它不是自签名证书,并且可以通过常规浏览器正常工作)。大多数时候,我使用 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 实现相同的效果?
最佳答案
直到最近,Liberty 的行为都是默认不信任任何内容,因此即使是 Google 等知名证书也必须添加到其信任库中,以避免出现您所看到的错误。
在较新的版本中,(190012+?)可以设置“trustDefaultCerts=true”,然后它的行为更像浏览器,并且默认信任来自 Google 等知名颁发者的证书。以下是 server.xml 中的示例片段:
<keyStore id="defaultKeyStore" password="keyspass" /> <ssl id="defaultSSLConfig" keyStoreRef="defaultKeyStore" trustDefaultCerts="true"/>
关于java - 信任 SSL 证书 : how does Commons HTTP client trust more than standard Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62802726/