Subject: Inject your own login module into JAAS framework -- Put all pieces together
Author: authen
In response to: Stand alone test before let your custom module out
Posted on: 10/20/2012 01:00:32 AM
Now that you have your own login module ready, you can integrate it into your business layer with the help of configuration file named C:\temp\jaas_login.conf:
myLoginEntity {
com.myCompany.MyLoginModule required
anonymousAllowed=true
debug=true;
};
Integration:
public static void main(String[] args) throws Exception
{
System.setProperty("java.security.auth.login.config",
"C:\\temp\\jaas_login.conf");
try{
// 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");
}
}
}
};
LoginContext lc = new LoginContext("myLoginEntity", ch);
lc.login();
Subject subject = lc.getSubject();
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:57:07 AM
authen wrote:
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
References: