How to disable certificate validations in the Java HTTP Client

这篇具有很好参考价值的文章主要介绍了How to disable certificate validations in the Java HTTP Client。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Java 11 introduced the HTTP Client, an API that made it easier to send HTTP requests with vanilla Java.

By default, it throws an exception if there are certificate path or hostname verification errors in the request.

Let’s see how to bypass certificate validations for cases where this is really necessary.

Disabling all certificate verifications for a specific client

To ignore both certificate path and hostname verifications, create an X509ExtendedTrustManager extension that doesn't do any verification and use it to init an SSLContext for an HttpClient:

var trustManager = new X509ExtendedTrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[]{};
    }

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) {
    }

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
    }

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
    }
};
var sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom());

var client = HttpClient.newBuilder()
        .sslContext(sslContext)
        .build();

With this solution, only that client with that custom SSLContext specified will allow insecure requests. So in many cases this is the best option.

You can use the example URLs https://expired.badssl.com/ and https://wrong.host.badssl.com/ to test:

var expiredRequest = HttpRequest.newBuilder()
        .uri(URI.create("https://expired.badssl.com/"))
        .build();

var wrongHostRequest = HttpRequest.newBuilder()
        .uri(URI.create("https://wrong.host.badssl.com/"))
        .build();

client.send(expiredRequest, BodyHandlers.discarding());
client.send(wrongHostRequest, BodyHandlers.discarding());

Errors you would get

Without disabling verification, this error would occur for an expired SSL/TLS certificate:

javax.net.ssl.SSLHandshakeException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed

...

Caused by: java.security.cert.CertificateExpiredException: NotAfter: Sun Apr 12 20:59:59 BRT 2015

And for a wrong hostname:

javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching wrong.host.badssl.com found.

...

Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching wrong.host.badssl.com found.

Disabling hostname verification by system property

You can set the jdk.internal.httpclient.disableHostnameVerification system property to "true" to disable only hostname verification, as shown in the Javadoc.

This solution isn’t applied to certificate path verification, so an expired certificate would still cause an exception. And it has the disadvantage of disabling hostname verification for requests from all clients.

Disabling only certificate path verification

If you create an X509TrustManager implementation (instead of an X509ExtendedTrustManager extension) that doesn't do verifications and use it on a client, it will ignore only the certificate path verification:

var sslContext = SSLContext.getInstance("TLS");
var trustManager = new X509TrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[]{};
    }

    @Override
    public void checkClientTrusted(X509Certificate[] certs, String authType) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] certs, String authType) {
    }
};
sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom());

var client = HttpClient.newBuilder()
        .sslContext(sslContext)
        .build();
var request = HttpRequest.newBuilder()
        .uri(URI.create("https://expired.badssl.com/"))
        .build();
client.send(request, BodyHandlers.discarding());

So this solution isn’t applied to hostname verification.

Conclusion

To disable certificate verification, the best option in most cases is to use an X509ExtendedTrustManager extension that doesn't do any verification, as this will bypass both certificate path and hostname verifications and will only apply to the specified client.文章来源地址https://www.toymoban.com/news/detail-803107.html

到了这里,关于How to disable certificate validations in the Java HTTP Client的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • K8S异常之Unable to connect to the server: x509: certificate has expired or is not yet valid

    2.1 处理步骤 2.2 处理步骤详细情况 如上,发现很多证书都是 invalid 的状态,接着更新证书: 如下,更新证书后,证书过期时间已经更新为 365d 3.1 再次查看kubectl get node,发现有新的错误: error: You must be logged in to the server (Unauthorized) 3.2 上述错误解决方案 备份配置文件 cp -rp

    2024年02月03日
    浏览(68)
  • How to fix the problem that Raspberry Pi cannot use the root user for SSH login All In One

    如何修复树莓派无法使用 root 用户进行 SSH 登录的问题 修改树莓派默认的 pi 用户名和密码后,需要使用 root 用户进行 SSH 登录; 对 pi/home 文件夹进行 备份 ,复制到新用户下 xgqfrms/home 备份后,要 删除 pi 用户, 必须切换到其他用户,毕竟 pi 用户不能自己删除自己呀!⚠️ 给

    2024年02月07日
    浏览(63)
  • How to boot the Raspberry Pi system from a USB Mass Storage Device All In One

    如何从 USB 启动树莓派引导系统 / 如何从 USB 大容量存储设备启动 Raspberry Pi 系统 First Stage Bootloader Second Stage Bootloader https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-4-boot-flow https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-4-bootloader-configuration BO

    2024年02月06日
    浏览(57)
  • Java中合并两个数组的4种方法(How to Merge Two Arrays in Java)

    int[] arr1={1, 2, 3, 4, 5, 6}; //first array int[] arr2={7, 8, 9, 0}; //second array int[] arr3={1, 2, 3, 4, 5, 6, 7, 8, 9, 0} //resultant array There are following ways to merge two arrays: 1.Java arraycopy() method 2.Without using arraycopy() method 3.Java Collections 4.Java Stream API Java arraycopy() is the method of System class which belongs to java.la

    2024年02月11日
    浏览(42)
  • unable to find valid certification path to requested target

    调用https接口时出现该异常, Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target   原因是可以看上图,因为本地没有目标服务器证书导致。解决此方法的两种方案,1.在运行

    2024年02月02日
    浏览(59)
  • 报错 unable to find valid certification path to requested target executing

    提示信息: 审核失败!sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target executing POST 。。。 。。。 出现原因 这个问题的根本原因是你安装JDK时,Javajar 1.8.0_141libext里面缺少了一

    2024年02月03日
    浏览(54)
  • Maven 私服 unable to find valid certification path to requested target 错误

    你遇到的错误信息 “sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target” 表明在 SSL/TLS 握手过程中,证书路径验证失败。这通常是由于缺少或不受信任的证书导致的,Maven 无法与远

    2024年02月08日
    浏览(69)
  • https请求报错unable to find valid certification path to requested target解决

            在Java项目中请求HTTPS时,可能会遇到 \\\"unable to find valid certification path to requested target\\\" 错误。这个错误通常是由于SSL证书问题引起的。要解决此问题,可以尝试以下方法 1.忽略SSL验证         OkHttpClient封装请求         HttpURLConnection请求         RestTemplate请求

    2024年02月08日
    浏览(60)
  • 彻底解决:SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法

    请求12306系统查票。之前正常的,现在提示这样的错误: Exception in thread \\\"main\\\" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 如下图:  导致原因:由

    2024年02月05日
    浏览(59)
  • how to read dwarf in linux

    Makefile demo.c

    2024年02月16日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包