SFTP connection through Java asking for weird authentication

前端 未结 3 486
执念已碎
执念已碎 2020-12-13 00:04

So I\'m writing a little program that needs to connect to a remote server through SFTP, pull down a file, and then processes the file. I came across JSch through some answer

3条回答
  •  臣服心动
    2020-12-13 00:33

    All answers are correct, I'll just add here the way it can be done for Spring Integration when trying to integrate with an SFTP server.

    So, if you are using SFTP Spring Integration and the weird user and password for Kerberos is prompting in the same way the OP is asking.

    Then modify your Spring configuration (I'm using Java Spring Integration config, if you are using XML config you can try to translate it yourself - I really don't like XML config :P ):

    So in the bean you are using as SessionFactory you need to add this change in config:

    @Bean
    public SessionFactory sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("hostname");
        factory.setPort(22);
        factory.setUser("username");
        factory.setPassword("superstrongpassword");
        factory.setAllowUnknownKeys(true);
        factory.setSessionConfig(buildSessionProperties());
        return new CachingSessionFactory<>(factory);
    }
    
    /**
     * Build JSch property PreferredAuthentications without "gssapi-with-mic"
     * This way it won't prompt for Kerberos authentication every time it tries to connect
     * to the SFTP.
     */
    private Properties buildSessionProperties() {
        Properties sessionProperties = new Properties();
        sessionProperties.setProperty("PreferredAuthentications", "publickey,keyboard-interactive,password");
        return sessionProperties;
    }
    

提交回复
热议问题