本文介绍了Apache MINA SSHD-连接到服务器时的身份验证方法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试遵循Apache MINA的对我来说非常有帮助) 公共类MyCommandFactory实现CommandFactory,Factory< Command> { public命令createCommand(String s) { System.out.println( command request: + s); 返回新的MyCommand; } public Command create() { return createCommand( none); } } 出于测试目的,创建如下命令: 公共类MyCommand实现命令 { 私有字符串名称; private InputStream inputStream; private OutputStream outputStream; private ExitCallback exitCallback; public MyCommand(String name) { this.name =名称; } public void setInputStream(final InputStream inputStream) { System.out.println(输入流是在命令上设置的: +名称); this.inputStream = inputStream; } public void setOutputStream(final OutputStream outputStream) { System.out.println(输出流是在命令上设置的: +名称); this.outputStream = outputStream; } public void setErrorStream(OutputStream outputStream) { System.out.println(错误流是在命令上设置的: +名称); } public void setExitCallback(ExitCallback exitCallback) { System.out.println(退出回调是在命令上设置的: +名称); this.exitCallback = exitCallback; } 公共无效启动(环境环境)引发IOException { try { final PrintWriter writer = new PrintWriter(outputStream,true); writer.println(欢迎使用Java SSH回显服务器!); writer.flush(); // ...在这里,您将从远程客户端读取输入信息... //使用编写器写回响应} catch(最终异常e ) { e.printStackTrace(); } } public void destroy() { System.out.println(在命令上调用销毁: +名称); //客户端或服务器关闭的连接,在此处清理资源} } $ b如您所见,$ b 我使用了一个密钥文件'./keys/mysample'。要创建这样的密钥文件,请尝试以下操作:(btw。有关SO的问题对此很有帮助) keytool -genkey -keystore ./keys/mysample- keyalg RSA 和瞧: 我希望这对 在这里寻找一个更好的回显服务器: http://svn.apache.org/repos /asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/EchoShellFactory.java I am trying to follow Apache MINA's guide for setting up a SSHD server but I run into problems when connecting to it with PuTTY. I get to enter a username but then get slapped with the following error message:Below is the code for my server. Do I have to manually set an authentication method for the server? Or am I doing something else wrong?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(); 解决方案 i just created a sample which works for me. i use Putty to connect to the Apache SSHD.Lib version i used was 'apache-sshd-0.9.0' (downloaded on:2014-02-04). JDK 1.7.the main of my sample looks like: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) ;}as you can see, i created my own command factory like this:(btw. this question on SO was very helpful for me)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"); }}for test purpose create a command like this: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 }}as you can see, i used a key file './keys/mysample'. to create such a key file try this:(btw. this question on SO was helpful for that)keytool -genkey -keystore ./keys/mysample" -keyalg RSAand voila:i hope this is helpful for someone.[EDIT] take a look here for a even better echo server:http://svn.apache.org/repos/asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/EchoShellFactory.java 这篇关于Apache MINA SSHD-连接到服务器时的身份验证方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-16 21:16