繁体   English   中英

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 库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM