Jsch 在尝试连接到 AWS Transfer 系列下的 SFTP 服务器时断开连接

问题描述 投票:0回答:1

我正在尝试使用 Jsch 连接到 SFTP 服务器。我的代码如下所示

public void putFile(
      final SftpConfig sftpConfig, final InputStream inputStream, final String remoteFileName) {
    Session sftpSession = null;
    Channel sftpChannel = null;
    try {
      JSch.setLogger(new MyLogger());
      val sftpShell = new JSch();
      if (sftpConfig.hasSecretKey()) {
        sftpShell.addIdentity(
            "dev03sftp.pem", getPassword(sftpConfig.getSecretKey()).getBytes(), null, null);
      }

      sftpSession =
          sftpShell.getSession(
              sftpConfig.getUsernamePassword().getUsername(),
              sftpConfig.getRemoteHost(),
              sftpConfig.getRemotePort());

      if (sftpConfig.hasUsernamePassword()) {
        sftpSession.setPassword(getPassword(sftpConfig.getUsernamePassword()));
      }
      
      sftpSession.setConfig("StrictHostKeyChecking", "no");
      sftpSession.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");

      sftpSession.connect();
      sftpChannel = sftpSession.openChannel("sftp");
      sftpChannel.connect();

      log.info("sftp session connected");
      putFileInternal(sftpConfig, (ChannelSftp) sftpChannel, inputStream, remoteFileName);
    } catch (final JSchException jSchException) {
      throw ExceptionUtils.internalErrorException(
          CommonError.CONNECTION_ERROR, "Error in establishing sftp session", jSchException);
    } catch (SftpException sftpException) {
      throw ExceptionUtils.internalErrorException(
          CommonError.CONNECTION_ERROR, "Error in putting file via sftp session", sftpException);
    } finally {
      if (sftpChannel != null && sftpChannel.isConnected()) {
        sftpChannel.disconnect();
      }
      if (sftpSession != null && sftpSession.isConnected()) {
        sftpSession.disconnect();
      }
    }
  }

我从

sftpSession.connect();
收到以下错误。

INFO: Connecting to uploads-dev03.dev.spotnana.com port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-AWS_SFTP_1.1
INFO: Local version string: SSH-2.0-JSCH_0.2.13
INFO: CheckCiphers: [email protected]
INFO: CheckKexes: curve25519-sha256,[email protected],curve448-sha512
INFO: CheckSignatures: ssh-ed25519,ssh-ed448
DEBUG: server_host_key proposal before known_host reordering is: ssh-dss,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
DEBUG: server_host_key proposal after known_host reordering is: ssh-dss,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: server proposal: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1
INFO: server proposal: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa
INFO: server proposal: ciphers c2s: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
INFO: server proposal: ciphers s2c: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
INFO: server proposal: MACs c2s: [email protected],umac-128-[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: server proposal: MACs s2c: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: server proposal: compression c2s: none,[email protected]
INFO: server proposal: compression s2c: none,[email protected]
INFO: server proposal: languages c2s: 
INFO: server proposal: languages s2c: 
INFO: client proposal: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
INFO: client proposal: host key algorithms: ssh-dss,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
INFO: client proposal: ciphers c2s: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
INFO: client proposal: ciphers s2c: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
INFO: client proposal: MACs c2s: [email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: client proposal: MACs s2c: [email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: client proposal: compression c2s: none
INFO: client proposal: compression s2c: none
INFO: client proposal: languages c2s: 
INFO: client proposal: languages s2c: 
INFO: kex: algorithm: curve25519-sha256
INFO: kex: host key algorithm: rsa-sha2-512
INFO: kex: server->client cipher: aes128-ctr MAC: [email protected] compression: none
INFO: kex: client->server cipher: aes128-ctr MAC: [email protected] compression: none
INFO: SSH_MSG_KEX_ECDH_INIT sent
INFO: expecting SSH_MSG_KEX_ECDH_REPLY
INFO: ssh_rsa_verify: rsa-sha2-512 signature true
WARN: Permanently added 'uploads-dev03.dev.spotnana.com' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_EXT_INFO received
INFO: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected]>
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Disconnecting from uploads-dev03.dev.spotnana.com port 22

com.spotnana.exceptions.SpotnanaRuntimeException: INTERNAL: Error in establishing sftp session

    at com.spotnana.exceptions.ExceptionUtils.internalErrorException(ExceptionUtils.java:295)
    at com.spotnana.ens.channelproviders.file.SftpFileDeliverer.putFile(SftpFileDeliverer.java:74)
    at com.spotnana.ens.channelproviders.file.SftpFileDelivererIntegrationTest.testCloudIntegration(SftpFileDelivererIntegrationTest.java:50)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
    at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
    at com.jcraft.jsch.Session.connect(Session.java:550)
    at com.jcraft.jsch.Session.connect(Session.java:189)
    at com.spotnana.ens.channelproviders.file.SftpFileDeliverer.putFile(SftpFileDeliverer.java:67)
    ... 71 more
Caused by: java.io.IOException: End of IO Stream Read
    at com.jcraft.jsch.IO.getByte(IO.java:95)
    at com.jcraft.jsch.Session.read(Session.java:1076)
    at com.jcraft.jsch.UserAuthNone.start(UserAuthNone.java:84)
    at com.jcraft.jsch.Session.connect(Session.java:393)
    ... 73 more

版本

<!-- https://mvnrepository.com/artifact/com.github.mwiede/jsch -->
<dependency>
    <groupId>com.github.mwiede</groupId>
    <artifactId>jsch</artifactId>
    <version>0.2.13</version>
</dependency>
  • Mac - 索诺玛 14.1
  • SFTP 服务器 - AWS Transfer 系列
  • Java - openjdk 版本“17.0.5”2022 年 10 月 18 日 LTS

我在服务器端没有看到任何错误。

我可以使用 Mac SFTP 客户端连接到 SFTP 服务器。只有 Jsch 才会失败。

debrajmanna@MAC-DEBRAJMANNA-DX6QR261G3 java % sftp -i ~/.ssh/dev03sftp.pem [email protected]
Connected to uploads-dev03.dev.spotnana.com.
sftp>

debrajmanna@MAC-DEBRAJMANNA-DX6QR261G3 java % which sftp
/usr/bin/sftp
java ssh sftp jsch
1个回答
0
投票

我将私钥从 Openssh 更改为 RSA,并且成功了。

完整的工作代码

public void putFile(
      final SftpConfig sftpConfig, final InputStream inputStream, final String remoteFileName) {
    Session sftpSession = null;
    Channel sftpChannel = null;
    try {
      // JSch.setLogger(new MyLogger());
      val sftpShell = new JSch();
      if (sftpConfig.hasSecretKey()) {
        sftpShell.addIdentity(
            "key.pem", getPassword(sftpConfig.getSecretKey()).getBytes(), null, null);
        sftpSession =
            sftpShell.getSession(
                sftpConfig.getSecretKey().getUsername(),
                sftpConfig.getRemoteHost(),
                sftpConfig.getRemotePort());
      } else {
        sftpSession =
            sftpShell.getSession(
                sftpConfig.getUsernamePassword().getUsername(),
                sftpConfig.getRemoteHost(),
                sftpConfig.getRemotePort());
        sftpSession.setPassword(getPassword(sftpConfig.getUsernamePassword()));
      }

      // NB: Turn off host verification since the key is calculated on a per-client basis and will
      // be different for each env.
      sftpSession.setConfig("StrictHostKeyChecking", "no");
      sftpSession.connect();
      sftpChannel = sftpSession.openChannel("sftp");
      sftpChannel.connect();

      log.info("sftp session connected");
      putFileInternal(sftpConfig, (ChannelSftp) sftpChannel, inputStream, remoteFileName);
    } catch (final JSchException jSchException) {
      throw ExceptionUtils.internalErrorException(
          CommonError.CONNECTION_ERROR, "Error in establishing sftp session", jSchException);
    } catch (SftpException sftpException) {
      throw ExceptionUtils.internalErrorException(
          CommonError.CONNECTION_ERROR, "Error in putting file via sftp session", sftpException);
    } finally {
      if (sftpChannel != null && sftpChannel.isConnected()) {
        sftpChannel.disconnect();
      }
      if (sftpSession != null && sftpSession.isConnected()) {
        sftpSession.disconnect();
      }
    }
  }

SFTP 客户端可以同时使用两者。

© www.soinside.com 2019 - 2024. All rights reserved.