zoukankan      html  css  js  c++  java
  • 建立HttpsConnection

    1建立HttpConnection,这种连接比较简单,但是是不安全的,网上例子比较多,现在主要说说如果建立HttpsConnection,这种连接时通过SSL协议加密,相对更安全,一般使用这种连接传输用户名密码,等重要信息的,下面看代码:

    public HttpsURLConnection getHttpsConnection(){
            try{
                TrustManager[] tm = { new MyX509TrustManager() };
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, tm, new java.security.SecureRandom());
                SSLSocketFactory ssf = sslContext.getSocketFactory();
                System.out.println(TAG+" getHttpsConnection serverUrl="+serverUrl);
                URL myURL = new URL(serverUrl);
                HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
                httpsConn.setSSLSocketFactory(ssf);
                httpsConn.setRequestProperty("accept", "*/*");
                httpsConn.setRequestProperty("connection", "Keep-Alive");
                httpsConn.setRequestMethod("POST");   
                httpsConn.setDoOutput(true);
                httpsConn.setDoInput(true);
                httpsConn.connect();
                return httpsConn;
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
    
        }

    在上面的代码中比较重要的是MyX509TrustManager这个类,这个类是安全的保障,可以是用默认的,但是一般我们都需要对证书进行自定义,因此需要继承下,下面看代码:

    package login;
    import java.io.FileInputStream;
    import java.security.KeyStore;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.TrustManagerFactory;
    import javax.net.ssl.X509TrustManager;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    public class MyX509TrustManager implements X509TrustManager {
        //private final String CER_NAME = "D:\Apache_Software_Foundation\Tomcat_6.0\cas.keystore";
        private final String CER_NAME ="D:\apache-tomcat-7.0.42\cas.keystore";
        private final String CER_PASSWORD = "changeit";
        private final Log logger = LogFactory.getLog(getClass());
    
        /*
         * The default X509TrustManager returned by SunX509.  We'll delegate
         * decisions to it, and fall back to the logic in this class if the
         * default X509TrustManager doesn't trust it.
         */
        X509TrustManager sunJSSEX509TrustManager;
    
        MyX509TrustManager() throws Exception {
            // create a "default" JSSE X509TrustManager. 
           KeyStore ks = KeyStore.getInstance("JKS");
           FileInputStream fis = new FileInputStream(CER_NAME);
           ks.load(fis,CER_PASSWORD.toCharArray());
           TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
           tmf.init(ks);
           TrustManager tms [] = tmf.getTrustManagers();
            for (int i = 0; i < tms.length; i++) {
                if (tms[i] instanceof X509TrustManager) {
                    sunJSSEX509TrustManager = (X509TrustManager) tms[i];
                    return;
                }
            }
            throw new Exception("liqingguo Couldn't initialize");
        }
    
        /*
         * Delegate to the default trust manager.
         */
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            try {
                sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
            } catch (CertificateException excep) {
                // do any special handling here, or rethrow exception. 
            }
        }
    
        /*
         * Delegate to the default trust manager.
         */
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            try {
                sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
            } catch (CertificateException excep) {
                /*
                 * Possibly pop up a dialog box asking whether to trust the
                 * cert chain.
                 */
            }
        }
    
        /*
         * Merely pass this through.
         */
        public X509Certificate[] getAcceptedIssuers() {
            return sunJSSEX509TrustManager.getAcceptedIssuers();
        }
    }
  • 相关阅读:
    ValueError: Layer conv2d_1 was called with an input that isn't a symbolic tensor. Received type: <class 'tuple'>. Full input: [(600, 600, 3)]. All inputs to the layer should be tensors.
    OSError: `pydot` failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.该错误的解决
    numpy操作1--任务一:单通道图像转三通道理解/任务二:按axis=0或axis=1或axis=2(轴)堆叠(stack)
    探究灰度图像对目标检测测试结果影响----RGB转灰度图像、灰度图像扩充成三通道
    git项目上传github
    jupyter中,ipynb文件转pdf文件
    面试-操作系统
    数据库-面试
    面试—计算机网络
    PE文件结构
  • 原文地址:https://www.cnblogs.com/qgli/p/3553868.html
Copyright © 2011-2022 走看看