Get login for java login

How can I get username / login in Java?

This is the code I tried ...

try{ LoginContext lc = new LoginContext(appName,new TextCallbackHandler()); lc.login(); Subject subject = lc.getSubject(); Principal principals[] = (Principal[])subject.getPrincipals().toArray(new Principal[0]); for (int i=0; i<principals.length; i++) { if (principals[i] instanceof NTUserPrincipal || principals[i] instanceof UnixPrincipal) { String loggedInUserName = principals[i].getName(); } } } catch(SecurityException se){ System.out.println("SecurityException: " + se.getMessage()); } 

I get a SecurityException when I try to run this code. Can someone please tell me if I'm heading in the right direction and help me understand the problem.

+83
java username
Apr 28 '09 at 12:08
source share
7 answers
 System.getProperty("user.name") 
+204
Apr 28 '09 at 12:11
source share

on Unix:

 new com.sun.security.auth.module.UnixSystem().getUsername() 

on Windows:

 new com.sun.security.auth.module.NTSystem().getName() 

on Solaris:

 new com.sun.security.auth.module.SolarisSystem().getUsername() 
+29
Apr 28 '09 at 23:35
source share

System.getProperty ("user.name") is not good protection, since this environment variable can be fake: C: \ set USERNAME = "Joe Doe" Java ... // will provide you with System.getProperty ("user.name ") You must do:

 com.sun.security.auth.module.NTSystem NTSystem = new com.sun.security.auth.module.NTSystem(); System.out.println(NTSystem.getName()); 

JDK 1.5 or more.

I use it in an applet and it must be signed. The source of information

+15
Feb 18 2018-10-18
source share

inspired by @newacct's answer, code that can be compiled on any platform:

 String osName = System.getProperty( "os.name" ).toLowerCase(); String className = null; String methodName = "getUsername"; if( osName.contains( "windows" ) ){ className = "com.sun.security.auth.module.NTSystem"; methodName = "getName"; } else if( osName.contains( "linux" ) ){ className = "com.sun.security.auth.module.UnixSystem"; } else if( osName.contains( "solaris" ) || osName.contains( "sunos" ) ){ className = "com.sun.security.auth.module.SolarisSystem"; } if( className != null ){ Class<?> c = Class.forName( className ); Method method = c.getDeclaredMethod( methodName ); Object o = c.newInstance(); System.out.println( method.invoke( o ) ); } 
+15
Jun 05 '14 at 15:37
source share

Using JNA is simple:

 String username = Advapi32Util.getUserName(); System.out.println(username); Advapi32Util.Account account = Advapi32Util.getAccountByName(username); System.out.println(account.accountType); System.out.println(account.domain); System.out.println(account.fqn); System.out.println(account.name); System.out.println(account.sidString); 

https://github.com/java-native-access/jna

+6
Nov 18 '15 at 15:53
source share

"Installed username =" Username "is a temporary redefinition that only exists when cmd windows still work, after it is destroyed, the variable loses value. Therefore, I think that

System.getProperty ("user.name");

is a short and accurate code to use.

+2
Jun 01 '15 at 13:30
source share

System.getenv().get("USERNAME"); - works on windows!

In the properties of the environment you have the information necessary for the computer and the host! I speak again! Powered by WINDOWS!

+1
Aug 11 '15 at 14:11
source share



All Articles