Java系统环境变量当前用户

时间:2021-10-01 10:35:40

What is the best way to get the current logged in user through Java application running on JBoss. The system environment variable System.getProperty("user.name") does not work as JBoss is running as a service.

通过在JBoss上运行的Java应用程序获取当前登录用户的最佳方法是什么。 JBoss作为服务运行时,系统环境变量System.getProperty(“user.name”)不起作用。

The application is running on a laptop running Windows XP. The application is web-based and accessed using Internet Explorer by particular logged in Windows user. Only 1 Windows user can be logged in at a time. The application needs to know which user has logged in to do a role based menu and security features. So both the client (Internet Explorer) and the server (JBoss) are running on the same laptop. Currently, we determine the logged in user using tasklist /v and then parsing the output to look for certain processes and the user running them. However, need to know if there is a cleaner way of getting the logged in Windows user.

该应用程序在运行Windows XP的笔记本电脑上运行。该应用程序是基于Web的,并由特定登录的Windows用户使用Internet Explorer访问。一次只能登录1个Windows用户。应用程序需要知道哪个用户已登录以执行基于角色的菜单和安全功能。因此,客户端(Internet Explorer)和服务器(JBoss)都在同一台笔记本电脑上运行。目前,我们使用tasklist / v确定登录用户,然后解析输出以查找某些进程以及运行它们的用户。但是,需要知道是否有更简洁的方法来获取登录的Windows用户。

4 个解决方案

#1


I don't think the question really makes much sense. There may be no users logged onto the host - or there may be multiple users.

我认为这个问题没有多大意义。可能没有用户登录到主机 - 或者可能有多个用户。

I'd be somewhat wary of a design that really wanted to know this anyway - web applications shouldn't really be interested in that sort of thing, IMO. What are you trying to do?

我有点担心一个真正想知道这个的设计 - 网络应用程序真的不应该对这类东西感兴趣,IMO。你想做什么?

#2


String uname = System.getenv("user.name")

String uname = System.getenv(“user.name”)

Reference: http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

#3


This assumes you have stored an environment variable named USERNAME when the user logged in:

这假设您在用户登录时存储了名为USERNAME的环境变量:

String username = System.getenv("USERNAME");

Or, if you want the complete set of environment variables:

或者,如果您需要完整的环境变量集:

Map<String, String> envMap = System.getenv();

Then iterate through the map to get an environment variable in which you're storing a username.

然后遍历地图以获取您要存储用户名的环境变量。

Map<String, String> envMap = System.getenv();
int mapsize = envMap.size();
Iterator i = envMap.entrySet().iterator();
for (int j = 0; j < mapsize; j++) {
    Map.Entry entry = (Map.Entry) i.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
}

#4


Use JAAS authentication. You should be able to use NTLM on windows, so the user won't have to do any additional work. Then, on the server you can use the security context to get the caller principal.

使用JAAS身份验证。您应该能够在Windows上使用NTLM,因此用户无需进行任何额外的工作。然后,在服务器上,您可以使用安全上下文来获取调用者主体。

#1


I don't think the question really makes much sense. There may be no users logged onto the host - or there may be multiple users.

我认为这个问题没有多大意义。可能没有用户登录到主机 - 或者可能有多个用户。

I'd be somewhat wary of a design that really wanted to know this anyway - web applications shouldn't really be interested in that sort of thing, IMO. What are you trying to do?

我有点担心一个真正想知道这个的设计 - 网络应用程序真的不应该对这类东西感兴趣,IMO。你想做什么?

#2


String uname = System.getenv("user.name")

String uname = System.getenv(“user.name”)

Reference: http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

#3


This assumes you have stored an environment variable named USERNAME when the user logged in:

这假设您在用户登录时存储了名为USERNAME的环境变量:

String username = System.getenv("USERNAME");

Or, if you want the complete set of environment variables:

或者,如果您需要完整的环境变量集:

Map<String, String> envMap = System.getenv();

Then iterate through the map to get an environment variable in which you're storing a username.

然后遍历地图以获取您要存储用户名的环境变量。

Map<String, String> envMap = System.getenv();
int mapsize = envMap.size();
Iterator i = envMap.entrySet().iterator();
for (int j = 0; j < mapsize; j++) {
    Map.Entry entry = (Map.Entry) i.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
}

#4


Use JAAS authentication. You should be able to use NTLM on windows, so the user won't have to do any additional work. Then, on the server you can use the security context to get the caller principal.

使用JAAS身份验证。您应该能够在Windows上使用NTLM,因此用户无需进行任何额外的工作。然后,在服务器上,您可以使用安全上下文来获取调用者主体。