我正在编写一个应用程序,它应该使用 JSch 通过 SSH 访问我的私人服务器。因为我已经设置了公钥身份验证,所以我希望这个应用程序以相同的方式进行身份验证。我将是唯一一个使用此应用程序的人,因此我想将我的密钥直接存储在应用程序中(例如,硬编码)或在手机主目录中的某个位置分开存储。哪种是最好的存储方式,也许是作为项目中的资源文件?由于我对 Android 开发还很陌生,因此我不确定最好的方法是什么。
我试过的:
// [...]
String user = "my_user";
String ssh_pwd = "my_pwd";
String host = "my_host";
// stored as OpenSSH key - file not found error - where shoud I move this file?
String private_key = "./my_pk";
int port = 22;
// basic SSH connection stuff
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
jsch.addIdentity(private_key, ssh_pwd.getBytes());
Run Code Online (Sandbox Code Playgroud)
其它的办法:
// [...]
// private key in OpenSSH format as a plain string
String private_key = …
Run Code Online (Sandbox Code Playgroud) 我需要从 hadoop 集群连接到 sftp 服务器。我想知道是否有办法从 hdfs 中存储的私钥加载身份。实际上,JSch 对象似乎只接受本地路径:
try {
String privateKeyPath = "hdfs://namenode:8020/path/to/privatekey"; // need this one to be an hdfs path
JSch jsch = new JSch();
jsch.addIdentity(privateKeyPath);
// [..]
}
catch (Exception ex) {
// [..]
}
Run Code Online (Sandbox Code Playgroud)
任何想法?