zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    EasyUI左边树菜单和datagrid分页
    Linux上安装Redis教程
    TreeMap和TreeSet的区别与联系
    将Map<String, List<Map<String,Object>>>进行排序
    Linux系统安装JDK和Tomcat
    点击添加按钮,使用ajax动态添加一行和移除一行,并且序号重新排序和数据不重复操作判断
    23种设计模式汇总整理
    SSH架构BaseDao实现
    双击Table表格td变成text修改内容
    用户找回密码功能JS验证邮箱通过点击下一步隐藏邮箱输入框并修改下一步按钮的ID
  • 原文地址:https://www.cnblogs.com/fengjian/p/3534410.html
Copyright © 2011-2022 走看看