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

📄 loginhandler.java

📁 javaP2P技术内幕课程0789的源代码
💻 JAVA
字号:
package org.jworkplace.login;

import java.io.*;
import java.util.*;
import java.security.Principal;
import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import javax.security.auth.spi.*;
import com.sun.security.auth.*;


public class LoginHandler implements CallbackHandler {
    LoginContext lc;
    NameCallback nameCallback;
    PasswordCallback passwordCallback;
    int loginTries = 0;
    String user;
    char[] password;
    ;

    //create the LoginContext using the JWorkPlace configuration
    public LoginHandler() {
        try {
            lc = new LoginContext("JWorkPlace", new Subject(), this);
        } catch (LoginException le) {
            le.printStackTrace();
            System.exit(-1);
        }
    }

    //The Callback Interface
    //provides the types of callbacks used to interface with the LoginModule
    public void handle(Callback[] callbacks) throws IOException, 
                                                    UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof TextOutputCallback) {
                //display the message according to the specified type
                TextOutputCallback toc = (TextOutputCallback) callbacks[i];

                switch (toc.getMessageType()) {
                case TextOutputCallback.INFORMATION:
                    System.out.println(toc.getMessage());

                    break;

                case TextOutputCallback.ERROR:
                    System.out.println("Login error: " + toc.getMessage());

                    break;

                case TextOutputCallback.WARNING:
                    System.out.println("Login warning: " + toc.getMessage());

                    break;

                default:
                    throw new IOException("Unknown message type: " + 
                                          toc.getMessageType());
                }
            } else if (callbacks[i] instanceof NameCallback) {
                nameCallback = (NameCallback) callbacks[i];
                nameCallback.setName(user);
            } else if (callbacks[i] instanceof PasswordCallback) {
                passwordCallback = (PasswordCallback) callbacks[i];
                passwordCallback.setPassword(password);
            } else {
                throw new UnsupportedCallbackException(callbacks[i], 
                                                       "Unrecognized Callback");
            }
        }
    }

    //user defined login method which simply counts the number of login attempts
    //and sets the user and password which will be used in the callback above
    public boolean login(String user, char[] password) {
        if (loginTries > 2) {
            System.out.println("Sorry charlie...");

            return false;
        } else {
            this.user = user;
            this.password = password;
        }

        loginTries++;

        try {
            //attempt authentication on the LoginContext
            lc.login();

            //authentication succeeded
            return true;
        } catch (AccountExpiredException aee) {
            System.out.println("Your account has expired. " + 
                               "Please notify your administrator.");
        } catch (CredentialExpiredException cee) {
            System.out.println("Your credentials have expired.");
        } catch (FailedLoginException fle) {
            System.out.println("Authentication Failed");
        } catch (Exception e) {
            System.out.println("Unexpected Exception -unable to continue");
            e.printStackTrace();
        }

        //authentication did not succeed
        return false;
    }
}

⌨️ 快捷键说明

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