TcpConnectionFactoryFactoryBean:同时使用SslContextSupport和SocketFactorySupport

时间:2019-02-14 05:47:18

标签: java spring spring-integration

我正在使用TcpConnectionFactoryFactoryBean(用于客户端,非NIO连接),并希望通过2种方式修改我的SSL套接字连接:

1 / 仅使用TLS1.2协议 我可以通过调用 TcpConnectionFactoryFactoryBean.setSslContextSupport()并提供具有TLSv1.2协议集的 DefaultTcpSSLContextSupport 对象来实现。

2 / 设置套接字连接超时 我通过调用 TcpConnectionFactoryFactoryBean.setSocketFactorySupport()并提供 TcpSocketFactorySupport 的自定义子类来创建套接字,然后在连接调用中应用超时。 here中记录了该技术。

我俩都独立工作,但无法让他们一起工作。看一下TcpConnectionFactoryFactoryBean代码,很显然,gettingSocketFactorySupport()方法只会选择一个。因此,我认为我的方法是不正确的。谁能提供任何指导?

1 个答案:

答案 0 :(得分:1)

我认为它可以通过组合成一个单独的SocketFactorySupport类来工作,该类将 DefaultTcpNetSSLSocketFactorySupport 子类化,然后与 TcpConnectionFactoryFactoryBean.setSocketFactorySupport()一起使用。我的子类如下所示,将 DefaultTcpSSLContextSupport (设置了协议)传递给构造函数:

public class MySSLSocketFactorySupport extends DefaultTcpNetSSLSocketFactorySupport {

    private final int connectTimeout;

    public MySSLSocketFactorySupport(TcpSSLContextSupport sslContextSupport, int connectTimeout) {
        super(sslContextSupport);
        this.connectTimeout = connectTimeout;
    }

    @Override
    public ServerSocketFactory getServerSocketFactory() {
        throw new UnsupportedOperationException("This does not support server sockets");
    }

    @Override
    public SocketFactory getSocketFactory() {
        return new PostilionSocketFactory();
    }

    private class PostilionSocketFactory extends SocketFactory {

        private final SocketFactory delegate = MySSLSocketFactorySupport.super.getSocketFactory();

        @Override
        public Socket createSocket() throws IOException {
            return delegate.createSocket();
        }

        @Override
        public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            Socket socket = this.delegate.createSocket();
            socket.connect(new InetSocketAddress(host, port), connectTimeout);
            return socket;
        }

        @Override
        public Socket createSocket(InetAddress host, int port) throws IOException {
            Socket socket = this.delegate.createSocket();
            socket.connect(new InetSocketAddress(host, port), connectTimeout);
            return socket;
        }

        @Override
        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
            Socket socket = this.delegate.createSocket();
            socket.bind(new InetSocketAddress(localHost, localPort));
            socket.connect(new InetSocketAddress(host, port), connectTimeout);
            return socket;
        }

        @Override
        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
            Socket socket = this.delegate.createSocket();
            socket.bind(new InetSocketAddress(localAddress, localPort));
            socket.connect(new InetSocketAddress(address, port), connectTimeout);
            return socket;
        }

    }

}