給JFinal添加Shiro插件功能,支持Shiro全部註解-HTTL篇

因爲使用HTTL模板引擎,因此也想將Shiro和HTTL集成起來。java

具體實現以下,此嗲嗎和給JFinal添加Shiro插件功能,支持Shiro全部註解-實現篇中的同樣。apache

 

package com.jfinal.ext.plugin.shiro;

import java.util.concurrent.ConcurrentMap;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;


/**
 * 將全部Shiro指令封裝成HTTL的函數。
 *
 * @author dafei
 */
public class ShiroKit {

	/**
	 * 用來記錄那個action或者actionpath中是否有shiro認證註解。
	 */
	private static ConcurrentMap<String, AuthzHandler> authzMaps = null;


	private static final String NAMES_DELIMETER = ",";

	/**
	 * 禁止初始化
	 */
	private ShiroKit() {}

	static void init(ConcurrentMap<String, AuthzHandler> maps) {
		authzMaps = maps;
	}

	static AuthzHandler getAuthzHandler(String actionKey){
		/*
		if(authzMaps.containsKey(controllerClassName)){
			return true;
		}*/
		return authzMaps.get(actionKey);
	}

	/**
	 * 獲取 Subject
	 *
	 * @return Subject
	 */
	protected static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	/**
	 * 驗證當前用戶是否屬於該角色?,使用時與lacksRole 搭配使用
	 *
	 * @param roleName
	 *            角色名
	 * @return 屬於該角色:true,不然false
	 */
	public static boolean hasRole(String roleName) {
		return getSubject() != null && roleName != null
				&& roleName.length() > 0 && getSubject().hasRole(roleName);
	}

	/**
	 * 與hasRole標籤邏輯相反,當用戶不屬於該角色時驗證經過。
	 *
	 * @param roleName
	 *            角色名
	 * @return 不屬於該角色:true,不然false
	 */
	public static boolean lacksRole(String roleName) {
		return !hasRole(roleName);
	}

	/**
	 * 驗證當前用戶是否屬於如下任意一個角色。
	 *
	 * @param roleNames
	 *            角色列表
	 * @return 屬於:true,不然false
	 */
	public static boolean hasAnyRoles(String roleNames) {
		boolean hasAnyRole = false;
		Subject subject = getSubject();
		if (subject != null && roleNames != null && roleNames.length() > 0) {
			// Iterate through roles and check to see if the user has one of the
			// roles
			for (String role : roleNames.split(NAMES_DELIMETER)) {
				if (subject.hasRole(role.trim())) {
					hasAnyRole = true;
					break;
				}
			}
		}
		return hasAnyRole;
	}

	/**
	 * 驗證當前用戶是否屬於如下全部角色。
	 *
	 * @param roleNames
	 *            角色列表
	 * @return 屬於:true,不然false
	 */
	public static boolean hasAllRoles(String roleNames) {
		boolean hasAllRole = true;
		Subject subject = getSubject();
		if (subject != null && roleNames != null && roleNames.length() > 0) {
			// Iterate through roles and check to see if the user has one of the
			// roles
			for (String role : roleNames.split(NAMES_DELIMETER)) {
				if (!subject.hasRole(role.trim())) {
					hasAllRole = false;
					break;
				}
			}
		}
		return hasAllRole;
	}

	/**
	 * 驗證當前用戶是否擁有指定權限,使用時與lacksPermission 搭配使用
	 *
	 * @param permission
	 *            權限名
	 * @return 擁有權限:true,不然false
	 */
	public static boolean hasPermission(String permission) {
		return getSubject() != null && permission != null
				&& permission.length() > 0
				&& getSubject().isPermitted(permission);
	}

	/**
	 * 與hasPermission標籤邏輯相反,當前用戶沒有制定權限時,驗證經過。
	 *
	 * @param permission
	 *            權限名
	 * @return 擁有權限:true,不然false
	 */
	public static boolean lacksPermission(String permission) {
		return !hasPermission(permission);
	}

	/**
	 * 已認證經過的用戶。不包含已記住的用戶,這是與user標籤的區別所在。與notAuthenticated搭配使用
	 *
	 * @return 經過身份驗證:true,不然false
	 */
	public static boolean authenticated() {
		return getSubject() != null && getSubject().isAuthenticated();
	}

	/**
	 * 未認證經過用戶,與authenticated標籤相對應。與guest標籤的區別是,該標籤包含已記住用戶。。
	 *
	 * @return 沒有經過身份驗證:true,不然false
	 */
	public static boolean notAuthenticated() {
		return !authenticated();
	}

	/**
	 * 認證經過或已記住的用戶。與guset搭配使用。
	 *
	 * @return 用戶:true,不然 false
	 */
	public static boolean user() {
		return getSubject() != null && getSubject().getPrincipal() != null;
	}

	/**
	 * 驗證當前用戶是否爲「訪客」,即未認證(包含未記住)的用戶。用user搭配使用
	 *
	 * @return 訪客:true,不然false
	 */
	public static boolean guest() {
		return !user();
	}

	/**
	 * 輸出當前用戶信息,一般爲登陸賬號信息。
	 * @return 當前用戶信息
	 */
	public String principal(){
		if (getSubject() != null) {
            // Get the principal to print out
            Object principal = getSubject().getPrincipal();
            return principal.toString();
        }
		return "";
	}
}

 

在httl.properties中添加一行代碼。ide

 

import.methods+=com.jfinal.ext.plugin.shiro.ShiroKit

集成完畢,如何使用?函數

 

 

<!--#if(hasRole("root"))-->
	<td>
	<a href="school/selectSchool?school_id=${school.id}&school_name=${school.name}&callback_action=/class/index" class="btn btn-large btn-block btn-success">班級列表</a>
	</td>
<!--#end-->


<!--#if(hasPermission("card:confirm"))-->
		<button class="btn btn-primary btn-huge btn-wide"  tabindex="3">添加</button>
<!--#end-->

 

用起來還不錯。插件

相關文章
相關標籤/搜索