1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public class ModuleConnexion implements LoginModule{
private Subject subject;
private CallbackHandler callbackHandler;
private NamePrincipal namePrincipal;
private boolean debug ;
private String userName ;
private boolean succeeded = false ;
private boolean commitSucceeded = false;
private Map sharedState;
@Override
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
this.subject = subject;
this.callbackHandler= callbackHandler;
debug ="true". equalsIgnoreCase((String) options.get("debug"));
}
@Override
public boolean login() throws LoginException {
if ( debug ) {
System.err.println("ModuleConnexion : login");
}
NameCallback nameCallback = new NameCallback ("user :");
PasswordCallback passwordCallback =new PasswordCallback("password : ", false);
Callback[] callbacks = new Callback[]{nameCallback ,passwordCallback} ;
try {
callbackHandler.handle(callbacks);
} catch ( Exception ex ) {
ex.printStackTrace();
throw new LoginException ("Error while getting user input !") ;
}
userName = nameCallback.getName();
succeeded = validateUser( userName , passwordCallback.getPassword ( ) ) ;
passwordCallback.clearPassword();
return succeeded ;
}
@Override
public boolean commit() throws LoginException {
if(debug){
System.err.println("GUILoginModule : commit");
}
if(!succeeded){
userName = null;
return false;
}
namePrincipal = new NamePrincipal ( userName ) ;
if( !subject.getPrincipals().contains(namePrincipal)){
subject.getPrincipals().add(namePrincipal);
}
userName = null;
commitSucceeded = true ;
return true ;
}
@Override
public boolean abort() throws LoginException {
if(debug){
System.err.println("GUILoginModule : abort");
}
if(!succeeded){
return false;
}else if ( succeeded && commitSucceeded ) {
logout();
} else {
succeeded = false;
}
return true ;
}
@Override
public boolean logout() throws LoginException {
if( debug ) {
System.err.println("GUILoginModule : logout") ;
}
subject.getPrincipals().remove(namePrincipal ) ;
namePrincipal = null ;
userName = null ;
succeeded = commitSucceeded = false ;
return true ;
}
private boolean validateUser ( String userName , char [ ] password ) {
return "jaas".equals( userName ) && "java".equals (new String ( password ) ) ;
}
} |
Partager