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

相关推荐
SimonKing1 分钟前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean9 分钟前
Jackson View Extension Spring Boot Starter
java·后端
Seven971 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55111 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河11 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程14 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅15 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者16 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺16 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端