How to connect to a secure website using SSL in Java with a pkcs12 file?

前端 未结 8 1361
野性不改
野性不改 2020-12-01 06:37

I have a pkcs12 file. I need to use this to connect to a webpage using https protocol. I came across some code where in order to connect to a secure web page i need to set t

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 07:24

    For anyone encountering a similar situation I was able to solve the issue above as follows:

    1. Regenerate your pkcs12 file as follows:

      openssl pkcs12 -in oldpkcs.p12 -out keys -passout pass:tmp
      openssl pkcs12 -in keys -export -out new.p12 -passin pass:tmp -passout pass:newpasswd
      
    2. Import the CA certificate from server into a TrustStore ( either your own, or the java keystore in $JAVA_HOME/jre/lib/security/cacerts, password: changeit).

    3. Set the following system properties:

      System.setProperty("javax.net.ssl.trustStore", "myTrustStore");
      System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
      System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
      System.setProperty("javax.net.ssl.keyStore", "new.p12");
      System.setProperty("javax.net.ssl.keyStorePassword", "newpasswd");
      
    4. Test ur url.

    Courtesy@ http://forums.sun.com/thread.jspa?threadID=5296333

提交回复
热议问题