热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PKIX:unabletofindvalidcertificationpathtorequestedtarget的问题

转https:blog.csdn.netfaye0412articledetails6883879问题的根本是:缺少安全证书时出现的异常。解决问题方法

 

转   

https://blog.csdn.net/faye0412/article/details/6883879

问题的根本是:

缺少安全证书时出现的异常。

解决问题方法:

将你要访问的webservice/url....的安全认证证书导入到客户端即可。

 

类   

InstallCert

package io.renren;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;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 [: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 > 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);}}}

 

 

类:   

MySecureProtocolSocketFactory

package io.renren;import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {private SSLContext sslContext = null;/*** Constructor for MySecureProtocolSocketFactory.*/public MySecureProtocolSocketFactory() {}/**** @return*/private static SSLContext createEasySSLContext() {try {SSLContext context = SSLContext.getInstance("SSL");context.init(null, new TrustManager[] { new MyX509TrustManager() },null);return context;} catch (Exception e) {throw new HttpClientError(e.toString());}}/**** @return*/private SSLContext getSSLContext() {if (this.sslContext == null) {this.sslContext = createEasySSLContext();}return this.sslContext;}/** (non-Javadoc)** @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,* int, java.net.InetAddress, int)*/public Socket createSocket(String host, int port, InetAddress clientHost,int clientPort) throws IOException, UnknownHostException {return getSSLContext().getSocketFactory().createSocket(host, port,clientHost, clientPort);}/** (non-Javadoc)** @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,* int, java.net.InetAddress, int,* org.apache.commons.httpclient.params.HttpConnectionParams)*/public Socket createSocket(final String host, final int port,final InetAddress localAddress, final int localPort,final HttpConnectionParams params) throws IOException,UnknownHostException, ConnectTimeoutException {if (params == null) {throw new IllegalArgumentException("Parameters may not be null");}int timeout = params.getConnectionTimeout();if (timeout == 0) {return createSocket(host, port, localAddress, localPort);} else {return ControllerThreadSocketFactory.createSocket(this, host, port,localAddress, localPort, timeout);}}/** (non-Javadoc)** @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)*/public Socket createSocket(String host, int port) throws IOException,UnknownHostException {return getSSLContext().getSocketFactory().createSocket(host, port);}/** (non-Javadoc)** @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)*/public Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException {return getSSLContext().getSocketFactory().createSocket(socket, host,port, autoClose);}
}

 

类:  

X509TrustManager

 

package io.renren;import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;import javax.net.ssl.X509TrustManager;public class MyX509TrustManager implements X509TrustManager {/* (non-Javadoc)* @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[], java.lang.String)*/public void checkClientTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {}/* (non-Javadoc)* @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[], java.lang.String)*/public void checkServerTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {}/* (non-Javadoc)* @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()*/public X509Certificate[] getAcceptedIssuers() {return null;}}

 

 

如果编译不了 。 就应该到当前目录的上一个目录进行编译运行。不用带https://

 

如:  F:\work\gitlab\renren-security\renren-admin\src\test\java> java io/renren/InstallCert wssb6.szsi.gov.cn

 

 

输入1,回车,然后会在当前的目录下产生一个名为“ssecacerts”的证书。

 

将证书拷贝到$JAVA_HOME/jre/lib/security目录下,或者通过以下方式:
System.setProperty("javax.net.ssl.trustStore", "你的jssecacerts证书路径"); //在connect前设置

 

static {try {System.setProperty("javax.net.ssl.trustStore", "F:\\work\\gitlab\\renren-security\\renren-admin\\src\\test\\java\\jssecacerts");ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();Protocol.registerProtocol("https", new Protocol("https", fcty, 443));HttpClient httpClient = new HttpClient();res =Jsoup.connect(logURL).data("user","1111","password","222220","isIE","1","type","1").method(Connection.Method.POST).execute();} catch (IOException e) {e.printStackTrace();}}


注意:因为是静态加载,所以要重新启动你的Web Server,证书才能生效。

 

 

 

 


推荐阅读
author-avatar
葬心xz
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有