Categories
Coding

NT Authentication using JAAS

SSO Made Easy

On a similar vein to the entries I posted a couple of months back, here is an example of how to use JAAS to get the current user credentials. It uses the NTLoginModule default implementation, with the following parameters in a config file called ntlogin.config:

NTLMTest {
com.sun.security.auth.module.NTLoginModule Required debug=true;
};

And here’s the code. Run it with the parameter -Djava.security.auth.login.config=ntlogin.config to test.


package uk.co.researchkitchen.auth.jaas;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import com.sun.security.auth.callback.TextCallbackHandler;

public class JaasNTCredentialsTest {

LoginContext loginContext = null;

public static void main(String[] args) {
JaasNTCredentialsTest jnt = new JaasNTCredentialsTest();
jnt.doAuth();
}

public void doAuth() {
try {
loginContext = new LoginContext("NTLMTest", new TextCallbackHandler());
loginContext.login();
} catch (LoginException e) {
e.printStackTrace();
}

Subject subject = loginContext.getSubject();

}

}

Incidentally, if you just want to get the user name, you can use something like this:

Set principals = subject.getPrincipals();

for (Principal p : principals) {
if (p instanceof NTUserPrincipal) {
System.out.println("User name: " + p.getName());
}
}

Leave a Reply