Apache Mina:收到命令后获取用户的用户名

时间:2017-07-29 04:00:18

标签: ssh sshd mina access-rights

使用Apache Mina,我试图允许某些用户运行某些自定义SSH命令。但我无法弄清楚如何在Public Sub UploadAfIle(sUrl As String, sFileName As String) Dim adoStream As Object Set adoStream = CreateObject("ADODB.Stream") adoStream.Mode = 3 ' read write adoStream.Type = 1 ' adTypeBinary adoStream.Open adoStream.LoadFromFile (sFileName) With CreateObject("Microsoft.XMLHTTP") adoStream.Position = 0 .Open "PUT", sUrl, False .setRequestHeader "Content-Length", "0" 'this is not a must .setRequestHeader "x-ms-blob-type", "BlockBlob" .Send adoStream.Read(adoStream.Size) End With Set adoStream = Nothing End Sub 中获取有关用户的用户名或任何类型的信息。

现在,我的服务器配置如下所示:

CommandFactory

这就是我的SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPasswordAuthenticator(new SshPasswordAuthenticator()); sshd.setPort(localServerPort); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setShellFactory(new SshShellFactory()); sshd.setSessionFactory(new SshSessionFactory()); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("my.pem").getAbsolutePath())); sshd.setFileSystemFactory(new VirtualFileSystemFactory(path.getAbsolutePath())); List<NamedFactory<Command>> namedFactoryList = new ArrayList<>(); namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList); 课程的样子:

SshShellFactory

import org.apache.sshd.common.Factory; import org.apache.sshd.server.session.SessionFactory; import org.apache.sshd.server.Command; import org.apache.sshd.server.CommandFactory; public class SshShellFactory implements CommandFactory, Factory<Command> { @Override public Command createCommand(String command) { return new SshSessionCommandWriter(); } @Override public Command create() { return createCommand("none"); } } 存储与我的软件相关的变量,ServerVariables对流进行I / O操作,SshSessionCommandWriter解释SshCommandManager中读取的命令并返回值客户端。

如果我能找到一种方法以某种方式获取运行命令的用户的用户名,那对我的用例来说就是完美的。我目前在SshSessionCommandWriterSshSessionFactory中有此信息,但在SshPasswordAuthenticator中没有。

1 个答案:

答案 0 :(得分:0)

因此,我创建的SshSessionCommandWriter对象必须实现Command接口。此命令接口在其合同中具有方法public void start(Environment env)。事实证明,用户名存储在环境变量中:

public class SshSessionCommandWriter implements Command, Runnable {

    @Override
    public void start(Environment env) throws IOException {
        String username = env.getEnv().get(Environment.ENV_USER);
    }
}