如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

高级配置

DefaultFtpSessionFactory 提供了对底层客户端 API 的抽象,该 API(自 Spring Integration 2.0 起)为 Apache Commons Net。 这使您无需处理 org.apache.commons.net.ftp.FTPClient 的低级配置细节。 会话工厂上暴露了几个常用属性(自版本 4.0 起,现在包括 connectTimeoutdefaultTimeoutdataTimeout)。 然而,有时您需要访问更低级别的 FTPClient 配置以实现更高级的配置(例如设置主动模式的端口范围)。 为此,AbstractFtpSessionFactory(所有 FTP 会话工厂的基类)以如下清单中所示的两个后处理方法的形式提供了钩子:spring-doc.cadn.net.cn

/**
 * 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 配置,如下例所示:spring-doc.cadn.net.cn

public class AdvancedFtpSessionFactory extends DefaultFtpSessionFactory {

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

FTPS 与共享 SSL 会话

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

目前,Apache FTPSClient 不支持此功能。 请参阅 NET-408spring-doc.cadn.net.cn

以下解决方案由Stack Overflow提供,它使用了sun.security.ssl.SSLSessionContextImpl上的反射,因此在其他 JVM 上可能无法工作。 该 Stack Overflow 答案提交于 2015 年,Spring Integration 团队已在 JDK 1.8.0_112 上对该解决方案进行了测试。spring-doc.cadn.net.cn

以下示例展示了如何创建 FTPS 会话:spring-doc.cadn.net.cn

@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());
            }
        }

    }

}