通过Java的SFTP连接要求进行奇怪的身份验证

时间:2020-12-05 20:46:19

So I'm writing a little program that needs to connect to a remote server through SFTP, pull down a file, and then processes the file. I came across JSch through some answers here and it looked perfect for the task. So far, easy to use and I've got it working, with one minor thing I'd like to fix. I'm using the following code to connect and pull the file down:

所以我正在编写一个需要通过SFTP连接到远程服务器的小程序,下拉文件,然后处理该文件。我在这里通过一些答案遇到了JSch,它看起来非常适合这项任务。到目前为止,易于使用,我已经有了它的工作,我想解决一个小问题。我正在使用以下代码连接并拉下文件:

    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession("username", "127.0.0.1", 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("password");
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.cd(REMOTE_FTP_DIR);
        sftpChannel.lcd(INCOMING_DIR);
        sftpChannel.get(TMP_FILE, TMP_FILE);
        sftpChannel.exit();
        session.disconnect();
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (SftpException e) {
        e.printStackTrace();
    }

So this works and I get the file. I'm running this code on a linux server and when I run the code JSch asks me for my Kerberos username and password. It looks like:

所以这有效,我得到了文件。我在linux服务器上运行此代码,当我运行代码时,JSch会询问我的Kerberos用户名和密码。看起来像:

Kerberos username [george]:

Kerberos用户名[george]:

Kerberos password for george:

george的Kerberos密码:

I just hit enter for both questions and then the program seems to continue on with no problems. However I need this code to be automated through a cron task and so I'd rather not having it pausing the program to ask me these two questions. Is there something I'm not supplying it so that it won't ask this? Something I need to do to stop it asking? Hopefully someone has some ideas. Thanks.

我只是点击输入两个问题然后程序似乎继续没有问题。但是我需要通过cron任务自动执行此代码,所以我宁愿不让它暂停程序来问我这两个问题。有什么我不提供它,所以它不会问这个?我需要做些什么来阻止它问?希望有人有一些想法。谢谢。

2 个解决方案

#1


68  

Thought I'd post an answer here since in case anyone else ends up running into a similar issue. Turns out I am missing a piece of code that makes all the difference. I just needed to add

以为我会在这里发布一个答案,因为如果其他人最终遇到类似的问题。事实证明,我错过了一段能让一切变得与众不同的代码。我只需要添加

session.setConfig("PreferredAuthentications", 
                  "publickey,keyboard-interactive,password");

before

session.connect();

and everything works perfectly now.

现在一切都很完美。

#2


7  

While the solution in the self-accepted answer is correct, it lacks any explanation.

虽然自我接受的答案中的解决方案是正确的,但它没有任何解释。

The problem is that the OP have a Kerberos/GSSAPI authentication set as the preferred (the JSch default). Yet OP does not seem to actually use/want it, as OP claims not to specify any username or password for the Kerberos prompts.

问题是OP将Kerberos / GSSAPI身份验证设置为首选(JSch默认设置)。然而OP似乎并没有真正使用/想要它,因为OP声称不为Kerberos提示指定任何用户名或密码。

The solution is to remove the Kerberos/GSSAPI (gssapi-with-mic) from the list of preferred authentication methods:

解决方案是从首选身份验证方法列表中删除Kerberos / GSSAPI(gssapi-with-mic):

session.setConfig(
    "PreferredAuthentications", 
    "publickey,keyboard-interactive,password");

#1


68  

Thought I'd post an answer here since in case anyone else ends up running into a similar issue. Turns out I am missing a piece of code that makes all the difference. I just needed to add

以为我会在这里发布一个答案,因为如果其他人最终遇到类似的问题。事实证明,我错过了一段能让一切变得与众不同的代码。我只需要添加

session.setConfig("PreferredAuthentications", 
                  "publickey,keyboard-interactive,password");

before

session.connect();

and everything works perfectly now.

现在一切都很完美。

#2


7  

While the solution in the self-accepted answer is correct, it lacks any explanation.

虽然自我接受的答案中的解决方案是正确的,但它没有任何解释。

The problem is that the OP have a Kerberos/GSSAPI authentication set as the preferred (the JSch default). Yet OP does not seem to actually use/want it, as OP claims not to specify any username or password for the Kerberos prompts.

问题是OP将Kerberos / GSSAPI身份验证设置为首选(JSch默认设置)。然而OP似乎并没有真正使用/想要它,因为OP声称不为Kerberos提示指定任何用户名或密码。

The solution is to remove the Kerberos/GSSAPI (gssapi-with-mic) from the list of preferred authentication methods:

解决方案是从首选身份验证方法列表中删除Kerberos / GSSAPI(gssapi-with-mic):

session.setConfig(
    "PreferredAuthentications", 
    "publickey,keyboard-interactive,password");