For the latest stable version, please use Spring Integration 7.2.0!

FTP Session Factory

Spring Integration provides factories you can use to create FTP (or FTPS) sessions.

Default Factories

Starting with version 3.0, sessions are no longer cached by default. See FTP Session Caching.

Before configuring FTP adapters, you must configure an FTP session factory. You can configure the FTP Session Factory with a regular bean definition where the implementation class is o.s.i.ftp.session.DefaultFtpSessionFactory. The following example shows a basic configuration:

<bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="localhost"/>
    <property name="port" value="22"/>
    <property name="username" value="kermit"/>
    <property name="password" value="frog"/>
    <property name="clientMode" value="0"/>
    <property name="fileType" value="2"/>
    <property name="bufferSize" value="100000"/>
</bean>

For FTPS connections, you can use o.s.i.ftp.session.DefaultFtpsSessionFactory instead.

The following example shows a complete configuration:

<bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpsSessionFactory">
    <property name="host" value="localhost"/>
    <property name="port" value="22"/>
    <property name="username" value="oleg"/>
    <property name="password" value="password"/>
    <property name="clientMode" value="1"/>
    <property name="fileType" value="2"/>
    <property name="useClientMode" value="true"/>
    <property name="cipherSuites" value="a,b.c"/>
    <property name="keyManager" ref="keyManager"/>
    <property name="protocol" value="SSL"/>
    <property name="trustManager" ref="trustManager"/>
    <property name="prot" value="P"/>
    <property name="needClientAuth" value="true"/>
    <property name="authValue" value="oleg"/>
    <property name="sessionCreation" value="true"/>
    <property name="protocols" value="SSL, TLS"/>
    <property name="implicit" value="true"/>
</bean>
If you experience connectivity problems and would like to trace session creation as well as see which sessions are polled, you can enable session tracing by setting the logger to the TRACE level (for example, log4j.category.org.springframework.integration.file=TRACE).

Now you need only inject these session factories into your adapters. The protocol (FTP or FTPS) that an adapter uses depends on the type of session factory that has been injected into the adapter.

A more practical way to provide values for FTP or FTPS session factories is to use Spring’s property placeholder support (See docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-placeholderconfigurer).

FTPS Server Certificate Validation

The DefaultFtpsSessionFactory fully validates the certificate of the FTPS server by default.

When no trustManager is configured, the JRE default TrustManagerFactory is used, so the server certificate chain must pass the PKIX validation against the trust store of the JVM (by default, the cacerts of the JRE, or the one set by the javax.net.ssl.trustStore system property). A trustManager set on the session factory is used as is.

In addition, the endpointCheckingEnabled property (true by default) requires the server certificate to match the host name the client connects to.

An application that connects to an FTPS server with a self-signed certificate, a certificate issued by a private certificate authority, or a certificate that does not match the configured host fails to create a session. For such a server, provide a trustManager that trusts its certificate, or add that certificate (or its certificate authority) to the trust store of the JVM. Set endpointCheckingEnabled to false only when connecting to a trusted internal server that cannot present a certificate matching its host name.

Proxy Support

Starting with version 7.2, the session factories can connect to the FTP (or FTPS) server through a proxy.

The proxy property of the AbstractFtpSessionFactory (and, therefore, of the DefaultFtpSessionFactory and the DefaultFtpsSessionFactory) accepts a java.net.Proxy. The proxy is used for the control connection and for the data connections, which requires the passive client mode (FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE): the server cannot connect back to the client through a proxy. Therefore, setting the proxy also switches the clientMode to the passive mode. If the clientMode is changed afterwards to another mode, the session factory fails to create a session. A Proxy.Type.SOCKS proxy is used as is. A Proxy.Type.HTTP proxy is used with the HTTP CONNECT tunneling of the JVM; the credentials for such a proxy, if any, have to be provided through a java.net.Authenticator.

The following example shows a session factory that connects through a SOCKS proxy:

@Bean
DefaultFtpSessionFactory ftpSessionFactory() {
    DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory();
    sessionFactory.setHost("ftp.example.com");
    sessionFactory.setUsername("kermit");
    sessionFactory.setPassword("frog");
    sessionFactory.setProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("proxy.example.com", 1080)));
    return sessionFactory;
}

When the HTTP proxy requires credentials, use the FtpHttpProxySessionFactory instead. It tunnels the connections through the proxy with the CONNECT method and sends the credentials to the proxy with the basic authentication scheme. This factory supports only the passive client mode, which is its default. The following example shows its configuration:

<bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.FtpHttpProxySessionFactory">
    <property name="host" value="ftp.example.com"/>
    <property name="username" value="kermit"/>
    <property name="password" value="frog"/>
    <property name="proxyHost" value="proxy.example.com"/>
    <property name="proxyPort" value="3128"/>
    <property name="proxyUsername" value="kermit"/>
    <property name="proxyPassword" value="frog"/>
</bean>
The FtpHttpProxySessionFactory supports only plain FTP connections. For FTPS connections through a proxy, use the proxy property of the DefaultFtpsSessionFactory.

Unlike the proxy system properties of the JVM (such as socksProxyHost), these options apply only to the sessions of the configured factory, not to all the connections of the application.