I want to get logged username on windows , I mean the user logged in to windows using java. I used
System.getProperty("user.name");
but I got the server-name, not the user.
Asked
Active
Viewed 2.1k times
-4

Danny
- 23
- 1
- 1
- 6
-
In what context? The computer? A webapp? According to the documentation, `user.name` _is_ the name of the account that is logged into the computer. – NilsH Apr 23 '13 at 11:04
-
can you please elborate more on your envionment? are you talking about Web application or Desktop application? if its web application `System.getProperty("user.name")` will give you user logged on server machine. – rahul maindargi Apr 23 '13 at 11:05
-
possible duplicate of http://stackoverflow.com/questions/797549/get-login-username-in-java – rahul maindargi Apr 23 '13 at 11:05
-
1If you use System.getProperty("user.name") in a JSP, which is deployed in a Servlet container running under Windows as a service with the System account, you will see the host name, since that is the "real" name of the system account. – jarnbjo Apr 23 '13 at 11:07
-
Thanks very much i am using web applications. I am using JSP and as you said i got the host name. – Danny Apr 23 '13 at 11:11
-
3Why does every starter think that Java/JSP runs in webbrowser instead of in webserver? Java/JSP runs in webserver and produces a bunch of HTML code which get sent to the webbrowser. In order to run Java in webbrowser, you need a client application (e.g. Swing) in flavor of an applet or webstart which you embed via ` – BalusC Apr 23 '13 at 11:12
-
And what user is it that you're trying to get? The user logged into your web application? Or the user running the server? – NilsH Apr 23 '13 at 11:12
-
@Danny: And which user name are you actually trying to find? – jarnbjo Apr 23 '13 at 11:13
-
I am getting the user logged in to the windows. – Danny Apr 23 '13 at 11:14
-
And you want the username of the user logged into your web application? – NilsH Apr 23 '13 at 11:15
-
I want to user logged in to the windows. (ie when you press start button you find your username.) that what i want to get. – Danny Apr 23 '13 at 11:19
-
... of the user accessing the web application, is that what you want? And how does the user log into the web application? – NilsH Apr 23 '13 at 11:22
-
when the user logs in to the windows. he will use many resources provided by the server. I want to be able to get that user name of windows in JAVA. i am developing web application. this application will be working just on the server ( Not online) i want to be able to get the user logged in in windows. – Danny Apr 23 '13 at 11:28
1 Answers
4
Getting the name of the user logged into your web application (which I think is what you want to do, based on your comments) entirely depends on how you have implemented authentication in your system. However, if your login mechanism adds this information, the username might be available in request.getRemoteUser()
or request.getUserPrincipal()
. But as mentioned, it all depends. Your authentication mechanism might expose this otherwise, but then we would need more information to assist.
System.getProperty("user.name")
will only give you the username of the user running the java process.

NilsH
- 13,705
- 4
- 41
- 59
-
3If you use a waffle filter, it will add the correct user credentials to the request. See https://github.com/dblock/waffle for more information. – tstorms Apr 23 '13 at 11:21
-
-
@tstorms: I don't know why you deleted your answer. Until now, it was the only reasonable attempt here to actually solve the problem. The Waffle documentation is however pretty vague. There are many NTLM authentication modules for Java supporting NTLMv1, but AFAIK none supporting NTLMv2. Do you know if Waffle also supports NTLMv2? NTLMv1 is for good reasons disabled in many Windows domains. – jarnbjo Apr 23 '13 at 11:29
-
Yes i am using Kerberos for Single sign in. But how can i get it from Kerberos. – Danny Apr 23 '13 at 11:30
-
I know, my answer has been deleted. Some admins are just too diligent... To answer your question, yes, waffle supports NTLM 2 out of the box. – tstorms Apr 23 '13 at 11:31
-
Danny - If you're using the filter that @tstorms suggested, it should be added to the request, if I understand correctly. tstorms can probably elaborate. – NilsH Apr 23 '13 at 11:31
-
@NilsH, request.getUserPrincipal().getName() will return the username. The filter takes care of this. – tstorms Apr 23 '13 at 11:33
-
I am not using something like that. Kerberos is taking the responsibility of taken the token from windows and make authentication by its self, but kerberos hides most of its file. I cant get the token or the Java file that process the login. – Danny Apr 23 '13 at 11:35
-
Exactly. And this filter adds this information to the request so it is available to the webapp. – NilsH Apr 23 '13 at 11:37
-
how to use this filter, because i am not quite familiar with the kerberos structure. and i tried request.getUserPrincipal().getName() it gives null. – Danny Apr 23 '13 at 11:44
-
See https://github.com/dblock/waffle/blob/master/Docs/ServletSingleSignOnSecurityFilter.md for the filter configuration. You can choose between kerberos (negotiate) and/or NTLM – tstorms Apr 23 '13 at 11:50
-
-
You know the problem with Kerberos that it supports just one domain. If i logged in from subdomain it doesnt work. and that is my main problem. – Danny Apr 23 '13 at 11:56
-
To be honest, I've lost track of what you're trying to do, and how the complete environment/usage is. I suggest you add some (a lot?) more information to your question, and maybe someone will be able to assist. – NilsH Apr 23 '13 at 12:01