zoukankan      html  css  js  c++  java
  • https接口调用 忽略https请求的ssl证书

    import javax.net.ssl.*;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;

    /**
    * @author h
    * @version V1.0
    * @ClassName: SslUtils
    * @Description TODO
    * @modify: {原因} by heamin 2020/6/29 9:52
    */
    public class SslUtils {

    private static void trustAllHttpsCertificates() throws Exception {
    TrustManager[] trustAllCerts = new TrustManager[1];
    TrustManager tm = new miTM();
    trustAllCerts[0] = tm;
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    static class miTM implements TrustManager, X509TrustManager {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }

    public boolean isServerTrusted(X509Certificate[] certs) {
    return true;
    }

    public boolean isClientTrusted(X509Certificate[] certs) {
    return true;
    }

    @Override
    public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
    return;
    }

    @Override
    public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
    return;
    }
    }

    /**
    * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
    *
    * @throws Exception
    */
    public static void ignoreSsl() throws Exception {
    HostnameVerifier hv = new HostnameVerifier() {
    @Override
    public boolean verify(String urlHostName, SSLSession session) {
    System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
    return true;
    }
    };
    trustAllHttpsCertificates();
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }

    }
  • 相关阅读:
    Python集合(set)类型的操作
    3GPP接口定义及相关协议一览
    OSS基本概念介绍
    建造者模式(Builder Pattern)
    组合模式(Composite Pattern)
    观察者模式(Observe Pattern)
    ReentrantLock
    <logger>和<root>
    logback的configuration
    logback的加载过程
  • 原文地址:https://www.cnblogs.com/heamin/p/13608107.html
Copyright © 2011-2022 走看看