Java https server certificate authentication Solution

Source: Internet
Author: User
Tags server website

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.