updated
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package com.mgm.mcrm.common.httpclient;
|
||||
|
||||
import com.mgm.mcrm.common.constants.SysCommon;
|
||||
import com.mgm.mcrm.common.services.ConfigurableTrustHandler;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.hc.core5.ssl.SSLContexts;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import javax.net.ssl.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -18,75 +17,36 @@ import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
|
||||
/**
|
||||
* SSL 支持类(基于 HttpClient 5)
|
||||
*
|
||||
* 提供:
|
||||
* - 信任所有证书的 SSLSocketFactory
|
||||
* - 信任所有证书的 SSLConnectionSocketFactory
|
||||
*
|
||||
* 注意:默认实现信任所有证书,仅适用于内部系统或测试环境。
|
||||
*
|
||||
* @author Anka
|
||||
* @version 2.0 (client5)
|
||||
*/
|
||||
public class HttpSslSupport {
|
||||
private static final SSLHandler SIMPLE_VERIFIER = new SSLHandler();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static boolean skipValidation=true;
|
||||
|
||||
private static ConfigurableTrustHandler trustHandler=null;
|
||||
|
||||
private static SSLSocketFactory sslFactory;
|
||||
// 注意:SSLConnectionSocketFactory 在 HttpClient 5 中被标记为 deprecated,
|
||||
// 但这是目前唯一可用的方式,新的 API 尚未稳定
|
||||
@SuppressWarnings("deprecation")
|
||||
private static SSLConnectionSocketFactory sslConnFactory;
|
||||
|
||||
private static final HttpSslSupport INSTANCE = new HttpSslSupport();
|
||||
|
||||
private SSLContext sc;
|
||||
|
||||
public static HttpSslSupport getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static HttpSslSupport create() {
|
||||
return new HttpSslSupport();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写 X509TrustManager 和 HostnameVerifier,信任所有证书与主机名。
|
||||
* 仅推荐在内部环境或调试场景使用。
|
||||
*/
|
||||
private static class SSLHandler implements X509TrustManager, HostnameVerifier {
|
||||
|
||||
@Override
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return new java.security.cert.X509Certificate[]{};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
|
||||
String authType) throws java.security.cert.CertificateException {
|
||||
// 信任所有服务器证书
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
|
||||
String authType) throws java.security.cert.CertificateException {
|
||||
// 信任所有客户端证书
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(String paramString, SSLSession paramSSLSession) {
|
||||
// 信任所有主机名
|
||||
return true;
|
||||
public static ConfigurableTrustHandler getTrustHandler(){
|
||||
if(trustHandler==null){
|
||||
trustHandler =new ConfigurableTrustHandler(skipValidation, SysCommon.getDefaultTrustManager(), SysCommon.getHttpApiServers());
|
||||
}
|
||||
return trustHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认 HostnameVerifier(信任所有主机名)
|
||||
*/
|
||||
public static HostnameVerifier getVerifier() {
|
||||
return SIMPLE_VERIFIER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取信任所有证书的 {@link SSLSocketFactory}
|
||||
@@ -97,7 +57,9 @@ public class HttpSslSupport {
|
||||
}
|
||||
try {
|
||||
SSLContext context = getSSLContext(sslpv);
|
||||
context.init(null, new TrustManager[]{SIMPLE_VERIFIER}, null);
|
||||
context.init(null, new TrustManager[]{
|
||||
getTrustHandler()
|
||||
}, null);
|
||||
sslFactory = context.getSocketFactory();
|
||||
return sslFactory;
|
||||
} catch (KeyManagementException e) {
|
||||
@@ -118,8 +80,11 @@ public class HttpSslSupport {
|
||||
}
|
||||
try {
|
||||
SSLContext context = getSSLContext(sslpv);
|
||||
context.init(null, new TrustManager[]{SIMPLE_VERIFIER}, new java.security.SecureRandom());
|
||||
sslConnFactory = new SSLConnectionSocketFactory(context, SIMPLE_VERIFIER);
|
||||
ConfigurableTrustHandler trustHandler = getTrustHandler();
|
||||
context.init(null, new TrustManager[]{
|
||||
trustHandler
|
||||
}, new java.security.SecureRandom());
|
||||
sslConnFactory = new SSLConnectionSocketFactory(context, trustHandler);
|
||||
return sslConnFactory;
|
||||
} catch (KeyManagementException e) {
|
||||
throw new RuntimeException("初始化 SSLConnectionSocketFactory 失败", e);
|
||||
|
||||
Reference in New Issue
Block a user