SFTP 协议栈及JSch的应用

现在项目中,从安全角度出发,更多的使用SFTP来取代FTP,以保证文件传输的安全性,本文简单的描述了SFTP的重要特性及各协议层次的交互过程,并没有深入到细节,另外也介绍了使用JSch在java应用中的开发过程,在研究过程中,查找学习了相关的技术文章,觉得有些图片能很好的描述SFTP协议,也引用了,并描述了原文的出处

1:协议栈

SFTP是SSH File Transfer Protocol的缩写,是一种建立在SSHv2之上的安全的文件传输协议,协议本身只是一个传输协议,其认证与安全性由SSH协议层提供保证,SSH2 满足FIPS 140-1 and 140-2 NIST/U.S.标准,可以有效防止密码嗅探及中间人攻击,SSH是TCP的上层协议,完整的协议栈如下图所示:

  • SSH Transport Layer Protocol: Provides server authentication, data confidentiality, and data integrity with forward secrecy (i.e. if a key is compromised during one session, the knowledge does not affect the security of earlier sessions). The transport layer may optionally provide compression
  • SSH User Authentication Protocol: This protocol authenticates user with server.
  • SSH Connection Protocol: This protocol type relies on the other two layers. It allows you to open one or several logical channels (multiplexing) above the first two layers over single SSH connection, and controls the data flow.

(*注:参见文档 http://www.rfwireless-world.com/Terminology/SSL-Handshake-Protocol-vs-SSH-Protocol-Stacks.html) 

SFTP协议更像一个远程文件系统,可以对远程文件进行一系列的操作,如恢复中断传输,目录列表,文件删除等

2:SSH 协议图解

下图描述了SFTP 3层协议的交互过程

 (*注:参见 https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch03_05.htm)

3:SSH协议认证方式

SSH协议提供了安全可靠的保证,在SSH2中可以提供多种认证方式

  • password 认证
  • public key 认证
  • password + public key 认证

SSH协议支持客户端和服务端的双向认证,这个得益于Identity Key的使用,

认证客户:客户端生成一对密钥 User Identity Key,将其中的public key 分发到server 端,客户端请求时,用private 可以加密数据,服务端用public key解密数据

认证server:为了防止man-in-the-middle (中间人)攻击,SFTP server将创建一对public/private key,其中的public key就是host key,当client连接server时,host key将被返回到client,client端提示接受host key(只在第一次存在提示),一旦接受后,这个host key将被保存在client端,然后被用来在每次连接SFTP server时,检验与server传来的host key是否匹配,这个验证过程在client authentication过程之前执行

4:SSH 连接过程

(*注:参见文档 http://www.rfwireless-world.com/Terminology/SSL-Handshake-Protocol-vs-SSH-Protocol-Stacks.html) 

5:java 工程JSch的应用

 JSch是SSHv2的一个java实现,可以用来连接SFTP server并进行文件操作,在基于maven的工程中,按以下步骤构建应用

  • 在pom文件中添加依赖
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
  • 在java文件中导入相应的包
import com.jcraft.jsch.*;
  • 建立连接及操作文件
public class SftpUtility {
    private static ChannelSftp sftpChannel = null;

    protected void connect(JSch jsch, String host, int port,
                           String user, String credential) throws Exception {
        try {
            Session session = jsch.getSession(user, host, port);
            session.setPassword(credential);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp) channel;
        }
        catch (Exception e)
        {
         ......
        }
    }

    protected void disconnect() {
        if(sftpChannel != null && sftpChannel.isConnected()) {
            try {
                if (sftpChannel.getSession() != null) {
                    sftpChannel.getSession().disconnect();
                }
            }
            catch (Exception e){
              ......
            }
            sftpChannel.disconnect();

        }
    }

    public void upload(String localPathName, String sftpTargetPath, String sftpTargetName)
    {
        try {
            sftpChannel.cd(sftpTargetPath);
        }
        catch (SftpException e)
        {
            try {
                sftpChannel.mkdir(sftpTargetPath);
                sftpChannel.cd(sftpTargetPath);
            }
            catch (SftpException se)
            {
             ......
            }
        }

        try {
            sftpChannel.put(localPathName, sftpTargetName);
        }
        catch (SftpException se)
        {
         ......
        }
    }

    public void download(String sourcePath, String targetName)
    {
        try {
            sftpChannel.get(sourcePath,targetName);
        }
        catch (SftpException se)
        {
         ......
        }
    }
}
原文地址:https://www.cnblogs.com/benfly/p/9970667.html