Getting the Login Name of the Currently Logged-In User

2010-03-29  来源:本站原创  分类:Java  人气:623 

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 game design and preparation of plug: QQ skyscraper next [second] (login game) 2010-07-05

    On a paper that the production plug of the first step is to connect with the game, which landed the game. QQ games for it, there are two methods of landing the game. One is the landing of space and then landing the game, QQ farms, QQ games grab a par

  • Web game design and preparation of plug: QQ skyscraper [2] (login game) 2010-07-05

    On a paper that the production plug of the first step is to connect with the game, which landed the game. QQ games for it, there are two methods of landing the game. One is the landing of space and then landing the game, QQ farms, QQ games grab a par

  • login shell and non-login shell of the difference: 2011-08-19

    login shell and non-login shell of the difference (2011-01-24 16:52) Category: Linux System A, login shell and non-login shell of the difference: login shell and non-login shell reads the biggest difference is the configuration file environment varia

  • sys difference with the system 2010-04-05

    1) The most important difference, the importance of data stored in different sys] [all oracle data dictionary base tables and views are stored in the sys user, these base tables and views for the oracle operation is essential, by the maintenance of t

  • the difference with the system sys 2010-04-05

    1) The most important difference, the importance of data stored in different sys] [all oracle data dictionary base tables and views are stored in the sys user, these base tables and views for the oracle operation is essential, by the maintenance of t

  • filter servlet difference - ZT 2010-04-21

    Filter description Servlet Filter can be considered a "variant", which is used to preprocess the user request, HttpServletResponse can also post processing, is a typical processing chain. It Servlet difference: it can not respond directly to use

  • (Assignment) Linux word wonderful Q & A 2010-05-12

    Q & A on Linux wonderful word: Xu Chen / Wei Qiong from: linux treasure (http://www.linuxmine.com) Contact: [email protected] Editor's note: Given the current development status of Linux in China, 90% of the questions can be answered in one sentenc

  • Web game design and preparation of plug: QQ skyscraper [1] (basic skills) 2010-05-25

    Web game preparation is very simple plug-in does not need to study the source code, do not know how to compile knowledge, only analysis sent to the server and the server sends to the local packet can be written. But if you want to analyze the data as

  • JIRA + PHP SOAP Client demo I 2010-09-02

    <? try { // Jira WSDL $wsdl = "http://localhost:8080/rpc/soap/jirasoapservice-v2?wsdl"; // Login info //$login = "admin"; //$password = "111111"; $login = "test1"; $password = "test1"; // Create the soa

  • JIRA + PHP SOAP Client demo II 2010-09-02

    <? try { // Jira WSDL $wsdl = "http://localhost:8080/rpc/soap/jirasoapservice-v2?wsdl"; // Login info $login = "admin"; $password = "111111"; // Create the soap Client $client = new soapclient($wsdl); // Login to Jira $log

  • cakephp + jira soap client to use 2010-09-13

    1. To create components file jira_client.php <?php class JiraClientComponent extends Object { // Jira WSDL var $wsdl = JIRA_WSDL; // Login info var $user = JIRA_USER; var $password = JIRA_PWD; var $client = null; var $login = null; //called before Co

  • Accessibility for all network analysis of a small war 2010-11-05

    Accessibility for all network analysis of a small war Words, the egg will be free of pain. Have not recently without incident, but everyone is playing a small network of war, feel pretty good, the boot is on the line every day, and then received food

  • linux RedHat AS4 system installation oracle 10g backup and data restore 2010-11-10

    A, RedHat AS4 system installation: Basic firewall configuration: ⊙ No Firewall ⊙ whether to enable SELinux: Disabled Installation options: ⊙ customized packages to be installed (C) Desktop Options: (√) X Window System (selected default) (√) GNOME des

  • Session state of the cluster concept of sharing java 2010-11-26

    Environment: Front controller: Nginx servlet container: resin Recently the company wants to engage in the cluster, but for now most of the market do Nginx cluster ip is always based on user access to a machine down if the machine off, then all state

  • Written the 10 MRD techniques - Part I (original translation) 2006-12-13 23:03 [translation by Tiger 2008-05-26

    MRD-"Marketing Requirements Document", is the product manager or product marketing manager of a written demand for the product descriptions, documentation. These documents are used to plan a new product or modify an existing product, product dev

  • Expect Chinese version of the tutorial 2011-01-06

    Transfer: http://fanqiang.chinaunix.net/a4/b8/20010912/1000001278.html Expect Chinese version of this tutorial from: author: gourd translation (2001-09-12 10:00:00) [Copyright] Copyright (c) 1999 This tutorial by * gourd * translation, and made the a

  • Analyze the linux boot process (transfer) 2011-01-09

    Original link: http://bbs.phpchina.com/thread-82640-1-15.html In this paper, RedHat9.0 and i386 platforms, for example, analysis of the power from the user to open the command prompt appears on the screen until the entire Linux boot process. And intr

  • session synchronization 2011-05-14

    Website Zhong doing a web cluster, you will definitely first consider the session synchronization problem, because through load balancing, the same IP to access the same page would be assigned to a different server, if the session is not synchronized

  • [Transfer] jira User Guide 2010-06-21

    JIRA playing this thing up, or not easy, it collects more information - jira User Guide The first chapter, Introduction JIRA is Australia Atlassian developed a good issue tracking management software tool that can track and manage all types of proble

  • Q reproduced wonderful word linux 2011-06-06

    Reproduced: http://bbs.chinaunix.net/thread-2283984-1-1.html Was wonderful, Mo Kanwan, backup collection - Network independent papers -------------------------- 1001 to modify the hostname (Chen Xu) vi / etc / sysconfig / network, modify the HOSTNAME