tags:

views:

58

answers:

3

i need to retrieve the User login ids who is logged on the server. I want to do it in java. If anyone who have the code please explain with the same.

Thanks you.

A: 

this is platform-dependent. what type of server are you talking about?

here is something that returns you the name of the user that is running your application. I've found this piece of code on http://www.exampledepot.com/egs/javax.security.auth.login/GetLogin.html

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
}

according to the site, you need a configuration for which modules are loaded.

regards

Atmocreations
+2  A: 

You can find it in the environment:

String login = System.getProperty("user.name");

But the security manager may prevent this (e.g. when called from an applet)!

Arne Burmeister
A: 

If you dump the System.properties you'll find a property named user.name. This should more or less be the OS user that started the JDK process.

Here's a code fragment to help

Properties props = System.getProperties(); Enumeration e = props.propertyNames(); while( e.hasMoreElements() ){ Object key = e.nextElement(); Object val = props.getProperty( key.toString() ); System.out.println( "key:"+ key +" val:"+ val ); }