通过使用相应的消息头密钥值来设置SFTP配置属性值

时间:2019-02-28 12:20:01

标签: spring-boot spring-integration

我有一个根据提供的配置连接SFTP服务器的要求。该配置会有所不同,并且在标头内部提供了正确的密钥。我想从键中获取值以设置相应的bean属性值,如下所述。我已经进行了更改,但显示为“未知主机异常”,有人可以帮我吗?

<bean id="sftpSessionFactory"
                class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
                <property name="allowUnknownKeys" value="true" />
                <property name="host" value="headers['SFTP_SERVER']" />
                <property name="privateKey"
                    value="headers['SFTP_KEY']" />
                <property name="privateKeyPassphrase" value="" />
                <property name="port" value="headers['SFTP_PORT']" />
                <property name="user" value="headers['SFTP_USER']" />
                <property name="password" value="headers.SFTP_PASSWORD" />
            </bean>

1 个答案:

答案 0 :(得分:0)

它不会以您看到的方式工作。这样的sftpSessionFactory <bean>在应用程序启动期间将仅创建一次,并且那些类似SpEL的属性值在此阶段不会带来任何值:没有任何消息可与之交互。请了解什么是Spring中的bean以及如何对其进行初始化:https://docs.spring.io/spring/docs/5.1.5.RELEASE/spring-framework-reference/core.html#spring-core

基于标头的用例的唯一解决方案可以通过DelegatingSessionFactory和自定义SessionFactoryLocator来完成,在此您可以动态创建DefaultSftpSessionFactory的实例。当然,您可以将它们缓存在那儿,因为不同的消息可能在其标头中带来相同的属性值。不要忘记在key中存储一些ThreadLocal

/**
 * Set a key to be used for {@link #getSession()} on this thread.
 * @param key the key.
 */
public void setThreadKey(Object key) {
    this.threadKey.set(key);
}

否则DelegatingSessionFactory将无法正常工作。

在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/#sftp-dsf