国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

請求https報錯證書校驗失敗(javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX

這篇具有很好參考價值的文章主要介紹了請求https報錯證書校驗失?。╦avax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

項目場景:

請求https報錯證書校驗失?。╦avax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target)

問題描述

項目中請求第三方https的URL,報錯ssl證書校驗失敗

14:33:55.195 [main] ERROR com.bd.common.utils.OKHttpUtil - context
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
	at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
	at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
	at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
	at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
	at com.squareup.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
	at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
	at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
	at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
	at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
	at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
	at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)

原因分析:

ssl校驗失敗有兩種可能,一種是服務(wù)端ssl證書配置錯誤,一種是客戶端請求的是非信任的https地址,客戶端不信任該https的ssl證書。懷疑是使用了自簽名證書,非各大廠提供簽名證書

解決方案:

該問題有兩種請求方案文章來源地址http://www.zghlxwxcb.cn/news/detail-780363.html

  1. 手動下載ssl證書
    (1)在瀏覽器地址欄里找到“證書信息”->“詳細信息”->“復(fù)制到文件”->選擇DER編碼二進制X.509(.CER)(D) 導(dǎo)出證書,如證書名為pro1.cer;
    (2)將pro1.cer上傳至服務(wù)器/usr/java;
    (3)執(zhí)行keytool -import -alias pro1 -keystore /usr/java/jdk1.8.0/jre/lib/security/cacerts -file /usr/java/pro1.cer;
    (4)輸入密碼:changeit
    (5)查看證書庫keytool -list -keystore /usr/java/jdk1.8.0/jre/lib/security/cacerts|grep pro1.cer,如找到到 證書信息,代表完成導(dǎo)入。
    (6)回到程序,調(diào)試java程序不再報錯。
  2. 使用提供的工具類自動下載證書
    使用該工具過程中,工具類會提示用戶選擇是否將證書添加到信任存儲中。用戶可以輸入證書的序號,將指定的證書添加到信任存儲中。
package com.sword.lasms.commons.httpClient;

/*
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

/**
 *  發(fā)送 Https 請求報錯:PKIX: unable to find valid certification path to requested target
 *
 * 問題的根本是:缺少安全證書時出現(xiàn)的異常。
 * 解決問題方法:將要訪問的webservice/url....的安全認證證書導(dǎo)入到客戶端即可。
 *
 * 以下是獲取安全證書的一種方法,通過以下程序獲取安全證書
 * (僅需要修改參數(shù) args )
 *
 */
public class InstallCert {

    public static void main(String[] args) throws Exception {
//        args= new String[]{"sgdzpic.3322.org:20008"};  // 填寫要訪問的 url:端口
        String host;
        int port;
        char[] passphrase;
        if ((args.length == 1) || (args.length == 2)) {
            String[] c = args[0].split(":");
            host = c[0];
            port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
            String p = (args.length == 1) ? "changeit" : args[1];
            passphrase = p.toCharArray();
        } else {
            System.out
                    .println("Usage: java InstallCert <host>[:port] [passphrase]");
            return;
        }

        File file = new File("jssecacerts");
        if (file.isFile() == false) {
            char SEP = File.separatorChar;
            File dir = new File(System.getProperty("java.home") + SEP + "lib"
                    + SEP + "security");
            file = new File(dir, "jssecacerts");
            if (file.isFile() == false) {
                file = new File(dir, "cacerts");
            }
        }
        System.out.println("Loading KeyStore " + file + "...");
        InputStream in = new FileInputStream(file);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(in, passphrase);
        in.close();

        SSLContext context = SSLContext.getInstance("TLS");
        TrustManagerFactory tmf = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        X509TrustManager defaultTrustManager = (X509TrustManager) tmf
                .getTrustManagers()[0];
        SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
        context.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory factory = context.getSocketFactory();

        System.out
                .println("Opening connection to " + host + ":" + port + "...");
        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
        socket.setSoTimeout(10000);
        try {
            System.out.println("Starting SSL handshake...");
            socket.startHandshake();
            socket.close();
            System.out.println();
            System.out.println("No errors, certificate is already trusted");
        } catch (SSLException e) {
            System.out.println();
            e.printStackTrace(System.out);
        }

        X509Certificate[] chain = tm.chain;
        if (chain == null) {
            System.out.println("Could not obtain server certificate chain");
            return;
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));

