我对此并不陌生,因此不胜感激。 我正在尝试使用私有ssh密钥和JSch连接到生产服务器,代码为:
$color-black: black;
$color-light-blue: lightblue;
.btn-link {
color: $color-black;
margin: 0;
&:hover, &:not(.collapsed) {
color: $color-light-blue;
}
}
运行此命令时,我仍然收到身份验证错误,我检查了StackOverflow上的许多帖子,看不到出了什么问题
private static Logger log = Logger.getLogger(SSHKeyConnector.class);
private final static String HOST_KEY_CHECKING = "StrictHostKeyChecking";
private final static String HOST_KEY_CHECKING_OPTION = "no";
private final static String AUTHENTICATION = "PreferredAuthentications";
private final static String AUTHENTICATION_STYLE = "publickey,keyboard-interactive,password";
private Session session;
public SSHKeyConnector() {}
public Session createSession( DataTransferParameterCollection parameters ) throws RemoteAccessException {
String host = parameters.getHostname();
int port = parameters.getPort();
String user = parameters.getHostUser();
String password = parameters.getHostPassword();
String keyFile = parameters.getKeyDirectory() + "id_rsa";
JSch jsch = new JSch();
try {
log.debug("Create an ssh connection to [" + host + "][" + port + "] as user [" + user + "]");
jsch.addIdentity( keyFile );
log.debug("Key file added to session");
session = jsch.getSession( user, host, port );
session.setConfig(AUTHENTICATION, AUTHENTICATION_STYLE);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword( password );
session.connect();
log.debug("Remote session connected");
return session;
}
catch ( JSchException je ) {
log.debug("CreateSession failed: " + je.getCause() );
je.printStackTrace();
throw new RemoteAccessException( "CreateSession failed: " + je.getCause());
}
}
答案 0 :(得分:0)
感谢您的回复,我通过更改
使此工作正常jsch.addIdentity(keyFile);
到
jsch.addIdentity(keyFile,password);
并已删除
session.setPassword(密码);
现在可以正常使用