国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

How to disable certificate validations in the Java HTTP Client

這篇具有很好參考價(jià)值的文章主要介紹了How to disable certificate validations in the Java HTTP Client。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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.文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-803107.html

到了這里,關(guān)于How to disable certificate validations in the Java HTTP Client的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

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

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

    2.1 處理步驟 2.2 處理步驟詳細(xì)情況 如上,發(fā)現(xiàn)很多證書(shū)都是 invalid 的狀態(tài),接著更新證書(shū): 如下,更新證書(shū)后,證書(shū)過(guò)期時(shí)間已經(jīng)更新為 365d 3.1 再次查看kubectl get node,發(fā)現(xiàn)有新的錯(cuò)誤: error: You must be logged in to the server (Unauthorized) 3.2 上述錯(cuò)誤解決方案 備份配置文件 cp -rp

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

    How to fix the problem that Raspberry Pi cannot use the root user for SSH login All In One

    如何修復(fù)樹(shù)莓派無(wú)法使用 root 用戶進(jìn)行 SSH 登錄的問(wèn)題 修改樹(shù)莓派默認(rèn)的 pi 用戶名和密碼后,需要使用 root 用戶進(jìn)行 SSH 登錄; 對(duì) pi/home 文件夾進(jìn)行 備份 ,復(fù)制到新用戶下 xgqfrms/home 備份后,要 刪除 pi 用戶, 必須切換到其他用戶,畢竟 pi 用戶不能自己刪除自己呀!?? 給

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

    How to boot the Raspberry Pi system from a USB Mass Storage Device All In One

    如何從 USB 啟動(dòng)樹(shù)莓派引導(dǎo)系統(tǒng) / 如何從 USB 大容量存儲(chǔ)設(shè)備啟動(dòng) Raspberry Pi 系統(tǒng) 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日
    瀏覽(65)
  • Java中合并兩個(gè)數(shù)組的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日
    瀏覽(24)
  • unable to find valid certification path to requested target

    unable to find valid certification path to requested target

    調(diào)用https接口時(shí)出現(xiàn)該異常, Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target ? 原因是可以看上圖,因?yàn)楸镜貨](méi)有目標(biāo)服務(wù)器證書(shū)導(dǎo)致。解決此方法的兩種方案,1.在運(yùn)行

    2024年02月02日
    瀏覽(82)
  • 報(bào)錯(cuò) unable to find valid certification path to requested target executing

    提示信息: 審核失?。un.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target executing POST 。。。 。。。 出現(xiàn)原因 這個(gè)問(wèn)題的根本原因是你安裝JDK時(shí),Javajar 1.8.0_141libext里面缺少了一

    2024年02月03日
    瀏覽(93)
  • Maven 私服 unable to find valid certification path to requested target 錯(cuò)誤

    你遇到的錯(cuò)誤信息 “sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target” 表明在 SSL/TLS 握手過(guò)程中,證書(shū)路徑驗(yàn)證失敗。這通常是由于缺少或不受信任的證書(shū)導(dǎo)致的,Maven 無(wú)法與遠(yuǎn)

    2024年02月08日
    瀏覽(94)
  • https請(qǐng)求報(bào)錯(cuò)unable to find valid certification path to requested target解決

    https請(qǐng)求報(bào)錯(cuò)unable to find valid certification path to requested target解決

    ? ? ? ? 在Java項(xiàng)目中請(qǐng)求HTTPS時(shí),可能會(huì)遇到 \\\"unable to find valid certification path to requested target\\\" 錯(cuò)誤。這個(gè)錯(cuò)誤通常是由于SSL證書(shū)問(wèn)題引起的。要解決此問(wèn)題,可以嘗試以下方法 1.忽略SSL驗(yàn)證 ????????OkHttpClient封裝請(qǐng)求 ? ? ? ? HttpURLConnection請(qǐng)求 ????????RestTemplate請(qǐng)求

    2024年02月08日
    瀏覽(97)
  • 徹底解決:SunCertPathBuilderException: unable to find valid certification path to requested target錯(cuò)誤的方法

    徹底解決:SunCertPathBuilderException: unable to find valid certification path to requested target錯(cuò)誤的方法

    請(qǐng)求12306系統(tǒng)查票。之前正常的,現(xiàn)在提示這樣的錯(cuò)誤: 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 如下圖: ?導(dǎo)致原因:由

    2024年02月05日
    瀏覽(94)
  • how to read dwarf in linux

    Makefile demo.c

    2024年02月16日
    瀏覽(26)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包