0
votes

We are trying to use the http outbound-gateway and it is working great; but need to go via a proxy.And the proxy needs authentications.

I found that spring configuration for http client is the following:

<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">

the same thing for sftp proxy.

For SFTP, I used SOCKS proxy :

    <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}" />

Because i don't add host and port to DefaultSftpSessionFactory, an Exception occured, i think that it is useless to add host and port to SftpSessionFactory because i already add it to proxy.

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

1 Answers

0
votes

See the Javadocs for the JVM Authenticator object - you need to create an authenticator and register it with the JVM using Authenticator.setDefault().

For SFTP, the DefaultSftpSessionFactory has a property proxy where you can pass in a jsch proxy implementation for connecting via a proxy.

See the Jsch documentation and javadocs for more information.

EDIT

The ProxySOCKS5 class is not Spring-friendly; you can work around that with a factory bean...

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;
    }

}

and

<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>

Or you can wire it up use JavaConfig and @Bean.