Java 什么';当您没有';你不需要提供证书吗?

Java 什么';当您没有';你不需要提供证书吗?,java,spring,ssl,spring-integration,Java,Spring,Ssl,Spring Integration,在使用SSL配置TCP连接时,根据spring文档,我们正在使用自己的TcpSSLContextSupport实例,因为DefaultTcpSSLContextSupport需要初始化客户端证书。下面是我们的bean配置: private static final int SERIALIZER_HEADER_SIZE = 2; @Bean public ByteArrayLengthHeaderSerializer byteArrayLengthHeaderSerializer() {

在使用SSL配置TCP连接时,根据spring文档,我们正在使用自己的
TcpSSLContextSupport
实例,因为
DefaultTcpSSLContextSupport
需要初始化客户端证书。下面是我们的bean配置:

private static final int SERIALIZER_HEADER_SIZE = 2;

@Bean
public ByteArrayLengthHeaderSerializer byteArrayLengthHeaderSerializer() {
    return new ByteArrayLengthHeaderSerializer(SERIALIZER_HEADER_SIZE);
}

@Bean
public AbstractClientConnectionFactory tcpClientConnectionFactory() {
    TcpNetClientConnectionFactory connFactory =
        new TcpNetClientConnectionFactory(props.getUrl(), props.getPort());
    connFactory.setSerializer(byteArrayLengthHeaderSerializer());
    connFactory.setDeserializer(byteArrayLengthHeaderSerializer());
    connFactory.setSoTimeout(props.getSoTimeout());

    if (props.isUseSSL()) {
        connFactory.setTcpSocketFactorySupport(new DefaultTcpNetSSLSocketFactorySupport(() -> {
            return SSLContext.getDefault();
        }));
    }

    return connFactory;
}

由于客户端证书是已知的证书,我们不需要提供自己的密钥库和信任库,因此这是在创建TCP连接时正确配置SSL的方法吗?

对,这正是将自定义的
SSLContext
注入
连接工厂的方法

你也可以考虑在你的上下文中“相信所有”:

    TrustManager[] trustAllCerts = new TrustManager[] { 
        new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                X509Certificate[] myTrustedAnchors = new X509Certificate[0];  
                return myTrustedAnchors;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {}

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {}
        }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    return sc;

请注意,客户端身份验证在默认情况下处于关闭状态,因此您还应该能够指向空存储。我们有一个专门支持客户端身份验证的测试用例。谢谢你们,测试用例非常清晰:)