java - 信任 SSL 证书 : how does Commons HTTP client trust more than standard Java?

标签 java ssl apache-httpclient-4.x websphere-liberty jose4j

为了验证 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/

相关文章:

java - Spring session ,部署后从 "local class incompatible"恢复

java - 在 java 中生成 X509v(1/3) 证书(有弹性的城堡)

RCurl 和自签名证书问题

c - 关于使用 libssl 编程的任何好的例子?

java - 如何从 HttpClient 获取 cookie?

performance - 提高 Jetty 性能

java - 如何检查数组是否是二维数组中的元素之一

java - 为什么 java.net.URL.toString 在 EMR AMI 3.8.0 上抛出 NullPointerException?

java - 如何使用 Apache HttpClient 将参数添加到 HTTP header

java - oop 中的接口(interface), "java-like"和 "c++-like"