Java web service via https - How to add a self-signed certificate to client api?

I have a Hello World web service created using axis2. I would like to write an api client that could use this service through httpswith a self-signed certificate. I have a self-signed certificate myCertificate.cerand keystorecontaining it.

Here is my client API:

public class MyApi{

public Object callMyService(){

Axis2TestStub stub = new Axis2TestStub(
"https://localhost:8443/axis2/services/Axis2Test");

System.setProperty("javax.net.ssl.trustStore",
"src/main/resources/myKeystore.jks")
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

Hello request = new Hello();
request.setMot("World");
HelloResponse response = stub.hello(request);
Object mot = response.get_return();
return mot;

This works, but I would like to use myCertificate.cer, not keystorecontaining it. Does anyone know how to do this? I tried to redefine the protocol httpswithout success:

HttpSecureProtocol myHttpsProtocol = new HttpSecureProtocol();
myHttpsProtocol .setCheckHostname(false);
myHttpsProtocol .addTrustMaterial(new TrustMaterial("myCertificate.cer"));
Protocol customHttps = new Protocol("https", 
(ProtocolSocketFactory) myHttpsProtocol , 8443);
Protocol.registerProtocol("https", customHttps);
+3
source share
2 answers

api, https . myCertificate.cer , .

.

( , , .. , ).

, myCertificate.cer, , .

, , . .

JAR API- . ( , JAR, ). .

IMHO, , , , CA (, Verisign, Thawte).

+3

cacerts JDK, . , . , , . , , :

C:/<jdk-version/bin/keytool -import -alias myalias -file mycert.crt -keystore C:/<jdk-version>/jre/lib/security/cacerts
+2

Source: https://habr.com/ru/post/1731909/


All Articles