Spring集成:http outbound-gateway和sftp-outbound-adapter代理

时间:2016-01-17 12:11:29

标签: spring proxy spring-integration jsch

我们正在尝试使用http outbound-gateway,它运行良好;但需要通过代理。代理需要进行身份验证。

我发现http客户端的spring配置如下:

<bean id="requestFactory"
class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="proxy">
    <bean id="proxy" class="java.net.Proxy">
        <constructor-arg>
            <util:constant static-field="java.net.Proxy.Type.HTTP"/>
        </constructor-arg>
        <constructor-arg>
            <bean class="java.net.InetSocketAddress">
                <constructor-arg value="123.0.0.1"/>
                <constructor-arg value="8080"/>
            </bean>
        </constructor-arg>
    </bean>
</property>

<int-http:outbound-gateway id="gtwy"
    request-channel="channel.request" url="${url}"
    http-method="POST" expected-response-type="java.lang.String" charset="UTF-8"
    reply-timeout="${ws.reply.timeout}" reply-channel="channel.reply"
    request-factory="requestFactory">

与sftp代理相同。

对于SFTP,我使用了SOCKS代理:     

    <property name="user" value="${sftp.user}" />
    <property name="privateKey" value="${sftp.private.keyfile}" />
    <property name="privateKeyPassphrase" value="${sftp.passphrase}" />
    <property name="allowUnknownKeys" value="true" />
    <property name="proxy" ref="proxySocks5" />
</bean>

<bean id="proxySocks5" class="com.foo.ProxySOCKS5FactoryBean">
<constructor-arg value="${sftp.proxy.address}" />
<constructor-arg value="${sftp.proxy.port}" />
<constructor-arg value="${sftp.proxy.user}" />
<constructor-arg value="${sftp.proxy.pw}" />

因为我没有向DefaultSftpSessionFactory添加主机和端口,所以发生了异常,我认为将主机和端口添加到SftpSessionFactory是没用的,因为我已经将它添加到代理中。

Downloading files failedorg.springframework.messaging.MessagingException: Problem occurred while synchronizing remote to local directory; nested exception is org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is java.lang.IllegalArgumentException: host must not be empty

1 个答案:

答案 0 :(得分:0)

请参阅Javadocs以获取JVM ;; (pp/pprint (mutils/keep-tag-and-contents-prepare-leafs xlmeta-testdata-small)) ;; [:defaultFormattings ;; ([:_columnMeta ;; ([:XLMetaColumn ;; ([:_name "Paris"] ;; [:_caption "Paris"] ;; [:_width "100"] ;; [:_hide "false"] ;; [:_input "false"] ;; [:_hideExport "false"] ;; [:_textAreaRows "0"])] ;; [:XLMetaColumn ;; ([:_name "footbill40"] ;; [:_caption "footbill40"] ;; [:_width "100"] ;; [:_hide "false"] ;; [:_input "false"] ;; [:_hideExport "false"] ;; [:_textAreaRows "0"])])] ;; [:_fmtStrings nil] ;; [:_maxHtmlColumns "50"])] 对象 - 您需要创建一个身份验证器并使用Authenticator向JVM注册它。

对于SFTP,Authenticator.setDefault()具有属性DefaultSftpSessionFactory,您可以在其中传入jsch代理实现以通过代理进行连接。

有关详细信息,请参阅Jsch documentation and javadocs

修改

ProxySOCKS5类不是Spring友好的;你可以用工厂bean解决这个问题......

proxy

public class ProxySOCKS5FactoryBean extends AbstractFactoryBean<ProxySOCKS5> {

    private final String host;

    private final int port;

    private final String user;

    private final String password;

    public ProxySOCKS5FactoryBean(String host, int port, String user, String password) {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
    }

    @Override
    public Class<?> getObjectType() {
        return Socks5.class;
    }

    @Override
    protected ProxySOCKS5 createInstance() throws Exception {
        ProxySOCKS5 proxy = new ProxySOCKS5(this.host, this.port);
        proxy.setUserPasswd(this.user, this.password);
        return proxy;
    }

}

或者您可以使用JavaConfig和<bean id="proxySocks5" class="com.foo.ProxySOCKS5FactoryBean"> <constructor-arg value="${sftp.proxy.address}" /> <constructor-arg value="${sftp.proxy.port}" /> <constructor-arg value="${sftp.proxy.user}" /> <constructor-arg value="${sftp.proxy.pw}" /> </bean> 连接它。