简体   繁体   中英

How to consume https RestFul Webservice using jersey client

Problem : I want to consume the HTTPS Restful Webservice by using jersey client.

1) I got CARoot certificate from the 3rd party and installed on browser (Mozilla) and i am able to access these services from RestClient on Mozilla browser.

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

2) I want to configure this Webservice in JAVA code by using jersey client.

3) What are the steps i have to do to configure these certificates in java code.

4) I don't want to configure these in local 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);

I have downloaded the .crt files from browser and need to configure, don't know how ?

In your java installation folder is a file called cacerts . This is the "Keystore" or "Truststore" of your JRE. It contains all certificates that are trusted by your JRE. You can add / remove certificates from the truststore. To easily add / remove certificates, you can use the GUI Programm Keystore Explorer .

Option 1 Using Keystore Explorer and the default Truststore

  1. Open the truststore with the Keystore Explorer.
    (The truststore should be under <JRE-HOME>/lib/security/cacerts , The default password should be "changeit" or "changeme")

  2. Drag and drop the ".crt" file into the opened truststore in the Keystore Explorer

  3. Click "import" and Save the truststore

Now your JRE installation is ready to consume the webservice.


Option 2 Using Keystore Explorer and a separate Truststore

  1. Copy your default truststore into your project. The path of the default truststroe is: <JRE-HOME>/lib/security/cacerts

  2. Open the copied truststore with the Keystore Explorer.
    (The default password should be "changeit" or "changeme")

  3. Drag and drop the ".crt" file into the opened truststore in the Keystore Explorer

  4. Start your programm with the following VM-Arguments:

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


Option 3 Using 2 Truststores (Default + Separate Truststore)

If you want to use the default truststore and a separate one for the Website refer to this post https://stackoverflow.com/a/24561444/1638059

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM