高级配置

DefaultFtpSessionFactory 在底层客户端 API 之上提供了一个抽象层,自 Spring Integration 2.0 版本起,该抽象层基于 Apache Commons Net。这使得您无需关注 org.apache.commons.net.ftp.FTPClient 的低层配置细节。Session Factory 暴露了一些通用属性(自 4.0 版本起,包括 connectTimeoutdefaultTimeoutdataTimeout)。然而,有时您需要访问更低层的 FTPClient 配置来实现更高级的设置(例如为主动模式设置端口范围)。为此,AbstractFtpSessionFactory(所有 FTP Session Factory 的基类)通过以下列表中所示的两个后处理方法提供了钩子

/**
 * Will handle additional initialization after client.connect() method was invoked,
 * but before any action on the client has been taken
 */
protected void postProcessClientAfterConnect(T t) throws IOException {
    // NOOP
}
/**
 * Will handle additional initialization before client.connect() method was invoked.
 */
protected void postProcessClientBeforeConnect(T client) throws IOException {
    // NOOP
}

如您所见,这两个方法没有默认实现。但是,通过扩展 DefaultFtpSessionFactory,您可以覆盖这些方法来提供更高级的 FTPClient 配置,示例如下所示

public class AdvancedFtpSessionFactory extends DefaultFtpSessionFactory {

    protected void postProcessClientBeforeConnect(FTPClient ftpClient) throws IOException {
       ftpClient.setActivePortRange(4000, 5000);
    }
}

FTPS 和共享 SSLSession

当使用基于 SSL 或 TLS 的 FTP 时,有些服务器要求控制连接和数据连接使用相同的 SSLSession。这是为了防止“数据连接被窃取”。更多信息请参见 scarybeastsecurity.blogspot.cz/2009/02/vsftpd-210-released.html

当前,Apache FTPSClient 不支持此特性。参见 NET-408

以下解决方案来自 Stack Overflow,使用反射访问了 sun.security.ssl.SSLSessionContextImpl,因此可能无法在其他 JVM 上工作。该 Stack Overflow 回答于 2015 年提交,Spring Integration 团队已在 JDK 1.8.0_112 上测试过该解决方案。

以下示例展示了如何创建 FTPS session

@Bean
public DefaultFtpsSessionFactory sf() {
    DefaultFtpsSessionFactory sf = new DefaultFtpsSessionFactory() {

        @Override
        protected FTPSClient createClientInstance() {
            return new SharedSSLFTPSClient();
        }

    };
    sf.setHost("...");
    sf.setPort(21);
    sf.setUsername("...");
    sf.setPassword("...");
    sf.setNeedClientAuth(true);
    return sf;
}

private static final class SharedSSLFTPSClient extends FTPSClient {

    @Override
    protected void _prepareDataSocket_(final Socket socket) throws IOException {
        if (socket instanceof SSLSocket) {
            // Control socket is SSL
            final SSLSession session = ((SSLSocket) _socket_).getSession();
            final SSLSessionContext context = session.getSessionContext();
            context.setSessionCacheSize(0); // you might want to limit the cache
            try {
                final Field sessionHostPortCache = context.getClass()
                        .getDeclaredField("sessionHostPortCache");
                sessionHostPortCache.setAccessible(true);
                final Object cache = sessionHostPortCache.get(context);
                final Method method = cache.getClass().getDeclaredMethod("put", Object.class,
                        Object.class);
                method.setAccessible(true);
                String key = String.format("%s:%s", socket.getInetAddress().getHostName(),
                        String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT);
                method.invoke(cache, key, session);
                key = String.format("%s:%s", socket.getInetAddress().getHostAddress(),
                        String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT);
                method.invoke(cache, key, session);
            }
            catch (NoSuchFieldException e) {
                // Not running in expected JRE
                logger.warn("No field sessionHostPortCache in SSLSessionContext", e);
            }
            catch (Exception e) {
                // Not running in expected JRE
                logger.warn(e.getMessage());
            }
        }

    }

}