Java代码中忽略HTTPS请求中的证书

请注意,忽略证书验证存在安全风险,因为这使得您的应用程序容易受到中间人攻击。在生产环境中,请谨慎使用此方法,并确保您的应用程序的安全性。

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

相关推荐
JAVA面经实录91711 小时前
Java企业级工程化·终极完整版背诵手册(无遗漏、全覆盖、面试+落地通用)
java·开发语言·面试
Flying pigs~~12 小时前
RAG智慧问答项目
数据库·人工智能·缓存·微调·知识库·rag
misL NITL13 小时前
mysql之如何获知版本
数据库·mysql
许彰午13 小时前
CacheSQL(二):主从复制——OpLog 环形缓冲区与故障自动恢复
java·数据库·缓存
2401_8323655214 小时前
JavaScript中rest参数(...args)取代arguments的优势
jvm·数据库·python
Bat U14 小时前
JavaEE|多线程初阶(七)
java·开发语言
2301_7796224114 小时前
Go语言怎么用信号量控制并发_Go语言semaphore信号量教程【入门】
jvm·数据库·python
2301_7662834414 小时前
c++如何将控制台输出保存到文件_cout重定向到txt【详解】
jvm·数据库·python
北极的冰箱14 小时前
MySQL Ver 8.0.41 for macos14.7密码遗忘
数据库·mysql
XDH_CS15 小时前
MySQL 8.0 安装与 MySQL Workbench 使用全流程(超详细教程)
开发语言·数据库·mysql