        System.out.println();
        System.out.println("Server sent " + chain.length + " certificate(s):");
        System.out.println();
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        for (int i = 0; i < chain.length; i++) {
            X509Certificate cert = chain[i];
            System.out.println(" " + (i + 1) + " Subject "
                    + cert.getSubjectDN());
            System.out.println("   Issuer  " + cert.getIssuerDN());
            sha1.update(cert.getEncoded());
            System.out.println("   sha1    " + toHexString(sha1.digest()));
            md5.update(cert.getEncoded());
            System.out.println("   md5     " + toHexString(md5.digest()));
            System.out.println();
        }

        System.out
                .println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
        String line = reader.readLine().trim();
        int k;
        try {
            k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
        } catch (NumberFormatException e) {
            System.out.println("KeyStore not changed");
            return;
        }

        X509Certificate cert = chain[k];
        String alias = host + "-" + (k + 1);
        ks.setCertificateEntry(alias, cert);

        OutputStream out = new FileOutputStream("jssecacerts");
        ks.store(out, passphrase);
        out.close();

        System.out.println();
        System.out.println(cert);
        System.out.println();
        System.out
                .println("Added certificate to keystore 'jssecacerts' using alias '"
                        + alias + "'");
    }

    private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();

    private static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 3);
        for (int b : bytes) {
            b &= 0xff;
            sb.append(HEXDIGITS[b >> 4]);
            sb.append(HEXDIGITS[b & 15]);
            sb.append(' ');
        }
        return sb.toString();
    }

    private static class SavingTrustManager implements X509TrustManager {

        private final X509TrustManager tm;
        private X509Certificate[] chain;

        SavingTrustManager(X509TrustManager tm) {
            this.tm = tm;
        }

        public X509Certificate[] getAcceptedIssuers() {
            throw new UnsupportedOperationException();
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            throw new UnsupportedOperationException();
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            this.chain = chain;
            tm.checkServerTrusted(chain, authType);
        }
    }

}

