`

Getting the Login Name of the Currently Logged-In User

阅读更多
This example retrieves the login name of the user that is running the application.

try {
    String loginAppName = "GetLoginNameUnix";

    // If the application is run on NT rather than Unix, use this name
    loginAppName = "GetLoginNameNT";

    // Create login context
    LoginContext lc = new LoginContext(loginAppName,
        new com.sun.security.auth.callback.TextCallbackHandler());

    // Retrieve the information on the logged-in user
    lc.login();

    // Get the authenticated subject
    Subject subject = lc.getSubject();

    // Get the subject principals
    Principal principals[] = (Principal[])subject.getPrincipals().toArray(new Principal[0]);
    for (int i=0; i<principals.length; i++) {
        if (principals[i] instanceof com.sun.security.auth.NTUserPrincipal
              || principals[i] instanceof com.sun.security.auth.UnixPrincipal) {
            String loggedInUserName = principals[i].getName();
        }
    }
} catch (LoginException e) {
    // Login failed
}


The example requires a login configuration file that specifies the login modules to execute when using a particular login-app name. This configuration file specifies two login-app names:

GetLoginNameNT {
    com.sun.security.auth.module.NTLoginModule required;
};

GetLoginNameUnix {
    com.sun.security.auth.module.UnixLoginModule required;
};


The login configuration file is specified at the command line:
> java -Djava.security.auth.login.config=myconfig.config MyApp
分享到:
评论

相关推荐

    Web开发-Flask Login登录成功返回上一页-Python源码示例.zip

    return "Welcome, logged-in user!" ``` 在这个示例中,我们学习了如何使用Flask和Flask-Login来创建一个简单的登录系统,以及如何在用户登录成功后返回他们之前试图访问的页面。这个功能提高了用户体验,使得用户...

    Login_System_v.2.0 from US

    This a login System, it has 3 files login.php register.php lastmember.php once a user has been logged in it will return him/her to the home page. to see if they are logged in use the code at the ...

    google api php client

    1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject: ```php $...

    NewSID(光学习一下代码就可以了,没看清楚介绍别运行)

    You must have permissions to change the security settings of the target computer‘s Registry keys, which typically means that you must be logged in as a domain administrator to use this feature.Note ...

    BURNINTEST--硬件检测工具

    number of Network Addresses specified in Preferences-&gt;Network will be the number of NICs tested. This will error faulty NICs that are not detected by the BurnInTest auto NIC detection mechanism. ...

    ramin 2.2 for xp

    logged into a WinNT domain connects to Radmin server, the user's current username/password are employed by Radmin viewer to authorize a connection to the Radmin server. Password Protection If ...

    logged-out:建立实时会话无效的三种方法

    实时会话无效 随播克隆和npm安装git clone https://github.com/robzhu/logged-outcd logged-outnpm install投票演示npm run polling# open polling/index.html in two browser tabs使用Web套接字推送演示npm run ...

    ATM_IN_JAVA.rar_ATM system_The First_account.java atm_atm simula

    Simulation of ATM terminal users logged in, their account numbers and passwords through the ATM network to transmit to the server, ATM database server based on the information to confirm the account ...

    mfix-19.2.0.tar.gz

    《mFix 19.2.0:源代码解析与应用探索》 mFix,全称为Multiphase Flow with Interphase eXchanges,是一款强大的多相流交互模拟软件,广泛应用于能源、环境、化工等领域的流体动力学研究。本文将深入探讨mFix ...

    add-securetoken-to-logged-in-user:将SecureToken添加到当前登录的用户,从而允许该用户在APFS格式的启动卷上使用FileVault和磁盘加密

    将SecureToken添加到登录用户 将SecureToken添加到当前登录的用户。... 需要此工作流程来授权以编程方式创建的用户帐户(尚未明确授予SecureToken的用户帐户)以启用或使用FileVault并解锁APFS格式的启动卷上的磁盘...

    the-book-of-forgotten-styles:WordPress生成的类的一小部分,我们倾向于可耻地忽略

    in-as .must-log-in login .must-log-in :用户必须登录才能发表评论.reply :包装器&lt;div&gt; .comment-reply-link :回复链接.comment-reply-login :用户必须登录才能发表评论.comment-notes #email-notes .form-...

    Diskeeper 2008 v12.0.759.0

    enhancement that is a hallmark of the Diskeeper name. Whether you抳e used Diskeeper before or not, you may already be familiar with some of its features, but Diskeeper 2008 adds refinements to ...

    addin

    It will take the current name that is in the property cell and replace it with the same value stripped of spaces and other undesirable text and prefixed with the right three letter convention.&lt;P&gt; ...

    一个很有用的工具集 - PsTools

    * PsGetSid - display the SID of a computer or a user * PsInfo - list information about a system * PsKill - kill processes by name or process ID * PsList - list detailed information about processes...

    drupal 6.12

    name of each file for CHANGELOG.txt in the example. For more information on setting file permissions, see "Modifying Linux, Unix, and Mac file permissions" (http://drupal.org/node/202483) or ...

    S7A驱动720版本

    switch:D11" delivers the state of the driver device with the name "D11". This function is also available via the OPC server interface. The same address format has to be used for the tag-I/O ...

    联想旭日420AXP系统芯片组驱动

    Only the files that reference the currently detected devices are copied to the system. If the -A option is exercised, the files are not copied to the &lt;Windows&gt;\INF directory. Refer to Section...

    Django 1.0 Website Development.pdf

    Restricting access to logged-in users 80 Methods for browsing bookmarks 81 Improving the user page 83 Creating a tag page 85 Building a tag cloud 87 A word on security 90 SQL injection 91 Cross...

    Backend-graphql:管理面板-用户注册,AuthenticateUser,LoggedInUser,重置密码

    dotbackend-graphql dotbackend-graphql-管理面板-UserAuthentication注册,LoggedInUser,忘记密码 跑 安装依赖项-npm install 全局安装GraphCool以使用CLI功能npm ...query{ loggedInUser{ id } } 参考文献:

    基于python的博客设计与开发.doc

    There's also a password modification feature, which can only be executed when the user is logged in. The administrator has the ability to modify all user information, including usernames, passwords, ...

Global site tag (gtag.js) - Google Analytics