请注意,忽略证书验证存在安全风险,因为这使得您的应用程序容易受到中间人攻击。在生产环境中,请谨慎使用此方法,并确保您的应用程序的安全性。
java
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class IgnoreSSL {
public static void main(String[] args) throws Exception {
// 创建信任所有证书的TrustManager
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
// 获取SSLContext实例
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
// 设置默认的HostnameVerifier,接受所有主机名
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
// 设置默认的SSLSocketFactory
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 发起HTTPS请求
// Your HTTPS request code here
}
}
另一种解决方法:
Nginx做转发
location /providerAuth{
proxy pass https://xxxx.com; #目标域名
access log auth.log json;
}
用于处理以/providerAuth开头的请求,将访问日志记录到auth.log文件中,日志格式为json