views:

789

answers:

4

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.

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.

+2  A: 

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?

Jon Skeet
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.
Amar Patel
It sounds to me like it would be best not running as a service then, but being started as a "regular" application - at which point the relevant user is the one that the application would be running as.
Jon Skeet
A: 

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

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();
}
A: 

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.

james
A: 

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

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

gforman