• Ignoring HTTPS certificates


    Our Development setups won't have valid or trusted certificates. When do you want test our webserver code over HTTPS, we need to handle these certificates with special code.

    The common approach is to import these HTTPS certificates into JDK cacerts or override the trust store:
    In Java:

    System.setProperty("javax.net.ssl.trustStore","clientTrustStore.key");
    System.setProperty("javax.net.ssl.trustStorePassword","qwerty");

    If you are working with many such systems or test setups in LAN or internet, it is time taking to import these certificates in our trust stores. So we can allow a setting in our code to ignore these certificates with below code snippets.

    HostnameVerifier hostNameVerifier = new HostnameVerifier()
    {
     
        public boolean verify(String s, SSLSession sslSession)
        {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hostNameVerifier);
     
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager()
            {
                public java.security.cert.X509Certificate[] getAcceptedIssuers()
                {
                    return null;
                }
     
                public void checkClientTrusted(X509Certificate[] certs, String authType)
                {
                }
     
                public void checkServerTrusted(X509Certificate[] certs, String authType)
                {
                }
            }
    };
     
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        logger.fine("Socket factory initialized");
        System.out.println("Ignoring server certificates");
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed initializing socket factory to ignore certificates.", e);
    }

    In Java using Apache Commons HTTP Util: 

     Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443);
            Protocol.registerProtocol("https", easyhttps);

    In PHP using PHP CURL: 

    //open connection
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
     
    // ignore certs
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
  • 相关阅读:
    static关键字的定义与使用
    String类练习统计一个字符串中大小写字母及数字字符个数
    Java中String类的常用方法
    String类的特点和使用步骤
    HTB 渗透测试笔记-Lame
    消息认证-数字签名-报文鉴别-到底是什么
    docker pull 太慢了解决办法
    彻底解决Mac无线网络故障和网速慢的问题
    彻底-有效-解决-Github下载太慢的问题
    Linux中的docker报错 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  • 原文地址:https://www.cnblogs.com/fengjian/p/3534410.html
Copyright © 2020-2023  润新知