HttpGet with HTTPS : SSLPeerUnverifiedException

前端 未结 6 1672
旧时难觅i
旧时难觅i 2020-11-27 13:29

Using HttpClient, I receive the following error when attempting to communicate over HTTPS:

Exception in thread \"main\" javax.net.ssl.SSLPeerUnverifie

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 13:55

    This answer follows on to owlstead and Mat's responses. It applies to SE/EE installations, not ME/mobile/Android SSL.

    Since no one has yet mentioned it, I'll mention the "production way" to fix this: Follow the steps from the AuthSSLProtocolSocketFactory class in HttpClient to update your trust store & key stores.

    1. Import a trusted certificate and generate a truststore file

    keytool -import -alias "my server cert" -file server.crt -keystore my.truststore

    1. Generate a new key (use the same password as the truststore)

    keytool -genkey -v -alias "my client key" -validity 365 -keystore my.keystore

    1. Issue a certificate signing request (CSR)

    keytool -certreq -alias "my client key" -file mycertreq.csr -keystore my.keystore

    1. (self-sign or get your cert signed)

    2. Import the trusted CA root certificate

    keytool -import -alias "my trusted ca" -file caroot.crt -keystore my.keystore

    1. Import the PKCS#7 file containg the complete certificate chain

    keytool -import -alias "my client key" -file mycert.p7 -keystore my.keystore

    1. Verify the resultant keystore file's contents

    keytool -list -v -keystore my.keystore

    If you don't have a server certificate, generate one in JKS format, then export it as a CRT file. Source: keytool documentation

    keytool -genkey -alias server-alias -keyalg RSA -keypass changeit
        -storepass changeit -keystore my.keystore
    
    keytool -export -alias server-alias -storepass changeit
        -file server.crt -keystore my.keystore
    

提交回复
热议问题