How to disable certificate validations in the Java HTTP Client

本文介绍了如何在Java11的HTTPClient中禁用证书验证,包括使用X509ExtendedTrustManager绕过路径和主机名验证,以及分别针对路径验证和主机名验证的单独解决方案。通常情况下,不进行任何验证的X509TrustManager是最佳选择。

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.

在Ubuntu系统中,禁用挂起功能可以通过多种方式实现,具体取决于用户希望在何种场景下禁用该功能。以下是几种常见的方法: ### 1. 禁用通过电源管理的自动挂起 Ubuntu 使用 `systemd` 来管理系统的电源状态,包括挂起(suspend)和休眠(hibernate)。可以通过修改 `logind.conf` 文件来禁用自动挂起功能。 编辑 `/etc/systemd/logind.conf` 文件: ```bash sudo nano /etc/systemd/logind.conf ``` 找到以下行并进行修改: ```ini HandleSuspendKey=ignore HandleLidSwitch=ignore ``` 保存文件后重启 `systemd-logind` 服务以应用更改: ```bash sudo systemctl restart systemd-logind ``` ### 2. 禁用通过图形界面的挂起选项 如果使用的是 GNOME 桌面环境,可以通过 GNOME 的设置工具来禁用挂起功能。安装 `dconf-editor` 工具并运行: ```bash sudo apt install dconf-editor dconf-editor ``` 导航到路径 `/org/gnome/settings-daemon/plugins/power/`,然后将 `sleep-inactive-ac-timeout` 和 `sleep-inactive-battery-timeout` 设置为 `0` 以禁用自动挂起。 ### 3. 完全移除挂起功能 如果希望完全禁用挂起功能,可以通过创建一个自定义的 `systemd` 服务来覆盖默认行为。创建一个新的服务文件: ```bash sudo nano /etc/systemd/system/disable-suspend.service ``` 添加以下内容: ```ini [Unit] Description=Disable Suspend [Service] Type=oneshot ExecStart=/bin/systemctl mask sleep.target [Install] WantedBy=multi-user.target ``` 启用并启动该服务: ```bash sudo systemctl enable disable-suspend.service sudo systemctl start disable-suspend.service ``` 此操作会阻止系统进入挂起状态,并且无法通过任何命令或快捷键触发挂起。 ### 4. 禁用通过内核参数 如果需要在系统启动时就完全禁用挂起功能,可以在 GRUB 配置中添加内核参数。编辑 `/etc/default/grub` 文件: ```bash sudo nano /etc/default/grub ``` 修改 `GRUB_CMDLINE_LINUX` 行以添加 `mem_sleep_default=deep` 参数: ```ini GRUB_CMDLINE_LINUX="... mem_sleep_default=deep" ``` 更新 GRUB 配置: ```bash sudo update-grub ``` 这种方法会直接影响系统的内存睡眠模式,从而限制挂起功能的行为。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值