gpt4 book ai didi

ssh - Apache MINA SSHD - 连接到服务器时的身份验证方法问题

转载 作者:行者123 更新时间:2023-12-05 01:20:46 25 4
gpt4 key购买 nike

我正在尝试关注 Apache MINA 的 guide用于设置 SSHD 服务器,但在使用 PuTTY 连接到它时遇到问题。我开始输入用户名,但随后收到以下错误消息: enter image description here

下面是我的服务器的代码。我是否必须为服务器手动设置身份验证方法?还是我做错了什么?

sshd = SshServer.setUpDefaultServer();
sshd.setPort(3333);

List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
sshd.setUserAuthFactories(userAuthFactories);

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

sshd.start();

最佳答案

我刚刚创建了一个适合我的示例。我使用 Putty 连接到 Apache SSHD。我使用的 Lib 版本是“apache-sshd-0.9.0”(下载时间:2014-02-04)。 JDK 1.7。

我的示例的主要内容如下:

public static void main(String[] args) throws IOException
{
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("./keys/mysample",
SimpleGeneratorHostKeyProvider.SSH_RSA));

List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthPassword.Factory());
sshd.setUserAuthFactories(userAuthFactories);

sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return "tomek".equals(username) && "123".equals(password);
}
});

sshd.setShellFactory(new MyCommandFactory());

sshd.start();
while (true) ;
}

如您所见,我创建了自己的命令工厂,如下所示:(顺便说一句。this question on SO 对我很有帮助)

public class MyCommandFactory  implements CommandFactory, Factory<Command>
{
public Command createCommand(String s)
{
System.out.println("command requested: " + s);
return new MyCommand(s);
}

public Command create()
{
return createCommand("none");
}
}

为了测试目的创建一个这样的命令:

public class MyCommand implements Command
{

private String name;

private InputStream inputStream;
private OutputStream outputStream;
private ExitCallback exitCallback;

public MyCommand(String name)
{
this.name = name;
}

public void setInputStream(final InputStream inputStream)
{
System.out.println("input stream was set on command: " + name);
this.inputStream = inputStream;
}

public void setOutputStream(final OutputStream outputStream)
{
System.out.println("output stream was set on command: " + name);
this.outputStream = outputStream;
}

public void setErrorStream(OutputStream outputStream)
{
System.out.println("error stream was set on command: " + name);
}

public void setExitCallback(ExitCallback exitCallback)
{
System.out.println("exit callback was set on command: " + name);
this.exitCallback = exitCallback;
}

public void start(Environment environment) throws IOException
{
try
{

final PrintWriter writer = new PrintWriter(outputStream, true);
writer.println("Welcome to Java SSH echo server!");
writer.flush();

// ... here you gonna read input from remote client ...
// use writer to write back responses
}
catch (final Exception e)
{
e.printStackTrace();
}
}

public void destroy()
{
System.out.println("destroy was called on command: " + name);
// client or server closed connection, clean up resources here
}
}

如您所见,我使用了 key 文件“./keys/mysample”。要创建这样一个 key 文件试试这个:(顺便说一句。this question on SO 对此很有帮助)

keytool -genkey -keystore ./keys/mysample" -keyalg RSA

瞧:

result of this sample

我希望这对某人有帮助。

[编辑] 在这里查看一个更好的回显服务器: http://svn.apache.org/repos/asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/EchoShellFactory.java

关于ssh - Apache MINA SSHD - 连接到服务器时的身份验证方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18694108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com