转
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
类:
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,证书才能生效。