Apache Mina SSHD 1.0 服务器立即退出

Apache Mina SSHD 1.0 Server exits immediately

我正在使用 Apache Mina sshd-core-1.0.0 启动 SFTP 守护程序。然而,程序在 sshd.start() 之后退出。没有错误。但是,如果我使用 sshd-core-0.0.14,服务器启动正常,我可以启动 SFTP 会话。我是否遗漏了 1.0.0 的内容?

1.0.0 的代码片段(不起作用)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File ("hostkey.ser")))   
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

0.0.14 的代码片段(有效)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); 
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

1.0.0运行时输出如下。代码开始正常,但在 sshd.start() 语句后终止。

2015-12-16 19:57:38,510 DEBUG SFTPServer.main(SFTPServer.java:26) message
2015-12-16 19:57:38,767 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:145) Trying to register BouncyCastle as a JCE provider
2015-12-16 19:57:39,076 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:149) Registration succeeded
2015-12-16 19:57:39,105 DEBUG org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:57) Binding Nio2Acceptor to address 0.0.0.0/0.0.0.0:2222
2015-12-16 19:57:39,114 INFO SFTPServer.main(SFTPServer.java:32) Started

SshServer.Start 仅开始侦听传入端口。它不会阻塞。所以 main 之后立即终止。这在 0.0.14 中应该没有什么不同,虽然我不能尝试。

您必须在 main 中等待明确保留服务器 运行。

查看 SshServer.main 是如何实现的(在 0.0.14 和 1.0.0 中):

public static void main(String[] args) throws Exception {

    ...

    SshServer sshd = SshServer.setUpDefaultServer();

    ...

    sshd.start();

    Thread.sleep(Long.MAX_VALUE);
}

我遇到了同样的问题,这是因为SSHD不知道使用哪个网络库。 我添加了 Netty 包:

<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-netty</artifactId>
    <version>2.6.0</version>
</dependency>

它在没有 Thread.sleep() 调用的情况下工作。在日志中您将看到:

[main] INFO org.apache.sshd.common.io.DefaultIoServiceFactoryFactory - Using NettyIoServiceFactoryFactory

然后 Netty 开始响应:

[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] REGISTERED
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] BIND: 0.0.0.0/0.0.0.0:2222
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050, L:/[0:0:0:0:0:0:0:0]:2222] ACTIVE

我没有在 Maven 中添加任何其他 Netty 库。