我正在使用TcpConnectionFactoryFactoryBean(用于客户端,非NIO连接),并希望通过2种方式修改我的SSL套接字连接:
1 /仅使用TLS1.2协议
我通过调用TcpConnectionFactoryFactoryBean.setSslContextSupport()并提供带有TLSv1.2协议集的DefaultTcpSSLContextSupport对象来实现。
和
2 /设置套接字连接超时
我可以通过调用TcpConnectionFactoryFactoryBean.setSocketFactorySupport()并提供TcpSocketFactorySupport的自定义子类来创建套接字,然后在连接调用中应用超时。 here中记录了该技术。
我俩都独立工作,但无法让他们一起工作。看一下TcpConnectionFactoryFactoryBean代码,很显然,gettingSocketFactorySupport()方法只会选择一个。因此,我认为我的方法是不正确的。谁能提供任何指导?
最佳答案
认为我已经通过组合到一个以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;
}
}
}