?? authenticationdemo2.java
字號:
// ==================== Program Discription ==========================
// 程序名稱:示例20-2 : AuthenticationDemo2.java
// 程序目的:使用JAAS進行認證
// ==============================================================
import java.security.*;
import javax.security.auth.*;
import javax.security.auth.spi.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import java.io.*;
import java.util.*;
public class AuthenticationDemo2 implements LoginModule
{
private Subject subject;
private Principal principal;
private CallbackHandler callbackHandler;
private String username;
private char[] password;
private boolean loginSuccess;
// set up the login module
public void initialize(Subject sub, CallbackHandler cbh,Map sharedState,Map options)
{
subject = sub;
callbackHandler = cbh;
loginSuccess = false;
username = null;
clearPassword();
}
// get username and password from the user and compare them to certain values
public boolean login() throws LoginException
{
if (callbackHandler == null) {
throw new LoginException("No CallbackHandler defined");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username");
callbacks[1] = new PasswordCallback("Password", false);
// Call the callback handler to get the username and password
try {
callbackHandler.handle(callbacks);
username = ((NameCallback)callbacks[0]).getName();
char[] temp = ((PasswordCallback)callbacks[1]).getPassword();
password = new char[temp.length];
System.arraycopy(temp, 0, password, 0, temp.length);
((PasswordCallback)callbacks[1]).clearPassword();
} catch (IOException ioe) {
throw new LoginException(ioe.toString());
} catch (UnsupportedCallbackException uce) {
throw new LoginException(uce.toString());
}
// If username matches, go on to check password
if ( "sa".equals(username)) {
System.out.println ( "Username Matches." );
if ( password.length == 5 && password[0] == 's' && password[1] == 'a' &&
password[2] == 's' && password[3] == 'a')
{
//If both username and password match, then login is successful
System.out.println( "Password Matches." );
loginSuccess = true;
System.out.println( "Login Success." );
clearPassword();
return true;
}
else {
System.out.println ( "Password doesn't match." );
}
}
else {
System.out.println( "Username doesn't match." );
}
// either username or password doesn't match,the login fails
loginSuccess = false;
username = null;
clearPassword();
System.out.println( "Login Fail." );
throw new FailedLoginException();
}
// Call commit to add the principal if both the succeeds
public boolean commit() throws LoginException
{
if (loginSuccess == false) {
return false;
}
// If login succeeded,add the new principal to the subject
principal = new PrincipalImpl(username);
if (!(subject.getPrincipals().contains(principal))) {
subject.getPrincipals().add(principal);
}
username = null;
return true;
}
// If authentication fails, cleanup the internal state
public boolean abort() throws LoginException
{
if (loginSuccess == false) {
principal = null;
username = null;
return false;
}
logout();
return true;
}
// method logout cleans up the state
public boolean logout() throws LoginException
{
subject.getPrincipals().remove(principal);
loginSuccess = false;
username = null;
principal = null;
return true;
}
// Private method clears the password
private void clearPassword()
{
if (password == null) {
return;
}
for (int i=0;i<password.length;i++) {
password[i] = ' ';
}
password = null;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -