网上找了mina sshd 相关的资料少之又少,看了一些英文文档,感觉也不是特别清楚,经过各种研究终于在项目中集成了 sshd 服务。
直接上代码
import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.PropertyResolverUtils;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.AsyncAuthException;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.password.PasswordChangeRequiredException;
import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator;
import org.apache.sshd.server.config.keys.DefaultAuthorizedKeysAuthenticator;
import org.apache.sshd.server.forward.AcceptAllForwardingFilter;
import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.ProcessShellFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Order(value = 1)
@Slf4j
public class SShServer implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setHost(ip);
sshd.setPort(Integer.valueOf(port));
AbstractGeneratorHostKeyProvider hostKeyProvider = null ;
Path hostKeyFile = null;
if (SecurityUtils.isBouncyCastleRegistered()) {
hostKeyFile = new File(key_path).toPath();
hostKeyProvider = SecurityUtils.createGeneratorHostKeyProvider(hostKeyFile);
}
else {
hostKeyFile = new File(key_path).toPath();
hostKeyProvider = new SimpleGeneratorHostKeyProvider(hostKeyFile);
}
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String pwd, ServerSession session)
throws PasswordChangeRequiredException, AsyncAuthException {
boolean flag = false;
if(name.equals(username) && password.equals(pwd)) {
flag = true;
}
log.info("[YunSShServer.run][response]username:{},pwd:{},flag:{}",username,pwd,flag);
return flag;
}
});
// 测试代码
sshd.setShellFactory(InteractiveProcessShellFactory.INSTANCE);
// sshd.setShellFactory(new ProcessShellFactory( Arrays.asList("bash") ) );
// sshd.setShellFactory(new ProcessShellFactory(new String[] { "bash ssh -R" }));
// sshd.setShellFactory(new ProcessShellFactory( new String[] {"/bin/sh","ssh -R"} ) );
sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE);
sshd.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
// 修改默认的配置 如 超时时间
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.DISCONNECT_TIMEOUT, "100");
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.AUTH_TIMEOUT, "99999999");
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.IDLE_TIMEOUT, "99999999");
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.NIO_WORKERS, "99999999");
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.NIO2_READ_TIMEOUT, "99999999");
// PropertyResolverUtils.updateProperty(sshd, FactoryManager.NIO2_MIN_WRITE_TIMEOUT, "99999999");
// 加上这个配置,终端访问的时候就要输入密码
sshd.setPublickeyAuthenticator(new DefaultAuthorizedKeysAuthenticator(false));
sshd.start();
log.info("[YunSShServer.run][response]result:{},host:{},port:{}","SSHD服务启动成功",sshd.getHost(),sshd.getPort());
}
@Value("${operator_name}")
private String name;
@Value("${operator_pwd}")
private String password;
@Value("${sshd.ip}")
private String ip;
@Value("${sshd.port}")
private String port;
@Value("${sshd.key_path}")
private String key_path;
}