【问题标题】:How to consume https RestFul Webservice using jersey client如何使用 jersey 客户端使用 https RestFul Webservice
【发布时间】:2017-07-28 22:43:05
【问题描述】:

问题:我想通过 jersey 客户端使用 HTTPS Restful Webservice。

1) 我从第 3 方获得 CARoot 证书并安装在浏览器 (Mozilla) 上,我可以从 Mozilla 浏览器上的 RestClient 访问这些服务。

i) RootCA.pem ii) SubCA-Client.pem iii) abc_sdsdllkl_p12.pfx

2) 我想使用 jersey 客户端在 JAVA 代码中配置这个 Web 服务。

3) 在 java 代码中配置这些证书的步骤是什么。

4) 我不想在本地 JRE 中配置这些。

PAttributes pd = new PAttributes();
            ClassLoader classLoader = pd.getClass().getClassLoader();
            File file = new File(classLoader.getResource("cacerts").getFile());
            System.setProperty("javax.net.ssl.trustStore",file.getAbsolutePath());
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
            Client client = Client.create();
            WebResource webResource = client.resource("https://xyz/abc/getAttributes");
            String input = new PAttributes().getRequestBody();
            ClientResponse clientResponse = webResource.accept("application/xml").type("application/xml").post(ClientResponse.class, input);
            String output = clientResponse.getEntity(String.class);
            System.out.println("output"+output);

我已经从浏览器下载了.crt文件,需要配置,不知道怎么配置?

【问题讨论】:

    标签: java web-services rest


    【解决方案1】:

    在您的 java 安装文件夹中有一个名为 cacerts 的文件。这是 JRE 的“Keystore”或“Truststore”。它包含您的 JRE 信任的所有证书。您可以从信任库中添加/删除证书。 要轻松添加/删除证书,您可以使用 GUI Programm Keystore Explorer

    选项 1 使用 Keystore Explorer 和默认的 Truststore

    1. 使用 Keystore Explorer 打开信任库。
      (信任库应在<JRE-HOME>/lib/security/cacerts下,默认密码应为“changeit”或“changeme”)

    2. 将“.crt”文件拖放到 Keystore Explorer 中打开的信任库中

    3. 点击“导入”并保存信任库

    现在您的 JRE 安装已准备好使用 Web 服务。


    选项 2 使用 Keystore Explorer 和单独的 Truststore

    1. 将您的默认信任库复制到您的项目中。默认信任路径为:<JRE-HOME>/lib/security/cacerts

    2. 使用密钥库资源管理器打开复制的信任库。
      (默认密码应该是“changeit”或“changeme”)

    3. 将“.crt”文件拖放到 Keystore Explorer 中打开的信任库中

    4. 使用以下 VM 参数启动您的程序:

      -Djavax.net.ssl.trustStore [path-to-copied-truststore]
      -Djavax.net.ssl.trustStorePassword [信任库密码]


    选项 3 使用 2 个信任库(默认 + 单独的信任库)

    如果您想为网站使用默认信任库和单独的信任库,请参阅此帖子https://stackoverflow.com/a/24561444/1638059

    【讨论】:

    • 我不知道该怎么做,我有两个 .crt 文件 'RootCA.crt' 和 SubCA-Client.crt,将这些文件放在哪里以及如何配置到 Java 代码中。
    • 您使用的是 Linux 还是 Windows?
    • 我使用的是 Windows 10 操作系统。我是 SSL 证书的新手。
    • 我编辑了 anwnser 以便更好地理解。
    • 在尝试第二种方法时遇到异常。 com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: 在 com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151) 处收到致命警报:handshake_failure 在 com.sun .jersey.api.client.Client.handle(Client.java:648) 在 com.sun.jersey.api.client.WebResource.handle(WebResource.java:680) 在 com.sun.jersey.api.client.WebResource .access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
    猜你喜欢
    • 2011-01-09
    • 2013-08-26
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2018-11-09
    • 1970-01-01
    • 2011-03-26
    相关资源
    最近更新 更多