因为使用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-->
用起来还不错。插件