unable to find valid certification path to requested target

[WARNING] Could not transfer metadata org.sonarsource.sonar-packaging-maven-plugin:sonar-packaging-maven-plugin/maven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

使用技术:maven   仓库:阿里云

如果报上边那个错误,有多种情况,最直接的方法就是换仓库,可以换国内的或国外的,

国内的下载快,但第三方可能存在验证问题;国外的下载较慢,其实也不是很慢,推荐国外使用maven官方的。

首先说一下我最后的解决方法。

我的方法一:

打开idea---》setting---》runnner

在vm options中添加:-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true     中间用空格隔开

根据网上搜查,大致还有以下几个方案,不过我尝试都没效果

1.删除本地仓库依赖(我删除了本地仓库,没有效果,反而使项目依赖全都没有,一堆爆红)

2.判斷是连到 repo.maven.apache.org 的时候凭证未信任.

  1 /*
  2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
  3  *
  4  * Redistribution and use in source and binary forms, with or without
  5  * modification, are permitted provided that the following conditions
  6  * are met:
  7  *
  8  *   - Redistributions of source code must retain the above copyright
  9  *     notice, this list of conditions and the following disclaimer.
 10  *
 11  *   - Redistributions in binary form must reproduce the above copyright
 12  *     notice, this list of conditions and the following disclaimer in the
 13  *     documentation and/or other materials provided with the distribution.
 14  *
 15  *   - Neither the name of Sun Microsystems nor the names of its
 16  *     contributors may be used to endorse or promote products derived
 17  *     from this software without specific prior written permission.
 18  *
 19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30  */
 31 /**
 32  * Originally from:
 33  * http://blogs.sun.com/andreas/resource/InstallCert.java
 34  * Use:
 35  * java InstallCert hostname
 36  * Example:
 37  *% java InstallCert ecc.fedora.redhat.com
 38  */
 39
 40 import javax.net.ssl.*;
 41 import java.io.*;
 42 import java.net.Socket;
 43
 44 import java.security.KeyStore;
 45 import java.security.MessageDigest;
 46 import java.security.cert.CertificateException;
 47 import java.security.cert.X509Certificate;
 48
 49 /**
 50  * Class used to add the server‘s certificate to the KeyStore
 51  * with your trusted certificates.
 52  */
 53 public class InstallCert {
 54
 55     public static void main(String[] args) throws Exception {
 56         String host;
 57         int port;
 58         char[] passphrase;
 59         if ((args.length == 1) || (args.length == 2)) {
 60             String[] c = args[0].split(":");
 61             host = c[0];
 62             port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
 63             String p = (args.length == 1) ? "changeit" : args[1];
 64             passphrase = p.toCharArray();
 65         } else {
 66             System.out.println("Usage: java InstallCert <host>[:port] [passphrase]");
 67             return;
 68         }
 69
 70         File file = new File("jssecacerts");
 71         if (file.isFile() == false) {
 72             char SEP = File.separatorChar;
 73             File dir = new File(System.getProperty("java.home") + SEP
 74                     + "lib" + SEP + "security");
 75             file = new File(dir, "jssecacerts");
 76             if (file.isFile() == false) {
 77                 file = new File(dir, "cacerts");
 78             }
 79         }
 80         System.out.println("Loading KeyStore " + file + "...");
 81         InputStream in = new FileInputStream(file);
 82         KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
 83         ks.load(in, passphrase);
 84         in.close();
 85
 86         SSLContext context = SSLContext.getInstance("TLS");
 87         TrustManagerFactory tmf =
 88                 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
 89         tmf.init(ks);
 90         X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
 91         SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
 92         context.init(null, new TrustManager[]{tm}, null);
 93         SSLSocketFactory factory = context.getSocketFactory();
 94
 95         System.out.println("Opening connection to " + host + ":" + port + "...");
 96         SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
 97         socket.setSoTimeout(10000);
 98         try {
 99             System.out.println("Starting SSL handshake...");
100             socket.startHandshake();
101             socket.close();
102             System.out.println();
103             System.out.println("No errors, certificate is already trusted");
104         } catch (SSLException e) {
105             System.out.println();
106             e.printStackTrace(System.out);
107         }
108
109         X509Certificate[] chain = tm.chain;
110         if (chain == null) {
111             System.out.println("Could not obtain server certificate chain");
112             return;
113         }
114
115         BufferedReader reader =
116                 new BufferedReader(new InputStreamReader(System.in));
117
118         System.out.println();
119         System.out.println("Server sent " + chain.length + " certificate(s):");
120         System.out.println();
121         MessageDigest sha1 = MessageDigest.getInstance("SHA1");
122         MessageDigest md5 = MessageDigest.getInstance("MD5");
123         for (int i = 0; i < chain.length; i++) {
124             X509Certificate cert = chain[i];
125             System.out.println
126                     (" " + (i + 1) + " Subject " + cert.getSubjectDN());
127             System.out.println("   Issuer  " + cert.getIssuerDN());
128             sha1.update(cert.getEncoded());
129             System.out.println("   sha1    " + toHexString(sha1.digest()));
130             md5.update(cert.getEncoded());
131             System.out.println("   md5     " + toHexString(md5.digest()));
132             System.out.println();
133         }
134
135         System.out.println("Enter certificate to add to trusted keystore or ‘q‘ to quit: [1]");
136         String line = reader.readLine().trim();
137         int k;
138         try {
139             k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
140         } catch (NumberFormatException e) {
141             System.out.println("KeyStore not changed");
142             return;
143         }
144
145         X509Certificate cert = chain[k];
146         String alias = host + "-" + (k + 1);
147         ks.setCertificateEntry(alias, cert);
148
149         OutputStream out = new FileOutputStream("jssecacerts");
150         ks.store(out, passphrase);
151         out.close();
152
153         System.out.println();
154         System.out.println(cert);
155         System.out.println();
156         System.out.println
157                 ("Added certificate to keystore ‘jssecacerts‘ using alias ‘"
158                         + alias + "‘");
159     }
160
161     private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
162
163     private static String toHexString(byte[] bytes) {
164         StringBuilder sb = new StringBuilder(bytes.length * 3);
165         for (int b : bytes) {
166             b &= 0xff;
167             sb.append(HEXDIGITS[b >> 4]);
168             sb.append(HEXDIGITS[b & 15]);
169             sb.append(‘ ‘);
170         }
171         return sb.toString();
172     }
173
174     private static class SavingTrustManager implements X509TrustManager {
175
176         private final X509TrustManager tm;
177         private X509Certificate[] chain;
178
179         SavingTrustManager(X509TrustManager tm) {
180             this.tm = tm;
181         }
182
183         public X509Certificate[] getAcceptedIssuers() {
184
185         /**
186          * This change has been done due to the following resolution advised for Java 1.7+
187         http://infposs.blogspot.kr/2013/06/installcert-and-java-7.html
188                 **/
189
190         return new X509Certificate[0];
191             //throw new UnsupportedOperationException();
192         }
193
194         public void checkClientTrusted(X509Certificate[] chain, String authType)
195                 throws CertificateException {
196             throw new UnsupportedOperationException();
197         }
198
199         public void checkServerTrusted(X509Certificate[] chain, String authType)
200                 throws CertificateException {
201             this.chain = chain;
202             tm.checkServerTrusted(chain, authType);
203         }
204     }
205 }

