How to add a java SSL certificate


There are two files with the example extension.key and example. crt, and there is a java application using the HttpsURLConnection class. The question is, what should I do with these two files to make my application work. I tried using PKCS12 and couldn't explicitly specify it in the keystore.

    KeyStore ks = KeyStore.getInstance("PKCS12");
    FileInputStream fis =new 
    FileInputStream(".../example.p12");
    ks.load(fis, "qwerty".toCharArray()); // There are other ways to read 
    the password.
    fis.close();


    SSLContext sc = SSLContext.getInstance("SSL");
    KeyManagerFactory kmf 
    =KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks,"qwerty".toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    sc.init(kms, null, new SecureRandom());
    URL url = new 
    URL("https://...");
    HttpsURLConnection request = (HttpsURLConnection) url.openConnection();
        request.setSSLSocketFactory(sc.getSocketFactory());
Author: Amos, 2019-07-29

1 answers

I spat on all this dancing with tambourines and ended up doing in 30 minutes what I couldn't do in a couple of days, I used the curl4j library as a result, one line replaced a ton of code with java. net. ssl

String json = $("-kX POST --cert crt/example.crt --key crt/example.key -H 
'Content-Type: application/json' -d " + dataJSON+ " example.com");
 1
Author: Amos, 2019-07-31 15:50:30