如何基于FTP4J实现FTPS连接过程解析

作者:cuisuqiang 时间:2022-09-19 21:51:35 

FTPS:

一种多传输协议,相当于加密版的FTP。当你在FTP服务器上收发文件的时候,你面临两个风险。第一个风险是在上载文件的时候为文件加密。第二个风险是,这些文件在你等待接收方下载的时候将停留在FTP服务器上,这时你如何保证这些文件的安全。你的第二个选择(创建一个支持SSL的FTP服务器)能够让你的主机使用一个FTPS连接上载这些文件。这包括使用一个在FTP协议下面的SSL层加密控制和数据通道。一种替代FTPS的协议是安全文件传输协议(SFTP)。这个协议使用SSH文件传输协议加密从客户机到服务器的FTP连接。

FTPS是在安全套接层使用标准的FTP协议和指令的一种增强型FTP协议,为FTP协议和数据通道增加了SSL安全功能。FTPS也称作“FTP-SSL”和“FTP-over-SSL”。SSL是一个在客户机和具有SSL功能的服务器之间的安全连接中对数据进行加密和解密的协议。

当使用FTPS与服务器连接时,有两种方法:显式和隐式。

简单来说:

显示又叫FTPES, FTPS客户端跟FTPS服务器必须显式使用一种同样的加密方法。如果客户端不要求加密,服务器也允许非加密通讯。

隐式 就是客户端直接通过TSL/SSL加密与服务器联系,如果服务器无响应,则停止通讯。

FTP4J 支持 FTPS/FTPES secured connection,其中使用FTPES还是原来的21端口,使用FTPS使用的是990端口,使用SFTP的是22端口,以下说的不包含SFTP内容。

可以查看Serv-U域详细信息查看服务邦定的端口,默认情况下是以下内容:

如何基于FTP4J实现FTPS连接过程解析

如果我们使用flashfxp进行连接,则使用不同连接方式时要进行选择,普通FTP连接使用21端口,不用选择:

如何基于FTP4J实现FTPS连接过程解析

我们通过21端口进行显示FTPS连接:


package test;
import it.sauronsoftware.ftp4j.FTPClient;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* 通过21端口进行显示FTPS连接
* @说明
* @author cuisuqiang
* @version 1.0
* @since
*/
public class Ftp4jTest {
public static void main(String[] args) {
try {
TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}
} };
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManager, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
FTPClient client = new FTPClient();
client.setSSLSocketFactory(sslSocketFactory);
client.setSecurity(FTPClient.SECURITY_FTPES);
client.connect("192.168.1.122", 21);
client.login("123", "123123");
System.out.println(client.toString());
System.out.println(client.currentDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
}

代码会打印连接信息和当前目录

使用990端口进行隐式FTPS连接:


package test;
import it.sauronsoftware.ftp4j.FTPClient;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* 进行隐式FTPS连接
* @说明
* @author cuisuqiang
* @version 1.0
* @since
*/
public class Ftp4jTest {
public static void main(String[] args) {
try {
TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}
} };
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManager, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
FTPClient client = new FTPClient();
client.setSSLSocketFactory(sslSocketFactory);
client.setSecurity(FTPClient.SECURITY_FTPS);
client.connect("192.168.1.122", 990);
client.login("123", "123123");
System.out.println(client.toString());
System.out.println(client.currentDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
}

打印内容相同

进行显示还是隐式连接的最大不同是指定了连接方式:

这个情况官方也给出了详细的说明:

The ftp4j library supports both FTPS (FTP over implicit TLS/SSL) and FTPES (FTP over explicit TLS/SSL).
The setSecurity() method can be used to turn on the feature:
client.setSecurity(FTPClient.SECURITY_FTPS); // enables FTPS
client.setSecurity(FTPClient.SECURITY_FTPES); // enables FTPES
Both methods must be called before connecting the remote server.
If the security is set to SECURITY_FTPS, the default port used by the connect() method changes to 990

来源:https://www.iteye.com/blog/cuisuqiang-1774840

标签:FTP,4J,FTPS,连接
0
投稿

猜你喜欢

  • SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法

    2023-11-01 16:03:39
  • Spring Security添加验证码的两种方式小结

    2021-08-05 17:24:25
  • 使用IDEA异常断点来定位java.lang.ArrayStoreException的问题

    2022-06-14 00:43:18
  • java(包括springboot)读取resources下文件方式实现

    2021-06-03 20:16:06
  • SpringMvc后台接收json数据中文乱码问题详解

    2022-12-03 00:08:58
  • SpringCloud消息总线Bus配置中心实现过程解析

    2023-02-14 10:05:18
  • Spring深入探索AOP切面编程

    2023-05-27 09:37:16
  • Java中不可或缺的关键字volatile详析

    2023-07-22 12:11:29
  • C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法

    2023-01-30 04:11:58
  • 教你使用java实现去除各种空格

    2022-09-21 21:27:07
  • Mybatis中的@Select、foreach用法

    2023-06-05 00:07:20
  • Java二维数组计算集合总结

    2023-02-15 16:38:14
  • Java后台通过Collections获取list集合中最大数,最小数代码

    2023-03-15 19:53:05
  • Java Document生成和解析XML操作

    2021-11-10 13:17:46
  • Java实现驼峰和下划线互相转换的示例代码

    2023-12-16 15:23:48
  • 总结Java对象被序列化的两种方法

    2023-05-11 09:46:52
  • 解决Spring在Thread中注入Bean无效的问题

    2022-06-26 13:03:59
  • C#实现一键清空控件值的示例代码

    2021-11-12 12:10:15
  • MyBatis的SQL执行结果和客户端执行结果不一致问题排查

    2022-12-30 20:37:22
  • SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码

    2023-08-15 04:12:46
  • asp之家 软件编程 m.aspxhome.com