views:

651

answers:

4

My Java RCP application prompts the user for username and password upon start-up. How could I use these credentials to do authentication against the native OS without using JNI to port some C libraries? Thanks!

PS. If possible, pure Java implementation without using third-party libraries will be very much preferable.

+8  A: 

AFAIK, this just isn't possible without involving native extensions to Java in some manner - there is no Java API for this.

You could take a look at the JNA project. It uses native code, but you don't have to write any - it's done for you.


EDIT: If all you want to do is validate the username/password, then I believe that the JNDI/LDAP direction may work for you - I've done this before on the AS/400 from Java, though I was not totally happy with the end result.

If you want to cause the O/S to recognize your JVM process as being credentialed as a particular user, you are going to need some form of access to non-portable native API's.

BTW, what O/S(s) are we talking about.


EDIT2: I am going to post fragments from how I used LDAP to verify a username/password, on the off chance that that is what you are after; these are lifted straight from my code, not intended to be directly compilable.

This is some of the first Java code I ever wrote, please be merciful:

import java.security.*;
import java.util.*;

import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;

...

private Hashtable                       masterEnv;          // master environment settings
private String                          authMethod;         // default authentication method

...

public void init() {
    // NOTE: Important to use a non-pooled context and a clone of the environment so that this authenticated
    //       connection is not returned to the pool and used for other operations
    masterEnv=new Hashtable();
    masterEnv.put(Context.INITIAL_CONTEXT_FACTORY,ldapFactory);
    masterEnv.put(Context.PROVIDER_URL,providerUrl);
    masterEnv.put(Context.SECURITY_PROTOCOL,secProtocol);
    masterEnv.put(Context.REFERRAL,"follow");
    masterEnv.put("com.sun.jndi.ldap.connect.pool","false");

    authMethod=System.getProperty("authenticationMethod","simple");
    }

...

private void verifyUserPassword(String ui, String pw, String am) throws NameNotFoundException, AuthenticationException, AuthenticationNotSupportedException, NamingException, NamingException {
    // ui=user ID
    // pw=password
    // am=authentication method

    DirContext      lc=null;                                // ldap context object
    Hashtable       le;                                     // ldap environment object

    if(am.length()==0) { am=authMethod; }

    le=(Hashtable)masterEnv.clone();
    le.put(Context.SECURITY_AUTHENTICATION,am);
    le.put(Context.SECURITY_PRINCIPAL     ,ui);
    le.put(Context.SECURITY_CREDENTIALS   ,pw);
    lc=new InitialDirContext(le);
    lc.close();
    }
Software Monkey
Hi, thanks for the reply, and sorry for the late reply. The application is meant to be portable, so the native OS targeted are both Linux and Windows, but concretely it would be Vista and RH Linux. Thanks, for the sample code, I will have a look at it.
+3  A: 

You could validate using JNDI and LDAP (assuming you are using LDAP/Active Directory for verification). Check out this thread for more details on how to do this.

Rob Di Marco
@OP: If you are successful using JNDI, please post the solution (at least an overview) here - I am quite interested in this direction.
Software Monkey
@OP: I have posted my LDAP code from back in '03.
Software Monkey
@Software Monkey, the code works great. Thanks!Do you know of anyway on how to do this if LDAP/ActiveDirectory is not involved at all (i.e. a vanilla OS that does not have connections to the network)? Is that even possible at all? Hmmmm, does not sound like it is doable...
A: 

Can some one post the LDAP Authentication code in detail ?

A: 

This example, http://spnego.sourceforge.net/pre_flight.html, talks about a HelloKDC.java program where the only thing it does is authenticate a username and password.

Pat Gonzalez