有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正在获取ssl。SSLHandshakeException在使用带有标头的REST客户端时发生,但在使用PostMan时可以正常工作

我有一个外部REST资源,详细信息如下:

  1. URL:abc.com/orders(域中有https
  2. 我需要将UserID作为HTTP头传递,键“user”值为“abcd”
  3. 这将返回一个JSON响应

为此,我使用以下Java代码:

try {

            Client client = Client.create();

            WebResource webResource = client.resource("abc.com/orders");

            ClientResponse response = webResource.header("user", "abcd").accept("application/json")
                    .get(ClientResponse.class);

            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Output from Server .... \n");
            System.out.println(output);

        } catch (Exception e) {

            e.printStackTrace();

        }

但是我得到了下面的异常,尽管它与PostMan配合得很好

com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
    at com.sun.jersey.api.client.Client.handle(Client.java:652)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)

我试着在上面搜索了一下,找到了需要从该URL获取证书并添加到jdk/lib/security文件夹的地方。但我不知道如何继续

使用openssl,我得到以下输出:

user>openssl s_client -connect  abc.com:443

CONNECTED(00000214)
7832:error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error:s23_clnt.c:802:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 7 bytes and written 308 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : 0000
    Session-ID:
    Session-ID-ctx:
    Master-Key:
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1531657560
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)

共 (2) 个答案

  1. # 1 楼答案

    老兄!不需要以上任何一项!!! 只需在RESTAPI代码之前传递RestAssured.useRelaxedHTTPSValidation();。 完成了

  2. # 2 楼答案

    以下是使程序正常运行的说明。顺便说一句,我在windows上,使用谷歌浏览器

    你确实需要一份证书

    1)首先,转到url(不管是网站还是restful服务),让我们选择谷歌。通用域名格式。在页面上单击鼠标右键,然后单击“检查”

    enter image description here

    2)转到“安全”选项卡

    enter image description here

    3)到达后,单击“查看证书”。 enter image description here

    将弹出一个窗口,显示站点证书的详细信息

    4)转到“认证路径”选项卡。然后双击要从层次结构中获取的证书enter image description here

    将弹出一个新窗口:

    enter image description here

    在本例中,我选择了名为“Google trust services…”的根证书,但你可以选择一个更具体的,比如“谷歌互联网管理局G3”。我认为越具体,它提供的安全性就越高(但我不确定)

    5)转到“详细信息”选项卡并选择您的证书名称:

    enter image description here

    6)单击“复制到文件”,然后选择文件名和保存位置。我在桌面上保存了我的,并将其命名为“test.cer”

    现在,您已完成证书的导出。接下来,您希望将其添加到jvm的信任库中

    1)查找应用程序运行在哪个JRE上,例如,我的计算机上只有一个JRE(不包括与JDK捆绑的JRE)。它位于这里:

    enter image description here

    存储证书的目标文件是cacerts:

    enter image description here

    2)以管理员身份打开cmd,然后执行cd "C:\Program Files\Java\jre-10.0.1\lib\security"(在我的例子中是cacerts的路径)

    3)发出以下命令:

    keytool -import -storepass changeit -noprompt -alias *alias* -keystore cacerts -trustcacerts -file *path_to_certificate*

    请注意,别名可以是任何内容,无论您如何调用文件,只要它不与信任库中已有的其他证书的别名冲突

    在我的情况下,我提出以下问题:

    enter image description here

    4)现在可以发出以下命令:keytool -list -keystore cacerts -alias *alias*以确保已添加证书。当您发出此命令时,它将询问您的密码。在第三步中,我给你的命令有一个选项:-storepass changeit,所以你的密码是changeit

    enter image description here

    就我而言,一切都很好

    5)现在可以重新启动应用程序,它应该可以工作了。有些人建议重启电脑,但我不知道这是否必要