打包
$ javac InstallCert.java

執行
$ java InstallCert repo.maven.apache.org

他會問你
Enter certificate to add to trusted keystore or ‘q‘ to quit: [1]

輸入 1 ,之後會在執行目錄下生成 jssecacerts
將“jssecacerts”的文件放入JAVA_HOME/jre/lib/security目錄下
再進行maven編譯就可以了

原文地址:https://www.cnblogs.com/268lwc/p/12432178.html

时间: 2024-10-14 03:26:48

unable to find valid certification path to requested target的相关文章

Java调用https服务报错unable to find valid certification path to requested target的解决方法

我们网站要进行https改造,配置上购买的SSL证书后,浏览器访问正常,但是写了个java代码用httpcomponents调用https rest接口时报错: Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath

解决PKIX:unable to find valid certification path to requested target 的问题

问题的根本是: 缺少安全证书时出现的异常. 解决问题方法: 将你要访问的webservice/url....的安全认证证书导入到客户端即可. 以下是获取安全证书的一种方法,通过以下程序获取安全证书: /* * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification,

解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target

最近在写java的一个服务,需要给远程服务器发送post请求,认证方式为Basic Authentication,在请求过程中出现了 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target的错误,于是开始搜索并得到解决, 下面总结一下解决过程: 我们要做的就是将

解决Android studio :Error:Cause: unable to find valid certification path to requested target

解决Android studio :Error:Cause: unable to find valid certification path to requested target ————————————记一个倒霉孩子的一周的挣扎 最近更新Android studio至3.5.1,然后出现了Error:Cause: unable to find valid certification path to requested target这个报错,总之就是gradle更新时总有一个.pom文件或者j

IDEA unable to find valid certification path to requested target

一.报错 Could not transfer artifact org.apache.maven.plugins:maven-install-plugin:pom:2.4 from/to alimaven (https://maven.aliyun.com/repository/central): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpat

MAVEN ERROR: unable to find valid certification path to requested target 解决办法

第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central</id> <url>http://repo1.maven.org/maven2</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>

解决PKIX问题:unable to find valid certification path to requested target

/* * 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

异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

前几天用JSOUP写爬虫Demo时,遇到这个异常 百度了一番原来是因为目标站点启用了HTTPS  而缺少安全证书时出现的异常,大概解决办法有2种: 1. 手动导入安全证书(嫌麻烦 没使用); 2. 忽略证书验证. 相对于来说简单一点,在发起请求前调用这个方法,问题解决. ``` java // 包不要导错了 import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.Certificat

mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ

mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 解决方案: The fact is that your maven plugin try