User guide

Chapter 7: Developing Applications
7-8
Example
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.security.Principal;
import java.security.PrivilegedAction;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import com.fujitsu.interstage.sso.auth.ISAuthenticationCredential;
import com.fujitsu.interstage.sso.auth.ISAuthorizationCredential;
import com.fujitsu.interstage.sso.auth.callback.ISCallbackHandler;
public class ISSsoJaas{
private Subject subject;
public ISSsoJaas(){
subject = new Subject();
}
public boolean login() throws Exception{
LoginContext loginContext = null;
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(System.in));
// attempt 3 times
for (int i=0 ; i<3; i++) {
// username is set from prompt
System.out.print("UserName=");
String username = reader.readLine();
// password is set from prompt
int PASSWORD_MAX_LENGTH = 128;
char[] tmp = new char[PASSWORD_MAX_LENGTH];
System.out.print("Password=");
int count = reader.read(tmp);
int lineSeparatorLength =
System.getProperty("line.separator").length();
char[] password = new char[count - lineSeparatorLength];
System.arraycopy(tmp, 0, password, 0, password.length);
// callback is created here by userid and password
// Converting CallbackHandler to instance
CallbackHandler myHandler = new ISCallbackHandler(username, password);
// create LoginContext object
// Converting LoginContext to instance
loginContext = new LoginContext(
"com.fujitsu.interstage.sso", subject, myHandler);
// Calling LoginContext login method
try{
loginContext.login();
return true;
}
catch(FailedLoginException ex){
System.out.println("Authenticate failed");