【问题标题】:SFTP connection issue with DefaultSftpSessionFactoryDefaultSftpSessionFactory 的 SFTP 连接问题
【发布时间】:2013-07-31 21:12:51
【问题描述】:

我正在尝试使用org.springframework.integration.sftp.session.DefaultSftpSessionFactory 连接到一台特定的 SFTP 服务器。

建立连接后立即失去连接。

Caused by: com.jcraft.jsch.JSchException: failed to send channel request
    at com.jcraft.jsch.Request.write(Request.java:65)
    at com.jcraft.jsch.RequestSftp.request(RequestSftp.java:47)
    at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:217)
    at com.jcraft.jsch.Channel.connect(Channel.java:208)
    at com.jcraft.jsch.Channel.connect(Channel.java:145)
    at 

使用同一个库,我可以毫无问题地连接到多个不同的 SFTP 服务器。

然后我尝试使用以下命令从命令行连接但失败了。

sftp -oIdentityFile=sftp_user_rsa -oUser=sftp_user sftp.zzzz.com

在尝试了多次不同的参数后,我在指定子系统后连接。

sftp -oIdentityFile=sftp_user_rsa -oUser=sftp_user -s/usr/libexec/sftp-server sftp.zzzz.com

filezilla 也可以正常连接。

在后台 DefaultSftpSessionFactory 正在使用 sftp 通道并将子系统设置为 sftp。 那部分是硬编码的。

有没有办法在这个库中使用不同的子系统?

非常感谢


扩展库(Spring 集成 SFTP)和它使用的库(JSch)后仍然无法正常工作。即使从源代码更改 RequestSftp 并替换硬编码子系统;

 public class RequestSftp extends Request{   
     RequestSftp(){
     setReply(true);   
 }   
 public void request(Session session, Channel channel) throws Exception{
     super.request(session, channel);

     Buffer buf=new Buffer();
     Packet packet=new Packet(buf);
     packet.reset();
     buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
     buf.putInt(channel.getRecipient());
     buf.putString(Util.str2byte("subsystem"));
     buf.putByte((byte)(waitForReply() ? 1 : 0));
     buf.putString(Util.str2byte("the-new-subsystem"));
     write(packet);   
} 

}

【问题讨论】:

    标签: spring sftp spring-integration jsch


    【解决方案1】:

    请打开'Improvement' JIRA Issue。目前,您必须继承(或克隆)DefaultSftpSessionFactoryDefaultSftpSession,覆盖工厂中的 getSession() 方法。不幸的是,会话中的connect() 是包私有的,因此您不能简单地覆盖它。

    看来你只需要更改这两行

    SftpSession sftpSession = new SftpSession(jschSession);
    sftpSession.connect();
    
    MySftpSession sftpSession = new MySftpSession(jschSession);
    sftpSession.connect(subsystem);
    

    其中subsystem 将是MySftpSessionFactory 上的属性,connect(String subsystem) 将类似于 SftpSession 上的connect()

    但是,我注意到getSession() 使用了一些其他私有方法(例如initJschSession),因此复制工厂并为其命名可能更容易。我不想这样建议,但现在对子类化不是很友好。请在 JIRA issue 中记下(除了添加子系统,我们应该让工厂更容易子类化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 2015-12-14
      相关资源
      最近更新 更多