|
Stand alone test before let your custom module out |
|
Subject: Stand alone test before let your custom module out
Author: authen
In response to: How to create my own JAAS login module
Posted on: 10/20/2012 12:57:07 AM
public static void main(String[] args) throws Exception
{
try{
Subject subject = new Subject();
MyLoginModule myLoginModule = new MyLoginModule();
Map <String, String> map = new HashMap <String, String>();
map.put("anonymousAllowed", "false");
// CallbackHandler
final String name = "Joe";
final String password = "Joe";
CallbackHandler ch = new CallbackHandler(){
public void handle (Callback[] callbacks) throws
UnsupportedCallbackException, IOException
{
for(int j=0; j<callbacks.length; j++) {
Callback callBack = callbacks[j];
// Handles username callback.
if (callBack instanceof NameCallback) {
NameCallback nc = (NameCallback)callBack;
nc.setName(name);
// Handles password callback.
} else if (callBack instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback)callBack;
pc.setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callBack,
"Call back not supported");
}
}
}
};
myLoginModule.initialize(subject, ch, null, map);
myLoginModule.login();
myLoginModule.commit();
System.out.println(subject);
}catch(Exception e){
e.printStackTrace();
}
}
Here is the output:
My Login Module - Constructor called
My Login Module - initialize() called
My Login Module - login() called
Succeeded!
My Login Module - commit() called
Subject:
Principal: Joe
>
> On 10/20/2012 12:53:55 AM authen wrote:
package com.myCompany;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.security.Principal;
import com.sun.security.auth.UserPrincipal;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
public class MyLoginModule implements LoginModule {
private Subject subject;
private CallbackHandler callbackHandler;
private Map sharedState;
private Map options;
private boolean succeeded = false;
private String username;
public MyLoginModule() {
System.out.println("My Login Module - Constructor called");
}
@Override
// Required
public boolean abort() throws LoginException {
System.out.println("My Login Module - abort() called");
return false;
}
@Override
// Required
public boolean commit() throws LoginException {
System.out.println("My Login Module - commit() called");
if(succeeded){
Set princSet = subject.getPrincipals();
Principal principal = new UserPrincipal(username);
princSet.add(principal);
}
return succeeded;
}
@Override
// Required
public void initialize(Subject subject,
CallbackHandler callbackHandler,
Map<String, ?> sharedState,
Map<String, ?> options)
{
System.out.println("My Login Module - initialize() called");
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
this.options = options;
succeeded = false;
}
@Override
// Required
public boolean login() throws LoginException
{
System.out.println("My Login Module - login() called");
if (callbackHandler == null) {
throw new LoginException("Oops, callbackHandler is null");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("name:");
callbacks[1] = new PasswordCallback("password:", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException e) {
throw new LoginException(
"Oops, IOException calling handle on callbackHandler");
} catch (UnsupportedCallbackException e) {
throw new LoginException("Oops, UnsupportedCallbackException " +
"calling handle on callbackHandler");
}
NameCallback nameCallback = (NameCallback) callbacks[0];
PasswordCallback passwordCallback = (PasswordCallback) callbacks[1];
// name & password from callback handler
String name = nameCallback.getName();
String password = new String(passwordCallback.getPassword());
// options from configuration settings
String option = (String)options.get("anonymousAllowed");
if(password==null || "".equals(password)){
if( option!=null && option.equalsIgnoreCase("true") ){
System.out.println("Succeeded!");
username = name;
succeeded = true;
return succeeded;
}
}else if(authenticate(name, password)){
System.out.println("Succeeded!");
username = name;
succeeded = true;
return succeeded;
}
System.out.println("Failed!");
succeeded = false;
throw new FailedLoginException("Name and password not matched.");
}
@Override
// Required
public boolean logout() throws LoginException {
System.out.println("My Login Module - logout() called");
return false;
}
private boolean authenticate(String name, String password)
{
/* just an exmaple, you can put whatever your authenticating logic here */
return name.equals(password);
}
}
References:
|
|
|
|