IDEA maven引入SSL證書(shū)校驗(yàn)問(wèn)題及處理
maven依賴(lài)導(dǎo)入問(wèn)題
之前在做項(xiàng)目的時(shí)候就遇到了這個(gè)問(wèn)題,pom文件中引入依賴(lài)時(shí),會(huì)有如下報(bào)錯(cuò):
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
這是因?yàn)閙aven請(qǐng)求遠(yuǎn)程倉(cāng)庫(kù)進(jìn)行下載依賴(lài)jar包時(shí)進(jìn)行了安全證書(shū)的驗(yàn)證,由于本地jdk沒(méi)有添加安全證書(shū),因此在執(zhí)行安全驗(yàn)證時(shí)沒(méi)有通過(guò),造成了依賴(lài)無(wú)法引入的問(wèn)題。
解決方法
1.手動(dòng)下載依賴(lài)
我第一次遇到這個(gè)問(wèn)題的時(shí)候就是這樣解決的,現(xiàn)在想來(lái)也是非常頭鐵的一種解決方式。
需要什么依賴(lài)自己下載到本地maven倉(cāng)庫(kù)。
由于不可名狀的原因國(guó)內(nèi)開(kāi)發(fā)者不可能訪問(wèn)國(guó)外的maven庫(kù)因此需要使用aliyun提供的maven庫(kù),此處附上網(wǎng)址:https://maven.aliyun.com/mvn/view
2.忽略SSL證書(shū)校驗(yàn)
既然我們沒(méi)有證書(shū),那么就直接忽略證書(shū)校驗(yàn)好了。
IDEA打開(kāi)file->settings->Build,Execution,Development->Build Tools->Runner->VM Options,在輸入框中輸入命令使運(yùn)行時(shí)忽略SLL證書(shū)的校驗(yàn)即可。
附上mvn命令:
-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
3.生成并導(dǎo)入SSL證書(shū)
如果我們沒(méi)有證書(shū),又不想忽略校驗(yàn),那么我們就自己生成一個(gè)SSL證書(shū)即可。
3.1
import javax.net.ssl.*; import java.io.*; import java.security.KeyStore; import java.security.MessageDigest; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class InstallCert { public static void main(String[] args) throws Exception { 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); } } }
新建一個(gè)InstallCert類(lèi)運(yùn)行證書(shū)生成代碼,注意,在執(zhí)行前需要設(shè)置main參數(shù),參數(shù)即為maven地址,如"maven.aliyun.com/nexus/content/groups/public"。
程序運(yùn)行過(guò)程中需要確定,輸入1即可。執(zhí)行完成后會(huì)在程序同級(jí)目錄生成jssecacerts證書(shū),接下來(lái)就是將證書(shū)導(dǎo)入。
PS:如果不想設(shè)置main參數(shù)也可以自定義一個(gè)String[] str變量并賦值為maven倉(cāng)庫(kù)的url,然后把代碼中的args改成str即可。
此處附上證書(shū)生成代碼:
3.2 把生成的證書(shū)文件放至路徑 JAVA_HOME/jre/lib/security下,每個(gè)人的 JAVA_HOME都不一樣,各自對(duì)應(yīng)自己的路徑,最后重啟系統(tǒng)。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot中記錄用戶(hù)系統(tǒng)操作流程
這篇文章主要介紹了如何在Spring?Boot中記錄用戶(hù)系統(tǒng)操作流程,將介紹如何在Spring?Boot中使用AOP(面向切面編程)和日志框架來(lái)實(shí)現(xiàn)用戶(hù)系統(tǒng)操作流程的記錄,需要的朋友可以參考下2023-07-07解決SpringBoot使用devtools導(dǎo)致的類(lèi)型轉(zhuǎn)換異常問(wèn)題
這篇文章主要介紹了解決SpringBoot使用devtools導(dǎo)致的類(lèi)型轉(zhuǎn)換異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。 一起跟隨小編過(guò)來(lái)看看吧2020-08-08基于spring@aspect注解的aop實(shí)現(xiàn)過(guò)程代碼實(shí)例
這篇文章主要介紹了基于spring@aspect注解的aop實(shí)現(xiàn)過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03java Date獲取年月日時(shí)分秒的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java Date獲取年月日時(shí)分秒的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問(wèn)題解決
做研發(fā)的朋友都知道,在項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)碰到ArrayList與數(shù)組類(lèi)型之間的相互轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問(wèn)題解決,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05使用@Autowired可以注入ApplicationContext
這篇文章主要介紹了使用@Autowired可以注入ApplicationContext問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Springboot+WebSocket+Netty實(shí)現(xiàn)在線聊天/群聊系統(tǒng)
這篇文章主要實(shí)現(xiàn)在好友添加、建群、聊天對(duì)話(huà)、群聊功能,使用Java作為后端語(yǔ)言進(jìn)行支持,界面友好,開(kāi)發(fā)簡(jiǎn)單,文章中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-08-08Java使用Tesseract-OCR實(shí)戰(zhàn)教程
本文介紹了如何在Java中使用Tesseract-OCR進(jìn)行文本提取,包括Tesseract-OCR的安裝、中文訓(xùn)練庫(kù)的配置、依賴(lài)庫(kù)的引入以及具體的代碼實(shí)現(xiàn),通過(guò)這個(gè)過(guò)程,我們將演示如何從視頻幀中提取文本2025-02-02