如何使用jersey客户端使用https RestFul Webservice

时间:2017-03-08 11:27:49

标签: java web-services rest

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

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

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

2)我想使用jersey客户端以JAVA代码配置此Webservice。

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文件,需要配置,不知道怎么做?

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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

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

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

    2. 使用Keystore Explorer打开复制的信任库 (默认密码应为“changeit”或“changeme”)

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

    4. 使用以下VM-Arguments启动您的程序:

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

    5. 选项3 使用2个Truststores(默认+独立信任库)

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