我无法通过 FTPS 与 Apache FTPSClient 和自定义 SSLContext 连接到我的 FTP 服务器 - 获取 "Unrecognized SSL message, plaintext connection?"

I cannot connect to my FTP server by FTPS with Apache FTPSClient with custom SSLContext - getting "Unrecognized SSL message, plaintext connection?"

我正在编写程序以通过 FTPS 连接到 FTP 服务器。

源代码:

String protocol = "TLS";
String host = "192.168.5.165";
String username = "usr";
String password = "111";
String trustStorePath = "C:/TEMP/truststore";
String trustStorePassword = "111111";
int port = 990;

FTPSClient client = new FTPSClient(protocol);

KeyStore trustStore = loadStore("JKS", new File(trustStorePath), trustStorePassword);
X509TrustManager trustManager = TrustManagerUtils.getDefaultTrustManager(trustStore);

SSLContext ctx = SSLContext.getInstance("TLSv1.1");
ctx.init(null, new TrustManager[] {trustManager}, null);

FTPSSocketFactory socketFactory = new FTPSSocketFactory(ctx);
client.setSocketFactory(socketFactory);

client.addProtocolCommandListener(new PrintCommandListener(new 
PrintWriter(System.out)));
client.connect(host, port);

我在 trustStore 中导入了服务器的证书。但是在 运行 这段代码之后我得到了这个错误:

220 Wing FTP Server ready... (UNREGISTERED WING FTP SERVER)
AUTH TLS
234 AUTH command OK. Initializing TLS connection.

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

  at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
  at sun.security.ssl.InputRecord.read(InputRecord.java:527)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
  at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
  at org.apache.commons.net.ftp.FTPSClient.sslNegotiation(FTPSClient.java:269)
  at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:211)
  at org.apache.commons.net.SocketClient.connect(SocketClient.java:183)
  at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
  at edtesb.remsenergy.FTPSTest.test(FTPSTest.java:63)

我做错了什么?

在我看来,你设计过度了。

我不确定我能否完全理解您的代码的作用,但我相信您无意中对连接进行了双重加密。


如果你真的想连接到隐式 FTPS 端口 990,只需使用

FTPSClient client = new FTPSClient(protocol, true);

protocol="TLS" 是默认值,这也可以:

FTPSClient client = new FTPSClient(true);

client.connect(host); // 990 is default, with FTPSClient with isImplicit=true

虽然隐式 FTPS 已过时,但您最好使用显式 FTPS(如果您的服务器支持,大多数支持),方法是连接到默认 FTP 端口21:

FTPSClient client = new FTPSClient();

client.connect(host);

如果您需要自定义 SSLContext,只需使用相应的 FTPSClient 重载,例如:

FTPSClient client = new FTPSClient(ctx);

client.connect(host);