進階設定
DefaultFtpSessionFactory
提供了底層客戶端 API 的抽象化,自 Spring Integration 2.0 起,該 API 為 Apache Commons Net。這讓您免於處理 org.apache.commons.net.ftp.FTPClient
的低階設定細節。工作階段工廠上公開了幾個常見屬性(自 4.0 版起,現在包括 connectTimeout
、defaultTimeout
和 dataTimeout
)。但是,有時您需要存取較低層級的 FTPClient
設定才能實現更進階的設定(例如設定主動模式的埠範圍)。為此,AbstractFtpSessionFactory
(所有 FTP 工作階段工廠的基底類別)以以下清單中顯示的兩個後處理方法的形式公開了掛鉤
/**
* 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
當使用 FTP over SSL 或 TLS 時,某些伺服器要求控制和資料連線使用相同的 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 工作階段
@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());
}
}
}
}