2

Есть два файла с расширением example.key и example.crt , и есть java приложение с использованием класса HttpsURLConnection. Вопрос, что мне c этими двумя файлами сделать что бы мое приложение заработало. Пробовал использовать PKCS12 и явно указывать в 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());

1 ответ 1

1

Я плюнул на все эти танцы с бубнами и в итоге за 30 минут сделал то, что не мог за пару дней, я использовал curl4j библиотеку в итоге одна строчка заменила тонну кода с java.net.ssl

String json = $("-kX POST --cert crt/example.crt --key crt/example.key -H 
'Content-Type: application/json' -d " + dataJSON+ " example.com");

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.