How can I get the username/login name in java ?
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 (LoginException le){
System.out.println("LoginException: " + le.getMessage());
}catch(SecurityException se){
System.out.println("SecurityException: " + se.getMessage());
}
but I get a SecurityException. I'm new to all this, so I don't know whether I'm using the right stuff, but badly, or I'm going in the right direction.
Any hint will help.