gpt4 book ai didi

java - 如何使用 Tomee 绕过 Https SSLHandshakeException

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:40 24 4
gpt4 key购买 nike

我正尝试通过 SSLtomee 1.6 服务器 调用 WS,但我收到了 SSLHandshakeError。问题是证书是自签名的,我的 JVM 无法识别。由于它仅用于测试目的,而不是生产目的,因此我被要求绕过证书控制。我阅读了很多关于如何进行的资料,并且我已经编写了代码:

NaiveSSLContext 类:

package fr.csf.ssl;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

/**
* A factory class which creates an {@link SSLContext} that
* naively accepts all certificates without verification.
*/
public class NaiveSSLContext
{
private NaiveSSLContext()
{}

/**
* Get an SSLContext that implements the specified secure
* socket protocol and naively accepts all certificates
* without verification.
*/
public static SSLContext getInstance( String protocol) throws NoSuchAlgorithmException
{
SSLContext sslCtx = SSLContext.getInstance( protocol);
init( sslCtx);
return sslCtx;
}

/**
* Get an SSLContext that implements the specified secure
* socket protocol and naively accepts all certificates
* without verification.
*/
public static SSLContext getInstance( String protocol, Provider provider) throws NoSuchAlgorithmException
{
SSLContext sslCtx = SSLContext.getInstance( protocol, provider);
init( sslCtx);
return sslCtx;
}

/**
* Get an SSLContext that implements the specified secure
* socket protocol and naively accepts all certificates
* without verification.
*/
public static SSLContext getInstance( String protocol, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
{
SSLContext sslCtx = SSLContext.getInstance( protocol, provider);
init( sslCtx);
return sslCtx;
}

/**
* Set NaiveTrustManager to the given context.
*/
private static void init( SSLContext context)
{
try
{
// Set NaiveTrustManager.
context.init( null, new TrustManager[] { new NaiveTrustManager() }, new java.security.SecureRandom());
System.out.println( "------------- Initialisation du NaiveSSLContext ---------------------");
}
catch( java.security.KeyManagementException e)
{
throw new RuntimeException( "Failed to initialize an SSLContext.", e);
}
}

/**
* A {@link TrustManager} which trusts all certificates naively.
*/
private static class NaiveTrustManager implements X509TrustManager
{
@Override
public X509Certificate[] getAcceptedIssuers()
{
System.out.println( "------------- NaiveTrustManager.getAcceptedIssuers() ---------------------");
return null;
}

@Override
public void checkClientTrusted( X509Certificate[] certs, String authType)
{
System.out.println( "------------- NaiveTrustManager.checkClientTrusted( " + certs.toString() + ", " + authType
+ ") ---------------------");
}

@Override
public void checkServerTrusted( X509Certificate[] certs, String authType)
{
System.out.println( "------------- NaiveTrustManager.checkServerTrusted( " + certs.toString() + ", " + authType
+ ") ---------------------");
}
}
}

和另一个类NaiveSSLSocketFactory:

package fr.csf.ssl;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;

public class NaiveSSLSocketFactory extends javax.net.ssl.SSLSocketFactory
{
private javax.net.ssl.SSLSocketFactory factory;

public NaiveSSLSocketFactory() throws NoSuchAlgorithmException
{
javax.net.ssl.SSLContext sslCtx = NaiveSSLContext.getInstance( "SSL");
factory = sslCtx.getSocketFactory();
}

private final String[] enabledProtocols = new String[]
{ "SSLv3", "TLSv1" };

@Override
public Socket createSocket( Socket s, String host, int port, boolean autoClose) throws IOException
{
Socket socket = factory.createSocket( s, host, port, autoClose);
((javax.net.ssl.SSLSocket) socket).setEnabledProtocols( enabledProtocols);
return socket;
}

@Override
public Socket createSocket( String host, int port) throws IOException, UnknownHostException
{
Socket socket = factory.createSocket( host, port);
((javax.net.ssl.SSLSocket) socket).setEnabledProtocols( enabledProtocols);
return socket;
}

@Override
public Socket createSocket( InetAddress host, int port) throws IOException
{
Socket socket = factory.createSocket( host, port);
((javax.net.ssl.SSLSocket) socket).setEnabledProtocols( enabledProtocols);
return socket;
}

@Override
public Socket createSocket( String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
{
Socket socket = factory.createSocket( host, port, localHost, localPort);
((javax.net.ssl.SSLSocket) socket).setEnabledProtocols( enabledProtocols);
return socket;
}

@Override
public Socket createSocket( InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
{
Socket socket = factory.createSocket( address, port, localAddress, localPort);
((javax.net.ssl.SSLSocket) socket).setEnabledProtocols( enabledProtocols);
return socket;
}

@Override
public String[] getDefaultCipherSuites()
{
String[] cipherSuites = factory.getDefaultCipherSuites();
return cipherSuites;
}

@Override
public String[] getSupportedCipherSuites()
{
String[] cipherSuites = factory.getSupportedCipherSuites();
return cipherSuites;
}
}

问题是我无法找到如何让 JVM 使用我的 Naive* 类而不是默认类。我尝试了不同的方法,但它们都不起作用:

第一次尝试:

javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory( new NaiveSSLSocketFactory());

我在 checkClientTruted 方法中的日志跟踪从未显示。似乎从未调用过我的 NaiveSSLSocketFactory。

第二次尝试:

java.security.Security.setProperty( "ssl.SocketFactory.provider", new NaiveSSLSocketFactory().getClass().getName());

由于 ClassLoader 问题,我遇到了 ClassNotFoundException,但在修复此问题后,同样的问题仍然存在。

我最终找到了一篇博客,其中说 CXF 客户端必须做一些更多的配置工作:

<http-conf:conduit name="*.http-conduit" >
<http-conf:tlsClientParameters
useHttpsURLConnectionDefaultSslSocketFactory="true"
/>
</http-conf:conduit>

由于我使用的是Tomee1.6服务器,我的程序是CXF客户端。所以这必须是解决方案。但是我必须在哪里写这个配置属性?我在 Tomee 中找不到任何与 CXF 相关的 xml 文件。只有 cxf.properties 文件,几乎是空的。

最佳答案

首先,Tomcat 不参与您对 Web 服务的使用 - 事实上,它实际上不参与您的应用程序正在建立的任何出站连接。

我知道有两种方法可以实现 CXF 提供的预期结果,并且不会影响在同一 JVM 上运行的任何其他出站 SSL 连接:

  1. 将自签名证书添加到 CXF 客户端的管道信任商店,或
  2. 为 CXF 安装一个“什么都不做”的信任管理器客户端的 TLS 参数

第一种方法更可取,因为第二种方法将信任您的客户端连接的任何端点。

要实现第一种方法,请创建一个包含您希望信任的证书的 keystore (并且最好包括任何中间证书)。然后按照 CXF 手册部分 Configuring SSL Support 中的概述添加此信任库.您的管道配置将如下所示:

<http:conduit name="{http://apache.org/hello_world}HelloWorld.http-conduit">

<http:tlsClientParameters>
<sec:trustManagers>
<sec:keyStore type="JKS" password="password"
file="my/file/dir/Truststore.jks"/>
</sec:trustManagers>
</http:tlsClientParameters>
<http:client AutoRedirect="true" Connection="Keep-Alive"/>
</http:conduit>

请注意,上面示例中的管道名称显然只是一个示例。参见 the update to my answer here关于如何指定管道名称的另一个问题。另请注意,我没有包含密码套件过滤器,因为我相信它会默认为某些值集,如果您使用的是 Java 6 或更早版本,这可能是不安全的……但那是另一个话题。

此外,您可以完全避开 CXF 的 Spring 配置,并使用 CXF 客户端 API 以编程方式执行上述所有操作。

我还强烈建议使用像 KeyStore Explorer 这样的工具从目标端点提取证书(和中介)并将它们导入到新的信任库中。

最后,关于您的初始解决方案,我想指出在 JVM 范围内安装诸如 SSL 套接字工厂和受 JDK API 支持的信任管理器之类的东西的危险。在支持多个应用程序的容器内运行时,这样做可能会产生危险的后果:您可能会破坏其他应用程序的安全配置文件。使用像 CXF 这样的框架的好处之一是它提供了为每个应用程序客户端(或服务器)实例自定义 SSL/TLS 配置的方法。

关于java - 如何使用 Tomee 绕过 Https SSLHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com