博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beetl 和 shrio 结合
阅读量:6095 次
发布时间:2019-06-20

本文共 4376 字,大约阅读时间需要 14 分钟。

  hot3.png

shrio 提供有jsp 标签,供在jsp 里使用,如果想在beetl中使用,有俩种方法,

一是beetl支持集成jsp页面,所以你可以在在jsp里使用shrio标签

另外,beetl 使用自定义函数写了shiro tag功能,你可以像使用shiro标签那样使用shiro

package com.ext;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.util.Map;import org.apache.shiro.SecurityUtils;import org.apache.shiro.subject.Subject;import org.bee.tl.core.GroupTemplate;/*gt.registerFunctionPackage("so",new ShiroExt ());你可以在模板里直接调用,譬如@if(so.isGuest()) {*/public class ShiroExt {	/**	 * The guest tag	 * 	 * @return	 */	public boolean isGuest() {		return getSubject() == null || getSubject().getPrincipal() == null;	}	/**	 * The user tag	 * 	 * @return	 */	public boolean isUser() {		return getSubject() != null && getSubject().getPrincipal() != null;	}	/**	 * The authenticated tag	 * 	 * @return	 */	public boolean isAuthenticated() {		return getSubject() != null && getSubject().isAuthenticated();	}	public boolean isNotAuthenticated() {		return !isAuthenticated();	}	/**	 * The principal tag	 * 	 * @param map	 * @return	 */	public String principal(Map map) {		String strValue = null;		if (getSubject() != null) {			// Get the principal to print out			Object principal;			String type = map != null ? (String) map.get("type") : null;			if (type == null) {				principal = getSubject().getPrincipal();			} else {				principal = getPrincipalFromClassName(type);			}			String property = map != null ? (String) map.get("property") : null;			// Get the string value of the principal			if (principal != null) {				if (property == null) {					strValue = principal.toString();				} else {					strValue = getPrincipalProperty(principal, property);				}			}		}		if (strValue != null) {			return strValue;		} else {			return null;		}	}	/**	 * The hasRole tag	 * 	 * @param roleName	 * @return	 */	public boolean hasRole(String roleName) {		return getSubject() != null && getSubject().hasRole(roleName);	}	/**	 * The lacksRole tag	 * 	 * @param roleName	 * @return	 */	public boolean lacksRole(String roleName) {		boolean hasRole = getSubject() != null				&& getSubject().hasRole(roleName);		return !hasRole;	}	/**	 * The hasAnyRole tag	 * 	 * @param roleNames	 * @return	 */	public boolean hasAnyRole(String roleNames) {		boolean hasAnyRole = false;		Subject subject = getSubject();		if (subject != null) {			// Iterate through roles and check to see if the user has one of the			// roles			for (String role : roleNames.split(",")) {				if (subject.hasRole(role.trim())) {					hasAnyRole = true;					break;				}			}		}		return hasAnyRole;	}	/**	 * The hasPermission tag	 * 	 * @param p	 * @return	 */	public boolean hasPermission(String p) {		return getSubject() != null && getSubject().isPermitted(p);	}	/**	 * The lacksPermission tag	 * 	 * @param p	 * @return	 */	public boolean lacksPermission(String p) {		return !hasPermission(p);	}	@SuppressWarnings({ "unchecked" })	private Object getPrincipalFromClassName(String type) {		Object principal = null;		try {			Class cls = Class.forName(type);			principal = getSubject().getPrincipals().oneByType(cls);		} catch (ClassNotFoundException e) {		}		return principal;	}	private String getPrincipalProperty(Object principal, String property) {		String strValue = null;		try {			BeanInfo bi = Introspector.getBeanInfo(principal.getClass());			// Loop through the properties to get the string value of the			// specified property			boolean foundProperty = false;			for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {				if (pd.getName().equals(property)) {					Object value = pd.getReadMethod().invoke(principal,							(Object[]) null);					strValue = String.valueOf(value);					foundProperty = true;					break;				}			}			if (!foundProperty) {				final String message = "Property [" + property						+ "] not found in principal of type ["						+ principal.getClass().getName() + "]";				throw new RuntimeException(message);			}		} catch (Exception e) {			final String message = "Error reading property [" + property					+ "] from principal of type ["					+ principal.getClass().getName() + "]";			throw new RuntimeException(message, e);		}		return strValue;	}	protected Subject getSubject() {		return SecurityUtils.getSubject();	}	public static void main(String[] args) {		GroupTemplate gt = new GroupTemplate();		gt.registerFunctionPackage("shiro", new ShiroExt());	}}
 

转载于:https://my.oschina.net/xiandafu/blog/143109

你可能感兴趣的文章
HashMap的实现原理笔记
查看>>
最快渲染方案
查看>>
Python基础要点总结
查看>>
go语言结构体入门
查看>>
virtualbox安装ghost xp
查看>>
ServletRequest.getParameter() && ServletRequest.getParameterValues();
查看>>
排序系列-插入排序
查看>>
MySQL知识点
查看>>
在线ASP转PHP
查看>>
js 小数取整的函数
查看>>
Maven 私有库和本地库的安装与配置 Sonatype Nexus+Maven
查看>>
开始写博客楼
查看>>
MySQL分页优化
查看>>
Linux 学习--Linux mint 安装
查看>>
Spring学习笔记(六)
查看>>
返回键失效,返回上一级
查看>>
file_get_content 和curl以及fopen 谁的效率最高
查看>>
JAVA虚拟机垃圾回收机制和JAVA排错三剑客
查看>>
MySQL的跨年周统计问题
查看>>
html多媒体简介:
查看>>