在 Spring RestTemplate 中禁用 SSL 证书验证 [英] Disabling SSL Certificate Validation in Spring RestTemplate
问题描述
我在两台不同的机器上有两个基于 Spring 的 Web 应用 A 和 B.
I am having two Spring-based web apps A and B, on two different machines.
我想从网络应用程序 A 到网络应用程序 B 进行 HTTPS 调用,但是,我在机器 B 中使用了自签名证书.所以我的 HTTPS 请求失败了.
I want to make an HTTPS call from web app A to web app B, however, I am using a self-signed certificate in Machine B. So my HTTPS request fails.
在 Spring 中使用 RestTemplate 时如何禁用 HTTPS 证书验证?我想禁用验证,因为 Web 应用 A 和 B 都在内部网络内,但数据传输必须通过 HTTPS 进行
How can I disable HTTPS certificate validation when using RestTemplate in Spring? I want to disable validation because both web app A and B are within the internal network, but data transfer has to happen over HTTPS
推荐答案
你需要添加的是一个自定义的 HostnameVerifier
类绕过证书验证并返回 true
What you need to add is a custom HostnameVerifier
class bypasses certificate verification and returns true
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
这需要适当地放置在您的代码中.
This needs to be placed appropriately in your code.
这篇关于在 Spring RestTemplate 中禁用 SSL 证书验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!