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

相关推荐
wuhuhuan几秒前
JMeter 连接数据库并取值(JDBC 实战)
数据库·jmeter
10mAh21 分钟前
【Java】ArrayList 遍历删除为什么报错?三种写法与漏删复现
java·开发语言·windows
thefool11226636 分钟前
多线程 4
java
小羊没烦恼!1 小时前
OS X 上使用.NET开发应用程序
开发语言·前端·数据库·人工智能·c#·策略模式
Rain的Java大神之路1 小时前
[特殊字符]8年Java老兵转型AI Agent:90%的人挂在同一个坑,根本不用学Python!(万字实战复盘,建议收藏)
java·数据库·人工智能·后端·python·spring·ai编程
高级程序源1 小时前
springboot大学生体测系统72170-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·django·课程设计
IT研究室1 小时前
最新计算机毕业设计选题推荐-基于spring boot的文学名著分享系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
码途行者1 小时前
缓存一致性难题:Redis 和数据库如何做到数据一致?
数据库·redis
denlaku1 小时前
JDK 13 新特性详解
java·开发语言
秋饼1 小时前
Java 后端企业级 LLM 网关实战:Spring Cloud Gateway 构建统一大模型接入层
java·ai·技术分享·后端开发