SSH 访问 spring 启动应用程序

ssh access to spring boot application

在 Spring Boot 1.2.3.RELEASE 尝试在 JDK 1.8、Windows 7.

上设置 SSH

我将 application.properties 配置为:

shell.ssh.port=2212
shell.ssh.enabled=true
shell.auth=simple
shell.auth.simple.user.name=user
shell.auth.simple.user.password=password

当应用程序启动并尝试通过 putty 访问它时,出现以下异常:

java.lang.IllegalStateException: No session available
    at org.apache.sshd.common.AbstractSessionIoHandler.exceptionCaught(AbstractSessionIoHandler.java:52) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.exceptionCaught(DefaultIoFilterChain.java:697) ~[mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextExceptionCaught(DefaultIoFilterChain.java:483) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:46) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.exceptionCaught(DefaultIoFilterChain.java:791) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.IoFilterAdapter.exceptionCaught(IoFilterAdapter.java:111) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextExceptionCaught(DefaultIoFilterChain.java:483) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireExceptionCaught(DefaultIoFilterChain.java:471) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextSessionCreated(DefaultIoFilterChain.java:359) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.access0(DefaultIoFilterChain.java:46) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.sessionCreated(DefaultIoFilterChain.java:771) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.IoFilterAdapter.sessionCreated(IoFilterAdapter.java:79) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextSessionCreated(DefaultIoFilterChain.java:357) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireSessionCreated(DefaultIoFilterChain.java:350) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.service.IoServiceListenerSupport.fireSessionCreated(IoServiceListenerSupport.java:211) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.addNow(AbstractPollingIoProcessor.java:514) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.handleNewSessions(AbstractPollingIoProcessor.java:481) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.access0(AbstractPollingIoProcessor.java:67) [mina-core-2.0.2.jar:na]
    at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1071) [mina-core-2.0.2.jar:na]
    at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64) [mina-core-2.0.2.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_31]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]
