【问题标题】:How to connect to FTPS server with reuse sessions?如何使用重用会话连接到 FTPS 服务器?
【发布时间】:2019-07-22 10:56:15
【问题描述】:

我在我的 android 应用程序中使用 Apache Commons FTP 库。

我正在尝试从我的应用程序(使用 Android Studio)连接到我的 FTPS 服务器,然后上传一些文件。

但是,当我想通过覆盖 FTPSClient 中的方法 _prepareDataSocket_ 来重用会话时,我总是遇到关于 :java.lang.NoSuchFieldException: sessionHostPortCache 的相同错误。 我已尝试使用其他帖子中的代码:How to connect to FTPS server with data connection using same TLS session?Transfer files from android with FTPS to the server 你知道我为什么会出现这个错误吗?

我正在使用带有 jdk 1.8 的 Android Studio。

感谢任何帮助,谢谢。

主要代码:

String server = "ftp.[HIDDEN]";
int port = 21;
String user = "[HIDDEN]";
String pass = "[HIDDEN]";

System.setProperty("jdk.tls.useExtendedMasterSecret", "false");
SSLSessionReuseFTPSClient ftpClient = new SSLSessionReuseFTPSClient("SSL");
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
                

ftpClient.connect(server, port);
System.out.println("Connected to " + server + " on " + port);

ftpClient.login(user, pass);

ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();

// transfer files
InputStream input;
input = new FileInputStream(local);
ftpClient.storeFile(remote, input);
input.close();

ftpClient.noop(); // check that control connection is working OK
ftpClient.logout();

SSLSessionReuseFTPSClient :

public class SSLSessionReuseFTPSClient extends FTPSClient {

    public SSLSessionReuseFTPSClient(String protocol) {
        super(protocol);
    }

    // adapted from: https://trac.cyberduck.io/changeset/10760
    @Override
    protected void _prepareDataSocket_(final Socket socket) throws IOException {
        if (socket instanceof SSLSocket) {
            // Control socket is SSL
            final SSLSession session = ((SSLSocket) _socket_).getSession();
            if (session.isValid()) {
                final SSLSessionContext context = session.getSessionContext();
                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);
                    method.invoke(cache, String
                            .format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
                            .toLowerCase(Locale.ROOT), session);
                    method.invoke(cache, String
                            .format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
                            .toLowerCase(Locale.ROOT), session);
                } catch (NoSuchFieldException e) {
                    throw new IOException(e);
                } catch (Exception e) {
                    throw new IOException(e);
                }
            } else {
                throw new IOException("Invalid SSL Session");
            }
        }
    }
}

Logcat:

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-You are user number 1 of 50 allowed.
07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-Local time is now 11:54. Server port: 21.
07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-This is a private system - No anonymous login
07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-IPv6 connections are also welcome on this server.
07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220 You will be disconnected after 15 minutes of inactivity.
07-22 09:53:59.519 9529-9561/com.example.test I/System.out: AUTH TLS
07-22 09:53:59.533 9529-9561/com.example.test I/System.out: 234 AUTH TLS OK.
07-22 09:53:59.561 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.589 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.593 9529-9561/com.example.test I/System.out: Connected to ftp.[HIDDEN] on 21
07-22 09:53:59.597 9529-9561/com.example.test I/System.out: USER *******
07-22 09:53:59.609 9529-9561/com.example.test I/System.out: 331 User [HIDDEN] OK. Password required
07-22 09:53:59.610 9529-9561/com.example.test I/System.out: PASS *******
07-22 09:53:59.613 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.695 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.701 9529-9561/com.example.test I/System.out: 230 OK. Current restricted directory is /
07-22 09:53:59.702 9529-9561/com.example.test I/System.out: PBSZ 0
07-22 09:53:59.714 9529-9561/com.example.test I/System.out: 200 PBSZ=0
07-22 09:53:59.717 9529-9561/com.example.test I/System.out: PROT P
07-22 09:53:59.727 9529-9561/com.example.test I/System.out: 200 Data protection level set to "private"
07-22 09:53:59.729 9529-9561/com.example.test I/System.out: TYPE I
07-22 09:53:59.743 9529-9561/com.example.test I/System.out: 200 TYPE is now 8-bit binary
07-22 09:53:59.743 9529-9561/com.example.test I/System.out: PASV
07-22 09:53:59.757 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.758 9529-9561/com.example.test I/System.out: 227 Entering Passive Mode (5,134,13,241,188,161)
07-22 09:53:59.777 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
07-22 09:53:59.782 9529-9561/com.example.test I/System.out: STOR pictures/test 18.07 1431/test 18.07 1431_19.6.2019_0.32.15.667.jpg
07-22 09:53:59.792 9529-9561/com.example.test I/System.out: 150 Accepted data connection
07-22 09:53:59.793 9529-9561/com.example.test W/System.err: java.io.IOException: java.lang.NoSuchFieldException: sessionHostPortCache
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.SSLSessionReuseFTPSClient._prepareDataSocket_(SSLSessionReuseFTPSClient.java:54)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:628)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.FTPfunctions2$BackGroundWorker.doInBackground(FTPfunctions2.java:81)
07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.FTPfunctions2$BackGroundWorker.doInBackground(FTPfunctions2.java:41)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.lang.Thread.run(Thread.java:818)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err: Caused by: java.lang.NoSuchFieldException: sessionHostPortCache
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.lang.Class.getDeclaredField(Class.java:890)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at com.example.test.functions.SSLSessionReuseFTPSClient._prepareDataSocket_(SSLSessionReuseFTPSClient.java:42)
07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     ... 12 more

【问题讨论】:

  • 您使用的 JDK/JRE 很可能有一些特别之处(SSLSocket 的不同实现)。您是否在桌面 Java 上测试过相同的代码?您应该缩小问题范围以获得正确的答案。
  • 我重新下载了 Jdk 1.8 但仍然无法正常工作。抱歉,我不明白你说的桌面 java 是什么意思。

标签: java android ftp ftps apache-commons-net


【解决方案1】:

当我将 java.security 更改为使用 SunJSSE 以外的 ssl 安全提供程序时,我遇到了同样的错误。特别是在我的情况下,我将其更改为: security.provider.4=org.bouncycastle.jsse.provider.BouncyCastleJsseProvider 反而 security.provider.4=com.sun.net.ssl.internal.ssl.Provider

1) 检查您的 java.security 文件。

2) 我在类中添加了一条日志消息,显示实际使用了 SSLSessionContext 的实现。

logger.debug("sessionContext calss = " + sessionContext.getClass().getCanonicalName());
final Field sessionHostPortCache = sessionContext.getClass().getDeclaredField("sessionHostPortCache");

我确实发现了 BouncyCastle org.bouncycastle.jsse.provider.ProvSSLSessionContext 类没有 sessionHostPortCache 成员。 你可以试试这个来发现问题。

获得 java.lang.NoSuchFieldException 的另一个原因是因为 JRE 字节码经过了混淆处理。因此 sessionHostPortCache 字段可以命名为例如“b”。

您可以通过在 IDE 中打开 SSLSessionContext.class 来检查这一点,如果您安装反编译器插件,它将显示源代码。

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多