0

I am having one servlet where I am trying to get windows user name. I am getting the correct host name but unable to get the correct user name for different machines. I have tried to deploy that servlet in my machine and sharing the URL to different folks, when they are hitting that URL, I am getting my machine user name (windows), not others. I have tried with below codes:

String userName = new com.sun.security.auth.module.NTSystem().getName();

and

String userName = System.getProperty(“username”);

and

System.getProperty("user.name");

In my servlet I am trying to check it, I am sharing my URL to different person but always I am getting same user name like it’s my user name but the host name I am getting correct one.

Can you please suggest me.

dur
  • 15,689
  • 25
  • 79
  • 125
user3452231
  • 1
  • 1
  • 1
  • You can use the code formatting buttons in the editor to format your code, or indent four spaces. – AlBlue Jun 16 '16 at 08:56

1 Answers1

-1

Update:

As Mr. BalusC pointed out, this will only work for local machines while development phase. A client-side language like JavaScript can't perform such security-risk task. When you will deploy this code/project, the

System.getenv().get("USERNAME");

will not target client's computer. It will target server system. Proof Here.

Before Update:

I tested this, and it worked.

System.getenv().get("USERNAME")

// Servlet
yourPrintWriter.println(System.getenv().get("USERNAME") + " is the username.");

source is the last answer by Dragos Roban

Community
  • 1
  • 1
rupinderjeet
  • 2,984
  • 30
  • 54
  • Hi , i have tried with below code too. It's not working also. String username = Advapi32Util.getUserName(); My scenario like , Consider I deploy my web-app on a server box-1 and is logged in with Admin1. Now when I access this application using a system logged in with user name as USER1 I need to get value as user1. and the same application when another user accesses it by logging in with USER2 I need USER2 to be captured – user3452231 Jun 16 '16 at 09:50
  • All due respect, JSP/Servlet code runs in webserver, not in webbrowser. This would obviously only "work" when both webserver and webbrowser coincidentally runs at physically same machine, which usually only happens during local development. A web developer should know that better. – BalusC Jun 16 '16 at 09:51
  • Basically i want all the login information in the window in same lan . That should be tracked by my programme written in the Servlet . Tried all the logics shared in the above . But always it's tracking my window user name . But ip is comming different suppose i will ask others to hit the same url shared by me . Not sure what is the problem – user3452231 Jun 16 '16 at 10:01
  • but, if we use client-side javascript, it is not possible at all. If that's possible, it will be a huge security breach. Then, i don't think javascript can do this at all. – rupinderjeet Jun 16 '16 at 10:17
  • Correct Balu. But Not sure how to implement . Because i have tried few approaches but unable to find solution for it. – user3452231 Jun 16 '16 at 11:19