⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authenticationdemo2.java

📁 精通Java核心技术的随书源代码
💻 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;
     }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -