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

📄 workplaceloginmodule.java

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

import java.util.*;
import java.io.IOException;
import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import javax.security.auth.spi.*;


public class WorkPlaceLoginModule implements LoginModule {
    //initial state
    private Subject subject;
    private CallbackHandler callbackHandler;
    private Map sharedState;
    private Map options;

    //configurable option
    private boolean debug = false;

    //the authentication status
    private boolean succeeded = false;
    private boolean commitSucceeded = false;

    //username and password
    private String username;
    private char[] password;
    ;

    private WorkPlacePrincipal userPrincipal;

    //the initialize method is called by the LoginContext
    public void initialize(Subject subject, CallbackHandler callbackHandler, 
                           Map sharedState, Map options) {
        this.subject = subject;
        this.callbackHandler = callbackHandler;
        this.sharedState = sharedState;
        this.options = options;
    }

    //this is the login method that is called by the LoginContext
    public boolean login() throws LoginException {
        //prompt for a username and password
        if (callbackHandler == null) {
            throw new LoginException("Error:no CallbackHandler available");
        }

        //Two Callback are created,one for the name and one for the password
        Callback[] callbacks = new Callback[2];
        callbacks[0] = new NameCallback("JWorkPlace username:");
        callbacks[1] = new PasswordCallback("JWorkPlace password:", false);

        try {
            //this is the callback to our LoginHandler
            callbackHandler.handle(callbacks);


            //get the username and password provided in the callback
            username = ((NameCallback) callbacks[0]).getName();

            char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();

            if (tmpPassword == null) {
                //treat a NULL password as an empty password
                tmpPassword = new char[0];
            }

            password = new char[tmpPassword.length];
            System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
            ((PasswordCallback) callbacks[1]).clearPassword();
        } catch (java.io.IOException ioe) {
            throw new LoginException(ioe.toString());
        } catch (UnsupportedCallbackException uce) {
            throw new LoginException("Error: " + 
                                     uce.getCallback().toString());
        }


        //verify the username/password
        //insert code to verify user /password here
        //Authentication succeeded
        succeeded = true;

        return true;

        //Authentication failed succeeded =false;
        throw new FailedLoginException("Password Incorrect");
    }

    //the 2 phases of authentication 

⌨️ 快捷键说明

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