尝试使用 spring-integration-sftp 方法通过 SFTP 传输文件时,请参阅以下内容。
错误
org.apache.sshd.common.SshException:无法协商 kex 算法的密钥交换。
使用其他方法(JSch)我可以将文件传输到远程服务器。
SftpConfig.java
@Configuration
public class SftpConfig{
@Value("${sftp.host}")
private String host;
@Value("${sftp.port}")
private int port;
@Value("${sftp.user}")
private String user;
@Value("${sftp.password}")
private String password;
@Value("${sftp.remoteDirectory}")
private String remoteDirectory;
@Bean
public SessionFactory<SftpClient.DirEntry> sftpSessionFactory(){
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
HostConfigEntry hostConfigEntry = new HostConfigEntry("",host,port,user);
hostConfigEntry.setProperty("StrictHostKeyChecking","no");
hostConfigEntry.setProperty("PreferredAuthentications","password");
factory.setHostConfig(hostConfigEntry);
factory.setPassword(password);
factory.setAllowedUnknownKeys(true);
return new CachingSessionFactory(factory,0)
}
@Bean(name="toSftpChannelTesting")
@ServiceActivator(inputChannel = "toChannelTesting")
public NessageHandler testingMessageHandler(){
SftpMessageHandler handlr = new SftpMessageHandler(sftpSessionFactory());
handlr.setRemoteDirectoryExpression(new LiteralExpression(remoteDirectory));
handlr.setAutoCreateDirectory(false);
handlr.setUseTemproryFileName(false);
return handler;
}
}
FileUploadAdapter.java
@Service
public class FileUploadAdapter{
@AutoWired private TransferGateway transferGateway;
@Retryable(
retryFor = RetryException.class, maxAttempts = 2,
backoff = @Backoff(delay = 3000))
pubic void transferTestFile(File file){
transferGateway.transferTestFile(file);
}
}
TransferGateway.java
@MessagingGateway
public interface TransferGateway{
@Gateway(requestChannel = "toChannelTesting")
void transferTestFile(File file);
}
我已尝试按照以下链接中的详细信息进行操作
Spring Integration SFTP 连接失败 - 无法协商 kex 算法的密钥交换
但是我遇到了以下错误 java.lang.IllegalStateException:必须在外部提供的 SshClient 实例上配置密码
我想使用首选身份验证作为密码
我在这里做错了什么吗?
非常感谢任何对此的帮助。谢谢!
修改如下后问题得到解决
@豆子
公共 SessionFactory
SshClient client = SshClient.setUpDefaultClient();
client.setKeyExchangeFactories(NamedFactory.setUpTransformedFactories(
false,
BuiltinDHFactories.VALUES,
ClientBuilder.DH2KEX));
client.setSignatureFactories(new ArrayList<>(BuiltinSignatures.VALUES));
client.addPasswordIdentity(password);
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(client,true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
return new CachingSessionFactory(factory,0)
}