我正在使用 spring-integration-ip-3.0.8.RELEASE jar 建立 TCP 连接。 我的要求是有条件地启用 SSL 支持。 我使用下面的代码分别使用和不使用 SSL 进行连接
With SSL
<beans:bean id="serverCustomSerializer"
class="com.telnet.core.serializer.CustomSerializer">
<beans:property name="terminatingChar"
value="${server.terminator}" />
<beans:property name="maxLength"
value="${server.msgLength}" />
</beans:bean>
<beans:bean id="serverFactoryTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<beans:property name="corePoolSize" value="5" />
<beans:property name="queueCapacity" value="0" />
</beans:bean>
<int:channel id="telnetLandingChannel" />
<ip:tcp-connection-factory id="serverFactoryWithSSL"
type="server" host="${server.host}" port="${server.port}"
single-use="false" serializer="${server.serializer}"
deserializer="${server.serializer}"
task-executor="serverFactoryTaskExecutor"
ssl-context-support="sslContextSupport" />
<ip:tcp-inbound-channel-adapter
id="serverInboundAdpaterAck" channel="telnetLandingChannel"
connection-factory="${server.factory}" error-channel="errorChannel"
auto-startup="false" />
<ip:tcp-outbound-channel-adapter
id="serverOutboundAdapter" channel="serverReplyChannel"
connection-factory="serverFactory" auto-startup="true" />
Without SSL
<beans:bean id="serverCustomSerializer"
class="com.telnet.core.serializer.CustomSerializer">
<beans:property name="terminatingChar"
value="${server.terminator}" />
<beans:property name="maxLength"
value="${server.msgLength}" />
</beans:bean>
<beans:bean id="serverFactoryTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<beans:property name="corePoolSize" value="5" />
<beans:property name="queueCapacity" value="0" />
</beans:bean>
<int:channel id="telnetLandingChannel" />
<ip:tcp-connection-factory id="serverFactory"
type="server" host="${server.host}" port="${server.port}"
single-use="false" serializer="${server.serializer}"
deserializer="${server.serializer}"
task-executor="serverFactoryTaskExecutor" />
<ip:tcp-inbound-channel-adapter
id="serverInboundAdpaterAck" channel="telnetLandingChannel"
connection-factory="${server.factory}" error-channel="errorChannel"
auto-startup="false" />
<ip:tcp-outbound-channel-adapter
id="serverOutboundAdapter" channel="serverReplyChannel"
connection-factory="serverFactory" auto-startup="true" />
我需要在同一个文件中同时使用这两者,并基于一些外部标志,需要决定连接是否支持 SSL,因为一些客户希望使用 SSL,而另一些客户希望不使用 SSL。
独立地,两者都是工作文件。 我需要建议如何使其可配置(基于标志)。 TcpConnectionFactoryFactoryBean 类有一个方法 setSslContextSupport(TcpSSLContextSupport sslContextSupport),但是如何在 xml 中有条件地调用它。
最佳答案
您可以使用 Spring Profiles在应用程序启动期间激活一个或另一个。
Bean definition profiles is a mechanism in the core container that allows for registration of different beans in different environments. The word environment can mean different things to different users and this feature can help with many use cases, including: ...
编辑
如果你只想将连接工厂转换为Java配置,只需添加一个<bean/>
即可。这种类型的到你的 xml。如果要将所有内容都转换为 Java 配置,请参阅 Spring Integration 引用手册。
@Configuration
public class Config {
@Bean
public TcpConnectionFactoryFactoryBean serverFactoryWithOrWithoutSSL(
@Value("${server.host}") String host,
@Value("${server.port}") int port,
@Value("${use.ssl}") boolean useSSL,
ByteArrayLengthHeaderSerializer serializer,
TcpSSLContextSupport sslContextSupport) {
TcpConnectionFactoryFactoryBean fb = new TcpConnectionFactoryFactoryBean("server");
fb.setHost(host);
fb.setPort(port);
fb.setSerializer(serializer);
fb.setDeserializer(serializer);
if (useSSL) {
fb.setSslContextSupport(sslContextSupport);
}
return fb;
}
@Bean
public ByteArrayLengthHeaderSerializer serializer() {
return new ByteArrayLengthHeaderSerializer();
}
@Bean
public TcpSSLContextSupport sslContextSupport() {
return new DefaultTcpSSLContextSupport("file:keyStore.ks", "secret", "file:trustStore", "secret");
}
}
关于java - 使用 Spring Integration 使 ssl-context-support 有条件地使用 tcp-connection-factory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065647/