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

📄 webconsolejaas.java

📁 JAAS 例子代码
💻 JAVA
字号:
/**
 * WebconsoleJaas.java
 *
 * Copyright 2009 Tidal Software. All rights reserved.
 *
 * Revision History:
 * Date             Name                Action
 * ------------------------------------------------
 * Feb 12, 2009       wayne            Created
 */
package com.tidalsoft.webconsole.sso;

/**
 * WebConsole JAAS Util Class, Login once and action with loginContext
 * @author  wayne
 */

import java.security.*;

import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;

import com.sun.security.auth.callback.*;

public class WebconsoleJaas {
	/**
	 * JAAS login entry name
	 */
	private static final String WEBCONSOLE_SERVER = "tidalsoft.webconsole.server";
	
	/**
	 * Get LoginContext with login and action with loginContext;
	 */
	private static LoginContext context = null;

	/**
	 * Single instance
	 */
	private static WebconsoleJaas instance = null;
	
	private WebconsoleJaas() {	
	}
	
	/**
	 * GetInstance method for Singleton pattern 
	 * @return
	 */
	public static WebconsoleJaas getInstance() {
		if(instance==null) {
			instance = new WebconsoleJaas();
			try {
				instance.login(WEBCONSOLE_SERVER);
				return instance;
			}catch(LoginException le) {
				le.printStackTrace();
				instance = null;
			}
		}
		
		return instance;		
	}
	
	/**
	 * Login to get a login context, this would be used in executing action
	 * @param name
	 * @throws LoginException
	 */
	public void login(String name) throws LoginException {
		if (context != null)
			return;

		// Create a callback handler
		CallbackHandler callbackHandler = new TextCallbackHandler();

		// Create a LoginContext with a callback handler
		context = new LoginContext(name, callbackHandler);
		context.login();
	}

	/**
	 * Logout and release context
	 * @throws LoginException
	 */
	public void logout() throws LoginException {
		context.logout();
		context = null;
	}

	/**
	 * Perform action as authenticated user
	 * 
	 * @param action
	 * @return
	 * @throws PrivilegedActionException
	 */
	public UserAuthResult action(
			PrivilegedExceptionAction<UserAuthResult> action)
			throws PrivilegedActionException {
		Subject subject = context.getSubject();
		return Subject.doAs(subject, action);
	}
}

⌨️ 快捷键说明

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