Caused by: java.lang.AbstractMethodError: org.apache.sshd.common.keyprovider.AbstractKeyPairProvider.loadKeys()[Ljava/security/KeyPair;
    at org.apache.sshd.common.keyprovider.AbstractKeyPairProvider.getKeyTypes(AbstractKeyPairProvider.java:49) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.sshd.server.session.ServerSession.sendKexInit(ServerSession.java:331) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.sshd.server.session.ServerSession.<init>(ServerSession.java:92) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.sshd.server.session.SessionFactory.doCreateSession(SessionFactory.java:43) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.sshd.common.session.AbstractSessionFactory.createSession(AbstractSessionFactory.java:38) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.sshd.common.AbstractSessionIoHandler.sessionCreated(AbstractSessionIoHandler.java:37) ~[sshd-core-0.6.0.jar:0.6.0]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.sessionCreated(DefaultIoFilterChain.java:643) ~[mina-core-2.0.2.jar:na]
    at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextSessionCreated(DefaultIoFilterChain.java:357) [mina-core-2.0.2.jar:na]

我在这里遗漏了什么吗?

谢谢。


更新: 谢谢指点。这是我的依赖关系树:

[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.2.3.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-actuator:jar:1.2.3.RELEASE:compile
[INFO] \- org.springframework.boot:spring-boot-starter-remote-shell:jar:1.2.3.RELEASE:compile
[INFO]    +- org.crashub:crash.cli:jar:1.3.1:compile
[INFO]    +- org.crashub:crash.connectors.ssh:jar:1.3.1:compile
[INFO]    |  +- org.apache.sshd:sshd-core:jar:0.6.0:compile
[INFO]    |  +- org.apache.sshd:sshd-pam:jar:0.11.0:compile
[INFO]    |  |  \- net.sf.jpam:jpam:jar:1.1:compile
[INFO]    |  +- org.bouncycastle:bcpkix-jdk15on:jar:1.51:compile
[INFO]    |  \- org.apache.mina:mina-core:jar:2.0.2:compile
[INFO]    +- org.crashub:crash.embed.spring:jar:1.3.1:compile
[INFO]    +- org.crashub:crash.plugins.cron:jar:1.3.1:compile
[INFO]    |  \- it.sauronsoftware.cron4j:cron4j:jar:2.2.5:compile
[INFO]    +- org.crashub:crash.plugins.mail:jar:1.3.1:compile
[INFO]    |  \- javax.mail:mail:jar:1.4.7:compile
[INFO]    |     \- javax.activation:activation:jar:1.1:compile
[INFO]    +- org.crashub:crash.shell:jar:1.3.1:compile
[INFO]    \- org.codehaus.groovy:groovy:jar:2.3.11:compile

所有依赖项似乎都可以从 spring-boot-starter-remote-shell 传递。我将尝试修复手动覆盖。

我用 jdk1.8 启动了一个 spring 引导项目,添加了 spring-boot-starter-remote-shell maven 依赖项和您的配置,试了一下,一切都很顺利。

但是 java.lang.AbstractMethodError 让我怀疑是类路径问题,所以我很快 运行 在我的项目中添加了 mvn dependency:tree,除此之外,spring-boot-starter-remote-shell:jar:1.2.3.RELEASEorg.apache.sshd:sshd-core:jar:0.11.0 作为 t运行sitive 依赖项,如下所示。

[INFO] +- org.springframework.boot:spring-boot-starter-remote-shell:jar:1.2.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-actuator:jar:1.2.3.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:1.2.3.RELEASE:compile
[INFO] |  +- org.crashub:crash.cli:jar:1.3.1:compile
[INFO] |  +- org.crashub:crash.connectors.ssh:jar:1.3.1:compile
[INFO] |  |  +- org.apache.sshd:sshd-core:jar:0.11.0:compile <===THIS ONE===
[INFO] |  |  +- org.apache.sshd:sshd-pam:jar:0.11.0:compile
[INFO] |  |  |  \- net.sf.jpam:jpam:jar:1.1:compile
[INFO] |  |  +- org.bouncycastle:bcprov-jdk15on:jar:1.51:compile
[INFO] |  |  +- org.bouncycastle:bcpkix-jdk15on:jar:1.51:compile
[INFO] |  |  \- org.apache.mina:mina-core:jar:2.0.7:compile
[INFO] |  +- org.crashub:crash.embed.spring:jar:1.3.1:compile
[INFO] |  +- org.crashub:crash.plugins.cron:jar:1.3.1:compile
[INFO] |  |  \- it.sauronsoftware.cron4j:cron4j:jar:2.2.5:compile
[INFO] |  +- org.crashub:crash.plugins.mail:jar:1.3.1:compile
[INFO] |  |  \- javax.mail:mail:jar:1.4:compile
[INFO] |  |     \- javax.activation:activation:jar:1.1:compile
[INFO] |  +- org.crashub:crash.shell:jar:1.3.1:compile
[INFO] |  \- org.codehaus.groovy:groovy:jar:2.3.11:compile

再次快速查看您的堆栈跟踪显示您在类路径上似乎有不同的版本 at org.apache.sshd.common.keyprovider.AbstractKeyPairProvider.getKeyTypes(AbstractKeyPairProvider.java:49) ~[sshd-core-0.6.0.jar:0.6.0] 所以我接下来要做的是手动将此版本指定为 pom 中的依赖项。当我重新运行该应用程序并尝试通过 SSH 连接时,我遇到了与您相同的异常。

同时查看 类,AbstractKeyPairProvider.loadKeys() 方法具有不同的签名,这很可能会根据 0.11.0 合同混淆实现,例如 URLKeyPairProvider:

  • 0.6.0 protected abstract KeyPair[] loadKeys();
  • 0.11.0 public abstract Iterable<KeyPair> loadKeys();

所以,要么是你无意中添加了这个版本,要么是你的一些其他依赖项引用了它,有效地隐藏了类路径中的 0.11.0。在我的例子中,我启用了 spring boot actuator HTTP endpoints 来确认以上内容:

总而言之,检查你的依赖树并找出 sshd-core 0.6.0mina-core 2.0.2 来自哪里并排除它们。