java - com.jcraft.jsch.JSchException : SSH_MSG_DISCONNECT: 2 Too many authentication failures 错误

标签 java spring-boot apache-camel sftp jsch

我正在尝试从 Apache Camel + JSch + SpringBoot 执行 SFTP。连接到 Linux SFTP,它正在工作,但 Windows SFTP 失败。在 WinSCP 中,Linux 和 Windows SFTP 都在工作

路线:

@Component
public class ReadSFTPFileRoute extends RouteBuilder {

    // Scenario 1 Read file from Windows using below details not working
    //default home directory /home/FTPAPPUI
    //path to be used to access files /home/FTPAPPUI/share/APP_Migration
    public static String user = "ABC+FTPAPPUI";
    public static String pass = "A=BC#d+dEfg-2HI";
    public static String host = "sftp.host.com";
    public static String path = "/share/APP_Migration";


    // Scenario 2 Read file from Linux using below details  working
    //default home directory /home/ftpappti
    //path to be used to access files /home/ftpappti/tmp/app/test1 
    public static String user = "ftpappti";
    public static String pass = "AB7cDE@F2#9Ghij";
    public static String host = "1.0.0.1";
    public static String path = "/tmp/app/test1";         

    @Override
    public void configure() throws Exception {

        from("sftp://"+user+"@"+host+path
                + "?strictHostKeyChecking=no"
                + "&useUserKnownHostsFile=false"
                + "&delete=true&moveFailed=.error"
                + "&password="+pass
                + "&preferredAuthentications=publickey,keyboard-interactive,password")
        .process(exchange -> {
            System.out.println("Processing File");
        }).to("file://src/main/resources/out/"
                + "?fileName=outfile-name.txt"
                + "&fileExist=Append");
    }
}

场景 1 的输出:(Windows) 异常

main][0;39m [36mo.a.c.c.file.remote.SftpOperations      [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations      [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations      [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations      [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpConsumer        [0;39m [2m:[0;39m Error auto creating directory: share/APP_Migration due Cannot connect to sftp://ABC+FTPAPPUI@sftp.host.com:22. This exception is ignored.

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot connect to sftp://ABC+FTPAPPUI@sftp.host.com:22
    at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:146) ~[camel-ftp-2.19.1.jar:2.19.1]
    at org.apache.camel.component.file.remote.RemoteFileConsumer.connectIfNecessary(RemoteFileConsumer.java:203) ~[camel-ftp-2.19.1.jar:2.19.1]
    at org.apache.camel.component.file.remote.SftpConsumer.doStart(SftpConsumer.java:53) ~[camel-ftp-2.19.1.jar:2.19.1]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.startService(DefaultCamelContext.java:3518) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:3835) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:3771) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3691) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3455) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3309) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:202) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3093) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3089) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3112) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3089) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3026) [camel-core-2.19.1.jar:2.19.1]
    at org.apache.camel.spring.boot.RoutesCollector.maybeStart(RoutesCollector.java:203) [camel-spring-boot-2.19.1.jar:2.19.1]
    at org.apache.camel.spring.boot.RoutesCollector.onApplicationEvent(RoutesCollector.java:178) [camel-spring-boot-2.19.1.jar:2.19.1]
    at org.apache.camel.spring.boot.RoutesCollector.onApplicationEvent(RoutesCollector.java:50) [camel-spring-boot-2.19.1.jar:2.19.1]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at com.learnjovi.route.Application.main(Application.java:10) [classes/:na]
Caused by: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 Too many authentication failures 
    at com.jcraft.jsch.Session.read(Session.java:1004) ~[jsch-0.1.54.jar:na]
    at com.jcraft.jsch.UserAuthPassword.start(UserAuthPassword.java:91) ~[jsch-0.1.54.jar:na]
    at com.jcraft.jsch.Session.connect(Session.java:470) ~[jsch-0.1.54.jar:na]
    at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:118) ~[camel-ftp-2.19.1.jar:2.19.1]
    ... 34 common frames omitted

main][0;39m [36mo.a.camel.spring.SpringCamelContext     [0;39m [2m:[0;39m Route: route4 started and consuming from: sftp://ABC+FTPAPPUI@sftp.host.com/share/APP_Migration?delete=true&moveFailed=.error&password=xxxxxx&preferredAuthentications=publickey%2Ckeyboard-interactive%2Cpassword&strictHostKeyChecking=no&useUserKnownHostsFile=false
main][0;39m [36mo.a.camel.spring.SpringCamelContext     [0;39m [2m:[0;39m Total 1 routes, of which 1 are started.

场景 2 的输出:(Linux)

main] o.a.c.c.file.remote.SftpOperations       : JSCH -> Permanently added '1.0.0.1' (RSA) to the list of known hosts.
main] o.a.camel.spring.SpringCamelContext      : Route: route4 started and consuming from: sftp://ftpappti@1.0.0.1/tmp/app/test1?delete=true&moveFailed=.error&password=xxxxxx&preferredAuthentications=publickey%2Ckeyboard-interactive%2Cpassword&strictHostKeyChecking=no&useUserKnownHostsFile=false
main] o.a.camel.spring.SpringCamelContext      : Total 1 routes, of which 1 are started.
main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) started in 6.805 seconds
main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
main] com.learnjovi.route.Application          : Started Application in 13.878 seconds (JVM running for 16.309)
Processing File

pom.xml:

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-stream</artifactId>
            <version>2.21.0</version>
        </dependency>
                <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-ftp</artifactId>
            <version>2.21.0</version>
        </dependency>
    </dependencies>

最佳答案

当在 URI 的查询字符串中使用特殊字符时,您需要用 RAW 将值括起来,否则它将变成 URL 编码。

然而,这似乎不适用于查询字符串之前的任何内容(以我的经验)。因此,用 RAW 附上您的密码值,它应该可以正常工作。

from("sftp://"+user+"@"+host+path + "?strictHostKeyChecking=no" + "&useUserKnownHostsFile=false" + "&delete=true&moveFailed=.error" + "&password=RAW("+pass+")&preferredAuthentications=publickey,keyboard-interactive,password")

关于java - com.jcraft.jsch.JSchException : SSH_MSG_DISCONNECT: 2 Too many authentication failures 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685467/

相关文章:

java - @SpringBootTest 使用测试属性(测试数据库)文件运行

java - Apache Camel : consume a rest service with jetty

java - Spring Boot 升级后 Jackson 序列化不起作用

java - 如何添加不同的属性以使用 jpa 规范进行查询

java - 如何更新 spring-data-neo4j 中的关系属性?

java - Camel 路线逻辑/输出期望

apache-camel - Camel SMPP - 服务器

java - 是否可以使用Java 8进行Android开发?

java - 在 Spring Boot 中,某些字段未在 JSON 中被解析/反序列化

java - 使用超时进行 SOAP 调用 - Spring Boot