I'm attempting to build an SFTP connection via Spring Boot. I'm using password authentication rather than PrivateKey. When I fire up my application it first attempts to authenticate over gssapi-with-mic and Kerberos prompts for credentials. After I key press through the Kerberos the application then attempts to look for a Private Key. After not finding any details regarding the key it will finally attempt the credentials supplied to the Session Factory and work as intended. As this application is going to be living within a Docker build I need it to attempt the password authentication first.
At this point I've attempted to set session properties, setting AllowUnkownKeys to false and removing it entirely with no success.
@SpringBootApplication
public class SFTPConnector {
public static void main(String[] args){
new SpringApplicationBuilder(SFTPConnector.class).run(args);
}
@Bean
Properties configProperties(){
Properties config = new Properties();
config.setProperty("PreferredAuthenticationMethod", "PASSWORD");
return config;
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller(){
System.out.println("Initializing Poller");
PollerMetadata pollerMetadata = new PollerMetadata();
pollerMetadata.setTrigger(new PeriodicTrigger(6000));
return pollerMetadata;
}
@Bean
SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory(){
System.out.println("Creating Session");
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost("someSFTPServer");
factory.setUser("user");
factory.setPassword("SomePassword");
factory.setPort(22);
factory.setAllowUnknownKeys(true);
factory.setSessionConfig(configProperties());
return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
}
@Bean
SftpInboundFileSynchronizer sftpInboundFileSynchronizer(){
System.out.println("In File Synchronizer");
SftpInboundFileSynchronizer fileSync = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSync.setDeleteRemoteFiles(false);
fileSync.setRemoteDirectory("SomeDir");
fileSync.setFilter(new SftpSimplePatternFileListFilter("*.csv"));
return fileSync;
}
@Bean
@InboundChannelAdapter("sftpChannel")
public MessageSource<File> sftpMessageSource(){
System.out.println("Inside SFTP Message Source");
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
source.setLocalDirectory(new File("/tmp/local_inbound"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
MessageHandler messageHandler(){
System.out.println("Inisde Message Handler");
return new MessageHandler() {
@Override
public void handleMessage(Message<?> arg0) throws MessagingException {
File f = (File) arg0.getPayload();
System.out.println(f.getName());
}
};
}
}`
I'm getting: 019-01-04 11:22:30.938 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : SSH_MSG_NEWKEYS sent 2019-01-04 11:22:30.938 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : SSH_MSG_NEWKEYS received 2019-01-04 11:22:30.943 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : SSH_MSG_SERVICE_REQUEST sent 2019-01-04 11:22:30.948 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : SSH_MSG_SERVICE_ACCEPT received 2019-01-04 11:22:30.952 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Authentications that can continue: gssapi-with-mic,publickey,keyboard-interactive,password 2019-01-04 11:22:30.953 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Next authentication method: gssapi-with-mic Kerberos username [bradley.dudra]: Kerberos password for bradley.dudra: 2019-01-04 11:34:28.546 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Authentications that can continue: publickey,keyboard-interactive,password 2019-01-04 11:34:28.547 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Next authentication method: publickey 2019-01-04 11:34:28.548 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Authentications that can continue: password 2019-01-04 11:34:28.548 INFO 11240 --- [ask-scheduler-1] com.jcraft.jsch : Next authentication method: password
After this point the authentication works. I need to omit the attempt to authenticate via password from the start.
I think your property in session configuration is wrong: try with the following :
config.setProperty("PreferredAuthentications", "password");
have a look at the supported properties Here