Java https server certificate authentication Solution
"Unable to find valid certification path to requested target", "PKIX path building failed" error for Java https connection
Cause
This problem occurs because the Java root certificate library does not contain the root certificate on the HTTPS server, so it cannot be authenticated.
Solution
There are two solutions that are easier to implement:
Import the server certificate to the local Java environment code to ignore the Certificate Trust Issue
This is not recommended because the second solution causes security issues.
Certificate import considerations
Importing the root certificate of the server to the root certificate library of the Java Runtime Environment can solve the https connection problem of the corresponding server.
The method is to add the server certificate to the jre/lib/security/cacerts file using the keytool under $ JAVA_HOME/jre/bin.
Note that:
Determine the path of the Java Runtime Environment jre used by the current java program (may be the jre under jdk or a separate jre ). Make sure you have the write permission for the jre/lib/security/cacerts file (you can run keytool as an administrator ). Detailed steps: 1. Obtain the server certificate file
You can open the server website page in a browser and export the Certificate file. Assume that the exported Certificate file is test. crt.
2. Generate a keystore File
Use keytool to generate the key file keystore:
keytool -importcert -noprompt -trustcacerts -alias test -file test.cer -keystore ~/mykeystore
A password is required here. Remember the password after setting it.
3. Import the certificate to the Java Runtime Environment
Import the certificate to jre/lib/security/cacerts:
keytool -importkeystore -srckeystore ~/keystore -destkeystore [path_to_jre]/lib/security/cacerts
The target keystore password (that is, the jre/lib/security/cacerts password) and the source keystore password (the previously set password) are required ).
Check whether the import is successful. If it succeeds, restart the Java program.