Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ProcessBuilder改变用户环境+;JAVA_Java_Environment Variables - Fatal编程技术网

使用ProcessBuilder改变用户环境+;JAVA

使用ProcessBuilder改变用户环境+;JAVA,java,environment-variables,Java,Environment Variables,我正在尝试将子进程的用户更改为具有次要权限的用户 但是,当我执行ProcessBuilder的start方法时,子流程exec将与父进程的同一用户一起执行 LinkedList<String> commands = new LinkedList<String>(); commands.add("vlc"); ProcessBuilder builder = new ProcessBuilder(commands);

我正在尝试将子进程的用户更改为具有次要权限的用户 但是,当我执行ProcessBuilder的start方法时,子流程exec将与父进程的同一用户一起执行

        LinkedList<String> commands = new LinkedList<String>();
        commands.add("vlc");
        ProcessBuilder builder = new ProcessBuilder(commands);
        Map<String,String> enviroment = builder.environment();
        enviroment.clear();            
        enviroment.put("USER", "otheruser");
        enviroment.put("LOGNAME", "otheruser");
        enviroment.put("PWD", "/home/otheruser");
        enviroment.put("HOME", "/home/otheruser");
        enviroment.put("USERNAME", "otheruser");
        enviroment.put("SHELL", "/bin/false");
        builder.directory(new File("/home/otheruser"));            

        Process process = builder.start();
        process.waitFor();
LinkedList命令=新建LinkedList();
命令。添加(“vlc”);
ProcessBuilder=新的ProcessBuilder(命令);
Map environment=builder.environment();
environment.clear();
Environment.put(“用户”、“其他用户”);
environment.put(“LOGNAME”、“otheruser”);
environment.put(“PWD”,“home/otheruser”);
environment.put(“HOME”,“/HOME/otheruser”);
environment.put(“用户名”、“其他用户”);
environment.put(“SHELL”,“/bin/false”);
builder.directory(新文件(“/home/otheruser”);
Process=builder.start();
process.waitFor();

我正在使用Linux(Ubuntu)

您不能仅仅通过传递不同的
user
环境变量来更改有效用户。这是Linux(通常是Unix)的一项安全功能,否则恶意用户可以将用户变量设置为
ROOT
。子进程总是以与父进程相同的用户身份执行,除非可执行文件被标记为setuid,或者进程执行setuid()来更改有效用户(并且允许setuid())。

Jim完全正确。 但是,如果您仍然希望以不同的用户身份运行程序,则必须使用依赖于平台的工具

窗口: 使用runas命令,例如:runas/user:domain\jamesbond regedt32.exe 不幸的是,runas需要用户手动输入密码。 以下文章介绍了如何解决此问题:

或者,您可以在VBS中编写自己的实用程序,并从java运行它。有关详细信息,请参阅此帖子:

Unix: 参见su和sudo的参考资料。 su很好,但它也需要密码(除非当前用户是root用户)。 要解决此问题,可以创建expect脚本(请参见)。 默认情况下,Expect安装在大多数unix发行版上

祝你好运