Java SSL:如何禁用主机名验证
标准 java SSL 套接字是否可以通过属性禁用 ssl 连接的主机名验证?到目前为止我发现的唯一方法是编写一个始终返回 true 的主机名验证器。
Weblogic 提供了这种可能性,可以使用以下属性禁用主机名验证:
-Dweblogic.security.SSL.ignoreHostnameVerify
Is there a way for the standard java SSL sockets to disable hostname verfication for ssl connections with a property? The only way I found until now, is to write a hostname verifier which returns true all the time.
Weblogic provides this possibility, it is possible to disable the hostname verification with the following property:
-Dweblogic.security.SSL.ignoreHostnameVerify
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应该可以创建覆盖默认
HostnameVerifier
的自定义 java 代理:然后只需将
-javaagent:LenientHostnameVerifierAgent.jar
添加到程序的 java 启动参数中即可。It should be possible to create custom java agent that overrides default
HostnameVerifier
:Then just add
-javaagent:LenientHostnameVerifierAgent.jar
to program's java startup arguments.@Nani 的答案不再适用于 Java 1.8u181。您仍然需要使用自己的 TrustManager,但它需要是
X509ExtendedTrustManager
而不是X509TrustManager
:The answer from @Nani doesn't work anymore with Java 1.8u181. You still need to use your own TrustManager, but it needs to be a
X509ExtendedTrustManager
instead of aX509TrustManager
:标准 Java SSL 套接字或 SSL 中没有主机名验证,因此您无法将其设置为该级别。主机名验证是 HTTPS (RFC 2818) 的一部分:这就是为什么它表现为 javax.net.ssl.HostnameVerifier,它应用于 HttpsURLConnection。
There is no hostname verification in standard Java SSL sockets or indeed SSL, so that's why you can't set it at that level. Hostname verification is part of HTTPS (RFC 2818): that's why it manifests itself as javax.net.ssl.HostnameVerifier, which is applied to an HttpsURLConnection.
我在访问 RESTful Web 服务时也遇到了同样的问题。我用下面的代码来解决这个问题:
它对我有用。试试吧!
I also had the same problem while accessing RESTful web services. And I their with the below code to overcome the issue:
It worked for me. try it!!
@user207421 是对的,标准 Java SSL 套接字或 SSL 中没有主机名验证。
但是 X509ExtendedTrustManager 实现主机名检查逻辑(参见 javadoc)。要禁用此功能,我们可以将 SSLParameters .endpointIdentificationAlgorithm 设置为 null 作为 JDK AbstractAsyncSSLConnection 做了:
disableHostnameVerification
从属性中读取:jdk.internal.httpclient.disableHostnameVerification。如何修改 SSLParameters 对象取决于您使用的指定软件。
如 spring webflux WebClient:
@user207421 is right, there is no hostname verification in standard Java SSL sockets or indeed SSL.
But X509ExtendedTrustManager implement the host name check logic(see it's javadoc). To disable this, We can set SSLParameters .endpointIdentificationAlgorithm to null as JDK AbstractAsyncSSLConnection did:
disableHostnameVerification
is read from property: jdk.internal.httpclient.disableHostnameVerification。How to modify SSLParameters Object is dependends on the specify soft you use。
as spring webflux WebClient:
如果您使用 apache 的 http-client 4:
In case you're using apache's http-client 4:
禁用检查主机名:
Disable check hostname: