1.通过httpCilent调用https遇到问题,用jdk1.6调用tomcat7.0.81 握手失败。
客户端的jdk换成jdk1.8 ,生成证书,把证书导入
keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -validity 36500
keytool -import -v -trustcacerts -alias joaker -file D:/joaker.crt -storepass changeit -keystore "%JAVA_HOME%/jre/lib/security/cacerts
替换jar 包 C:\Program Files\Java\jdk1.8.0_40\jre\lib\security local_policy.jar和 US_export_policy.jar
配置server.xml
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
connectionTimeout="20000"
keystoreFile="conf/tomcat.keystore"
keystorePass="123456"/>
运行,正常了。
但是客户要求必须使用jdk1.6。于是从头开始研究问题。
首先: 修改server.xml配置 支持SSLv2Hello。
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" sslEnabledProtocols="TLSv1,TLSv1.1,TLSv1.2,SSLv2Hello"
connectionTimeout="20000"
keystoreFile="conf/tomcat.keystore"
keystorePass="123456"/>
然后:
keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -validity 36500
keytool -import -v -trustcacerts -alias joaker -file D:/joaker.crt -storepass changeit -keystore "%JAVA_HOME%/jre/lib/security/cacerts
替换jar 包 C:\Program Files\Java\jdk1.8.0_40\jre\lib\security local_policy.jar和 US_export_policy.jar
调用: 报错:javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
继续解决:
下载:bcprov-ext-jdk15on-1.52.jar 和 bcprov-jdk15on-1.52.jar
复制这两个jar包到: $JAVA_HOME/jre/lib/ext
修改:$JAVA_HOME/jre/lib/security/java.security
增加security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
调用正常了。
https调用源码:
public static String doPost(String url,Map<String, String> map,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(), elem.getValue().toString()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
SSLClient源码:
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
//用于进行Https请求的HttpClient
@SuppressWarnings("deprecation")
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super();
// String defaultValue = System.getProperty("https.protocols");
// System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,TLSv1.0,SSLv3");
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// sslSocketFactory = new SSLSocketFactory(ctx,new X509HostnameVerifier(){
// @Override
// public boolean verify(String s, SSLSession sslSession) {
// return true;
// }
//
// @Override
// public void verify(String host, SSLSocket ssl) throws IOException {
//
// }
//
// @Override
// public void verify(String host, X509Certificate cert) throws SSLException {
//
// }
//
// @Override
// public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
//
// }
// });
// System.setProperty("javax.net.debug","ssl");
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, sslSocketFactory));
}
}