MINA SSHD

网上找了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;

}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值