我需要将代码从Perl迁移到Java。
我在perl:中有这样的代码
use Net::FTPSSL;
print "RUNn";
my $ftpdebug = 0;
my $ip = "...";
my $port = 2121;
my $atmpassword = "...";
my $atmuser = "...";
my $ftp = Net::FTPSSL->new( $ip, Port => $port, Debug => $ftpdebug );
if ($ftp) {
print "SUCCESSn";
$ftp->login( $atmuser, $atmpassword );
print "LOGIN n";
@list = $ftp->list();
for $i (@list) {
print $i . "n";
}
} else {
print "FAILn";
}
而且效果很好。它向我输出了一个服务器上的文件列表。但当我尝试在Java中使用相同的主机和端口时,我甚至无法连接到服务器。我正在尝试:
try {
FTPSClient ftpClient = new FTPSClient(false);
// Connect to host
ftpClient.connect(host, 2121);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
if (ftpClient.login(name, password)) {
// Set protection buffer size
ftpClient.execPBSZ(0);
// Set data channel protection to private
ftpClient.execPROT("P");
// Enter local passive mode
ftpClient.enterLocalPassiveMode();
log.info(ftpClient.printWorkingDirectory());
// Logout
ftpClient.logout();
} else {
log.info("FTP login failed");
}
// Disconnect
ftpClient.disconnect();
} else {
log.info("FTP connect to host failed: " + reply);
}
} catch (IOException ioe) {
log.info("FTP client received network error");
log.info(ioe.getMessage());
ioe.printStackTrace();
} catch (NoSuchAlgorithmException nsae) {
log.info("FTP client could not use SSL algorithm");
}
它在这条线上失败了:
ftpClient.connect(host, 2121);
错误是:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExpiredException: NotAfter: Fri Dec 19 15:47:22 EET 2008
据我所知,FTPSClient使用的协议与Net::FTPSSL-FTP over SSL相同。那么我做错了什么?
据我所知,FTPSClient使用的协议与Net::FTPSSL-FTP over SSL相同。
Net::FTPSSL默认情况下不验证证书(因此对中间人攻击是开放的)。FTPSClient验证证书并发出嘎嘎声,因为证书已在2008年过期:"CertificateExpiredException:NotAfter:Fri Dec 19 15:47:22 EET 2008"。
据我所知,FTPSClient使用的协议与Net::FTPSSL-FTP over SSL相同。
您尝试使用无效(因为过期)证书连接到网站。FTPSClient正确拒绝连接,而Net::FTPSSL盲目连接。
此代码运行良好。但正如@Steffen Ullrich所说,这是一种不安全的方式。
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
FTPSClient ftpClient = new FTPSClient(sslContext);