自己生成hostkey被 apachmina sshd服务端重写
时间: 2024-03-05 13:50:00 浏览: 128
Apache Mina-sshd服务端如果没有设置hostkey,会自动生成一个hostkey,如果设置了hostkey,会优先使用设置的hostkey,而不是自动生成的hostkey。因此,如果你自己生成了hostkey并将其用作Apache Mina-sshd服务端的hostkey,服务端不会对其进行重写。
以下是一个使用自己生成的hostkey的示例代码:
```java
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
import org.apache.sshd.common.config.keys.PublicKeyEntry;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.server.auth.pubkey.SimplePublicKeyAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
public class SSHServer {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
// Load the hostkey from a file
File hostKeyFile = new File("hostkeyfile");
SimpleGeneratorHostKeyProvider hostKeyProvider = new SimpleGeneratorHostKeyProvider(hostKeyFile);
// Create the SSH server
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setKeyPairProvider(hostKeyProvider);
// Configure public key authentication
PublickeyAuthenticator publicKeyAuthenticator = new SimplePublicKeyAuthenticator();
PublicKeyEntry publicKeyEntry = new AuthorizedKeyEntry("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDq1Oy25jyN3x3uTDVQZC1Oq8MlK8+3+6kqCQs+4j6aGqAq5nbCwVJlbD5zKbJN1yJFJX8kVzXo4yPhS6Y/7I6sE4mFQvT5XgqD1Wl1QeN8L4q5Pv9aJsLd9FmXjCdsfZnJh3aT4s6XnLQ6YFQWvI4QaQdjC3QpE+8XKoO/RiU4jH3S8yS1XG2QJrBbG2Yx7RzUZm5O5F2RJjZQw5L6rWVJo4nL3a6zG1zVhl8Kv3LxWpqtMKzB0S+c8LwSv7q2a5W1yPznJy4eOYQXb9yYrTm8pQJ5qk5S5TjIHX5y5QG2Z43v/2xFyQHqy7CHYKlR8K9K2mTm7QgE8O0t1a24VQW1xQZ8QrjK+qJIViFzUx8P");
publicKeyAuthenticator.addAuthorizedKey(publicKeyEntry);
sshd.setPublickeyAuthenticator(publicKeyAuthenticator);
// Start the SSH server
sshd.start();
System.out.println("SSH server is running");
}
}
```
该程序将从名为hostkeyfile的文件中加载hostkey,并将其用作Apache Mina-sshd服务端的hostkey。在配置公钥认证时,将使用一个硬编码的公钥作为示例。你应该将其替换为你自己的公钥。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)