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

相关推荐
haaaaaaarry4 分钟前
Element Plus常见基础组件(一)
java·前端·javascript·vue.js
歌者長門9 分钟前
做题笔记:某大讯飞真题28道
java·数据结构·算法
Savvy..12 分钟前
Day05 Maven
java·junit·maven·注解
weixin_4196583123 分钟前
MySQL的JDBC编程
数据库·mysql
Goboy33 分钟前
我是如何设计出高性能群消息已读回执系统的
java·后端·架构
JavaLearnerZGQ33 分钟前
Docker部署Nacos
数据库·docker·容器
阳光明媚sunny1 小时前
结构型设计模式
java·设计模式
码luffyliu1 小时前
Java:高频面试知识分享1
java·八股文
小信丶1 小时前
Spring Boot 简单接口角色授权检查实现
java·spring boot·后端
IT乐手1 小时前
java 或 安卓项目中耗时统计工具类
android·java