在java中获取登录用户名 [英] Get login username in java
本文介绍了在java中获取登录用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Java 中获取用户名/登录名?
How can I get the username/login name in Java?
这是我试过的代码...
This is the code I have 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());
}
当我尝试运行此代码时,我收到一个 SecurityException
.有人可以告诉我我是否朝着正确的方向前进,并帮助我理解问题.
I get a SecurityException
when I try to run this code. Could someone please tell me whether I'm heading in the right direction, and help me to understand the problem.
推荐答案
System.getProperty("user.name");
这篇关于在java中获取登录用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文