Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?

后端 未结 22 2524
名媛妹妹
名媛妹妹 2020-11-21 05:59

Edit :- Tried to format the question and accepted answer in more presentable way at mine Blog

Here is the original issue.

相关标签:
22条回答
  • 2020-11-21 06:11

    DEPLOYABLE SOLUTION (Alpine Linux)

    To be able to fix this issue in our application environments, we have prepared Linux terminal commands as follows:

    cd ~
    

    Will generate cert file in home directory.

    apk add openssl
    

    This command installs openssl in alpine Linux. You can find proper commands for other Linux distributions.

    openssl s_client -connect <host-dns-ssl-belongs> < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > public.crt
    

    Generated the needed cert file.

    sudo $JAVA_HOME/bin/keytool -import -alias server_name -keystore $JAVA_HOME/lib/security/cacerts -file public.crt -storepass changeit -noprompt
    

    Applied the generated file to the JRE with the program 'keytool'.

    Note: Please replace your DNS with <host-dns-ssl-belongs>

    Note2: Please gently note that -noprompt will not prompt the verification message (yes/no) and -storepass changeit parameter will disable password prompt and provide the needed password (default is 'changeit'). These two properties will let you use those scripts in your application environments like building a Docker image.

    Note3 If you are deploying your app via Docker, you can generate the secret file once and put it in your application project files. You won't need to generate it again and again.

    0 讨论(0)
  • 2020-11-21 06:12

    How to work-it in Tomcat 7

    I wanted to support a self signed certificate in a Tomcat App but the following snippet failed to work

    import java.io.DataOutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class HTTPSPlayground {
        public static void main(String[] args) throws Exception {
    
            URL url = new URL("https:// ... .com");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            httpURLConnection.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
    
            String serializedMessage = "{}";
            wr.writeBytes(serializedMessage);
            wr.flush();
            wr.close();
    
            int responseCode = httpURLConnection.getResponseCode();
            System.out.println(responseCode);
        }
    }
    

    this is what solved my issue:

    1) Download the .crt file

    echo -n | openssl s_client -connect <your domain>:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/<your domain>.crt
    
    • replace <your domain> with your domain (e.g. jossef.com)

    2) Apply the .crt file in Java's cacerts certificate store

    keytool -import -v -trustcacerts -alias <your domain> -file ~/<your domain>.crt -keystore <JAVA HOME>/jre/lib/security/cacerts -keypass changeit -storepass changeit
    
    • replace <your domain> with your domain (e.g. jossef.com)
    • replace <JAVA HOME> with your java home directory

    3) Hack it

    Even though iv'e installed my certificate in Java's default certificate stores, Tomcat ignores that (seems like it's not configured to use Java's default certificate stores).

    To hack this, add the following somewhere in your code:

    String certificatesTrustStorePath = "<JAVA HOME>/jre/lib/security/cacerts";
    System.setProperty("javax.net.ssl.trustStore", certificatesTrustStorePath);
    
    // ...
    
    0 讨论(0)
  • 2020-11-21 06:12

    For me didn't work the recognized solution from this post: https://stackoverflow.com/a/9619478/4507034.

    Instead, I managed to solve the problem by importing the certification to my machine trusted certifications.

    Steps:

    1. Go to the URL (eg. https://localhost:8443/yourpath) where the certification is not working.
    2. Export the certification as described in the mentioned post.
    3. On your windows machine open: Manage computer certificates
    4. Go to Trusted Root Certification Authorities -> Certificates
    5. Import here your your_certification_name.cer file.
    0 讨论(0)
  • 2020-11-21 06:12

    for safety we should not use self signed certificates in our implementation. However, when it comes to development often we have to use trial environments which got self-signed certs. I tried to fix this issue programmatically in my code and I fail. However, by adding the cert to the jre trust-store fixed my issue. Please find below steps,

    1. Download the site cert,

      • Using Chrome
      • Using Firefox
    2. Copy the certificate(ex:cert_file.cer) into the directory $JAVA_HOME\Jre\Lib\Security

    3. Open CMD in Administrator and change the directory to $JAVA_HOME\Jre\Lib\Security

    4. Import the certificate to a trust store using below command,

    keytool -import -alias ca -file cert_file.cer -keystore cacerts -storepass changeit

    If you got a error saying keytool is not recognizable please refer this.

    Type yes like below

    Trust this certificate: [Yes]

    1. Now try to run your code or access the URL programmatically using java.

    Update

    If your app server is jboss try adding below system property

    System.setProperty("org.jboss.security.ignoreHttpsHost","true");
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-21 06:15

    You need to add the certificate for App2 to the truststore file of the used JVM located at %JAVA_HOME%\lib\security\cacerts.

    First you can check if your certificate is already in the truststore by running the following command: keytool -list -keystore "%JAVA_HOME%/jre/lib/security/cacerts" (you don't need to provide a password)

    If your certificate is missing, you can get it by downloading it with your browser and add it to the truststore with the following command:

    keytool -import -noprompt -trustcacerts -alias <AliasName> -file <certificate> -keystore <KeystoreFile> -storepass <Password>

    Example:
    keytool -import -noprompt -trustcacerts -alias myFancyAlias -file /path/to/my/cert/myCert.cer -keystore /path/to/my/jdk/jre/lib/security/cacerts/keystore.jks -storepass changeit

    After import you can run the first command again to check if your certificate was added.

    Sun/Oracle information can be found here.

    0 讨论(0)
  • 2020-11-21 06:16

    For me, this error appeared too while trying to connect to a process behind an NGINX reverse proxy which was handling the SSL.

    It turned out the problem was a certificate without the entire certificate chain concatenated. When I added intermediate certs, the problem was solved.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题