Https 调用遇到的问题以及解决方法

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));  
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值