简体   繁体   English

检查用户是否是java应用程序中的root用户

[英]Check if a user is root in a java application

How can i verify if a user is root in a java application? 如何验证用户是否是java应用程序的root用户?

Thanks 谢谢

Process p = Runtime.getRuntime().exec("id -u")

Keep in mind that the "root" user on a system may not be called root (although it's rare to change it), and it's also possible to alias it to another username. 请记住,系统上的“root”用户可能不会被称为root (尽管很少更改它),并且也可以将其别名为另一个用户名。 If the current user is root-like, the output will be 0 . 如果当前用户是root用户,则输出将为0

Easy. 简单。 Just use 只是用

System.getProperty("user.name")

run a native command? 运行本机命令? like whoami 像whoami

You can call 你可以打电话

  Process p = Runtime.getRuntime.exec("whoami")

method. 方法。 Then you can process p 's stdout to read output of command. 然后你可以处理p的stdout来读取命令的输出。

检查一下: 在java中获取登录用户名

String userName = System.getProperty("user.name");
Process p = Runtime.getRuntime().exec("groups " + userName);
String output = read(p.getInputStream());
String error = read(p.getErrorStream());

And here is a read function: 这是一个读取功能:

public static String read(InputStream input) throws IOException {
    try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
        return buffer.lines().collect(Collectors.joining("\n"));
    }
}

Just "another" modified solution. 只是“另一个”修改后的解决方

The best way is to run 最好的方法是跑步

 Process p = Runtime.getRuntime.exec("groups `whoaim`"); 

and try to parse string to get group call root. 并尝试解析字符串以获取组调用root。 Your JVM process could be run by user not call root but ie moderator but this user could be in root group and you have root privileges. 您的JVM进程可以由用户运行,而不是调用root,但是调度程序,但该用户可以在root组中,并且您具有root权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查在Java应用程序中使用Active Directory登录的用户 - Check for user logged in with Active Directory in Java application 以非root用户身份使用Solaris SMF运行Java应用程序 - Running Java Application with Solaris SMF as Non-Root User Java 应用程序无法以 debian 中的非 root 用户身份连接到 mysql - Java application fails to connect to mysql as non-root user in debian 查根节点剪JAVA - Check root node cut JAVA 如何检查没有空白字段并提示用户是否有(Java Android 应用程序) - How to check for no blanks fields and prompt the user if there is any (Java Android Application) 在Java中检查Facebook用户是否从服务器端连接到应用程序 - Check if a facebook user is connected to the application from server side in java Java应用程序-加载网页并检查DOM中的用户选择 - Java Application - Load web page and check user selections in DOM 从JAVA应用程序检查LDAP上的用户密码 - Check user's password on LDAP from JAVA application 非root用户无法读取在Linux上具有文件写入/输出的JAVA应用程序 - JAVA application with file write/output on linux cant be read by non root user 用户'root'@'localhost'的访问被拒绝-无法使用Java应用程序连接到数据库 - Access denied for user 'root'@'localhost' - unable to connect to database using java application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM