【问题标题】:apache sshd public key authenticationapache sshd 公钥认证
【发布时间】:2013-02-28 14:57:24
【问题描述】:

我正在尝试使用 apache mina sshd 设置 ssh 服务器。我想使用公钥认证,基本上我想知道如何实现

package org.apache.sshd.server;
import java.security.PublicKey;
import org.apache.sshd.server.session.ServerSession;

public interface PublickeyAuthenticator {

boolean authenticate(String username, PublicKey key, ServerSession session);

}

我看到传递的是另一个公钥。所以我假设您应该将参数中给出的公钥与服务器拥有的公钥进行比较。但我不知道该怎么做。

我发现的一件事是this 实现。这似乎毫无意义,因为它似乎将公钥的模数与自身进行比较。假设这个实现有一个错误,并且应该比较每个公钥的模数,这足以进行身份​​验证 - 模数同意吗?当然,如果我只是将我的公开可用公钥提供给这个函数,那么我会得到身份验证吗?

【问题讨论】:

    标签: java authorization public-key sshd mina


    【解决方案1】:

    我想我在org.apache.sshd.server.auth.UserAuthPublicKey#auth 的源代码中找到了答案。此类使用密钥进行实际身份验证。我认为让我感到困惑的是方法的名称 - authenticate()。实际情况如下:

    • 服务器请求客户端的公钥

    • 公钥传递给PublickeyAuthenticator#authenticate

    • 您应该在authenticate() 中做的就是检查这是您想要允许的公钥

    • 如果 authenticate() 返回 true,则 UserAuthPublicKey#auth 将检查消息是否已使用私钥签名。如果是,则验证已通过验证。

    【讨论】:

      【解决方案2】:

      以下代码是如何使用 Apache MINA SSHD 执行公钥认证的示例,示例代码创建了一个 SFTP 服务器。

      import java.io.File;
      import java.io.IOException;
      import java.util.Collections;
      
      @Service
      public class MySftpServer {
      
          private Log log = LogFactory.getLog(MySftpServer.class);
      
          @PostConstruct
          public void startServer() throws IOException {
              start();
          }
      
          private void start() throws IOException {
              SshServer sshd = SshServer.setUpDefaultServer();
              sshd.setHost("localhost");
              sshd.setPort(2222);
              sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("host.ser")));
              sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
              sshd.setPasswordAuthenticator((username, password, session) -> username.equals("test") && password.equals("password"));
              sshd.setPublickeyAuthenticator(new AuthorizedKeysAuthenticator(new File("<Location of authorized_keys file>")));
              sshd.start();
              log.info("SFTP server started");
          }
      }
      

      类似于@northshorefiend 在他的回答中提到的内容,在这种情况下,AuthorizedKeysAuthenticator 将公钥传递给服务器,并根据 authorized_keys 文件 new AuthorizedKeysAuthenticator(new File("&lt;Location of authorized_keys file&gt;") 对其进行验证。如果文件中存在指定的公钥,则认证通过。

      您可以阅读有关此herehere 的更多详细信息

      【讨论】:

      • 嗨,我试过这个,但我一直收到这个错误:test@localhost: Permission denied (keyboard-interactive,publickey)。我究竟做错了什么?这就是我尝试连接的方式: $ sftp -oPort=2222 test@localhost 来自我的私钥所在的目录
      猜你喜欢
      • 2018-11-08
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多