我使用jsch从java连接sftp服务器,并发送一个文件到一个特定的目录。
private static boolean enviarCSV(String localFile) throws IOException, JSchException, SftpException {
logger.info("Enviando fichero a maquina destino..");
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
String remoteDir = Config.getParametro("wssf.conf.remoteDir");
channelSftp.put(localFile, remoteDir + localFile);
logger.info("Enviado con exito!");
channelSftp.exit();
return false;
}
private static ChannelSftp setupJsch() throws JSchException {
JSch jsch = new JSch();
String user = Config.getParametro("wssf.conf.login.usuario");
String password = Config.getParametro("wssf.conf.login.password");
String remoteHost = Config.getParametro("wssf.conf.remotehost");
Session jschSession = jsch.getSession(user, remoteHost, 40020);
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.setPassword(password);
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
我需要使用公钥来连接SFTP服务器,我是安全方面的新手,我不知道该怎么做,另外我只看到使用私钥的例子,但我认为我不需要它,是吗?我很新的安全,我不知道如何做到这一点,另外我只看到使用私钥的例子,但我认为我不需要它,我?
非常感谢,如果你帮助我Thanks
密钥是成对的。一个是私钥,一个是公钥。
要用你的公钥(你自由分享的)来验证,你需要向对方证明你实际上也有匹配的私钥(因为任何人都可能有你的公钥,但只有你有私钥)。
你通过用你的私钥enrcypt或唱一些东西来实现--这可以通过你的公钥来验证。
所以,对于 "公钥认证",你实际上也必须用私钥工作......并且可以使用参考的例子:-)