到了這里,關(guān)于請求https報錯證書校驗失?。╦avax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 證書異常導(dǎo)致:javax.net.ssl.SSLHandshakeException: sun.security.validator

    證書異常導(dǎo)致:javax.net.ssl.SSLHandshakeException: sun.security.validator

    ????????當我們應(yīng)用程序 訪問設(shè)有https證書的服務(wù) 時,若 JRE未安裝指定證書 則會提示標題的報錯,此時有 兩種方式 解決該問題: ? ? ? ?按照實際場景,可從信息科、網(wǎng)絡(luò)處等部門協(xié)調(diào)獲取證書,也可以直接從安裝證書的電腦上,通過瀏覽器下載證書,本文介紹后一種

    2024年01月17日
    瀏覽(18)
  • 解決遠程調(diào)用三方接口:javax.net.ssl.SSLHandshakeException報錯

    最近在對接騰訊會議API接口,在鑒權(quán)完成后開始調(diào)用對方的接口,在此過程中出現(xiàn)調(diào)用報錯:javax.net.ssl.SSLHandshakeException。 當你在進行https請求時,JDK中不存在三方服務(wù)的信任證書,導(dǎo)致出現(xiàn)錯誤javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑構(gòu)建失敗。

    2024年02月13日
    瀏覽(28)
  • 解決 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX 路徑構(gòu)建失敗

    解決 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX 路徑構(gòu)建失敗

    之前一個項目使用Java請求客戶提供的接口時,返回報錯如下: javax.net.ssl.SSLHandshakeException 錯誤通常表示 SSL 握手失敗。這意味著在建立 SSL 連接時,Java 安全管理器無法構(gòu)建一個有效的證書路徑,從而無法找到有效的證書路徑來驗證目標站點的證書,觸發(fā)此錯誤信息的調(diào)用方

    2024年02月04日
    瀏覽(23)
  • 解決報錯:javax.net.ssl.SSLHandshakeException: No appropriate protocol

    解決報錯:javax.net.ssl.SSLHandshakeException: No appropriate protocol

    使用對象存儲進行文件上傳時報錯 注:該問題只要需要用到http的都有可能出現(xiàn),不是只針對對象存儲 jdk 的 java.security 文件存在配置問題 1、查看當前服務(wù)器使用的 jdk 版本 命令: java -version 2、查看該jdk的安裝目錄 命令: find / -name java.security 這里選擇通過搜索 java.security 來

    2024年01月24日
    瀏覽(22)
  • javax.net.ssl.SSLHandshakeException No appropriate protocol報錯解決方案

    javax.net.ssl.SSLHandshakeException No appropriate protocol報錯解決方案

    用java開發(fā)了一個簡單的***發(fā)送郵件***的程序,本地運行正常,但是上傳到服務(wù)器就出現(xiàn)報錯: 方案一 [原文參考地址](javax.net.ssl.SSLHandshakeException: No appropriate protocol報錯解決_藍緣的博客-CSDN博客) ? 1、找到j(luò)dk目錄/jre/lib/security/java.security,去掉jdk.tls.disabledAlgorithm中的SSLv3、T

    2023年04月15日
    瀏覽(21)
  • WGCLOUD監(jiān)控sqlserver報錯javax.net.ssl.SSLHandshakeException: The server selected protocol version TLS10

    翻譯成中文:SqlServer 服務(wù)器只接受 TLS1.0,但是客戶端給的是 TLS1.2。 找到 jdk11confsecurity 下java.security 文件 1.?把 jdk.tls.disabledAlgorithms 配置項中 TLSv1 刪除,這樣? TLS1.0 就可以使用了。 #jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, # ? ?DH keySize 1024, EC keySize 224, 3D

    2024年02月06日
    瀏覽(24)
  • Debezium報錯處理系列之九十四: javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake

    研究Debezium技術(shù)遇到的各種錯誤解決方法系列文章傳送門: Debezium從入門到精通系列之:百篇系列文章匯總之研究Debezium技術(shù)遇到的各種錯誤的解決方法

    2024年01月18日
    瀏覽(22)
  • javax.net.ssl.SSLHandshakeException

    解決辦法升級jdk版本或者修改jdk文件 1、對于服務(wù)器來說要支持域名并且不進行ssl證書校驗,需要升級到j(luò)dk1.8的201版本及以上 2、修改…JavaJDKjrelibsecurity目錄下java.security文件,添加下面語句到文件內(nèi)容中

    2024年02月11日
    瀏覽(21)
  • 請求第三方Https地址忽略SSL證書校驗

    說明:個人使用記錄 需要在請求之前忽略ssl協(xié)議,這里是直接使用靜態(tài)方法初始化時就執(zhí)行了 也需要在請求接口之前忽略SSL

    2024年04月10日
    瀏覽(27)
  • Macos jdk ssl javax.net.ssl.SSLHandshakeException完美解決

    Macos jdk ssl javax.net.ssl.SSLHandshakeException完美解決

    報了這么一個錯誤 javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake 網(wǎng)上一大把,測試不能用,谷歌了一下,發(fā)現(xiàn)少配置了一個環(huán)境變量。 System.setProperty(\\\"jdk.tls.useExtendedMasterSecret\\\", \\\"false\\\");//設(shè)置環(huán)境變量 /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/security/java.se

    2024年02月13日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包