博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Velocity模版进行shiro验证
阅读量:4322 次
发布时间:2019-06-06

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

先在Spring配置Velocity视图解析器

UTF-8
UTF-8
text/html;charset=UTF-8

在WEB-INF文件夹创建velocity-toolbox.xml

shiro
application
com.wstro.shiro.VelocityShiro

再来看

com.wstro.shiro.VelocityShiro

import org.apache.shiro.SecurityUtils;import org.apache.shiro.subject.Subject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Shiro权限标签(Velocity版) *  * @author chenshun * @email sunlightcs@gmail.com * @date 2016年12月3日 下午11:32:47 */public class VelocityShiro {	private Logger logger = LoggerFactory.getLogger(getClass());	/**	 * 是否拥有该权限	 * 	 * @param permission	 *            权限标识	 * @return true:是 false:否	 */	public boolean hasPermission(String permission) {		logger.info(permission);		Subject subject = SecurityUtils.getSubject();		return subject != null && subject.isPermitted(permission);	}	/**	 * 是否拥有该权限	 * 	 * @param permission	 *            权限标识	 * @return true:是 false:否	 */	public static boolean hasPermissionInMethod(String permission) {		Subject subject = SecurityUtils.getSubject();		return subject != null && subject.isPermitted(permission);	}}
ShiroUtils工具类

import org.apache.shiro.SecurityUtils;import org.apache.shiro.session.Session;import org.apache.shiro.subject.Subject;import com.wstro.entity.SysUserEntity;/** * Shiro工具类 *  * @author chenshun * @email sunlightcs@gmail.com * @date 2016年11月12日 上午9:49:19 */public class ShiroUtils {	public static Session getSession() {		return SecurityUtils.getSubject().getSession();	}	public static Subject getSubject() {		return SecurityUtils.getSubject();	}	public static SysUserEntity getUserEntity() {		return (SysUserEntity)SecurityUtils.getSubject().getPrincipal();	}	public static Long getUserId() {		return getUserEntity().getUserId();	}		public static void setSessionAttribute(Object key, Object value) {		getSession().setAttribute(key, value);	}	public static Object getSessionAttribute(Object key) {		return getSession().getAttribute(key);	}	public static boolean isLogin() {		return SecurityUtils.getSubject().getPrincipal() != null;	}	public static void logout() {		SecurityUtils.getSubject().logout();	}		public static String getKaptcha(String key) {		String kaptcha = getSessionAttribute(key).toString();		getSession().removeAttribute(key);		return kaptcha;	}}
前台Velocity模版直接调用就可以

#if($shiro.hasPermission("sys:project${projectcategory}:save")) 新增				#end
这里只实现了Permission验证。如果要多个自己可以模仿这个

还有

转载于:https://www.cnblogs.com/zhousiwei/p/10625892.html

你可能感兴趣的文章
产品经理 - 登录 注册
查看>>
小白的python进阶历程------05.占位符
查看>>
CF414BMashmokh and ACMDP
查看>>
Notepad++ 通过g++编译
查看>>
JAVA基础2——类初始化相关执行顺序
查看>>
转:Zend Framework 重定向方法(render, forward, redirect)
查看>>
Linux下查看磁盘与目录的容量——df、du
查看>>
关于日记app的思考
查看>>
使用sencha的cmd创建项目时提示找不到\Sencha\Cmd\repo\.sencha\codegen.json
查看>>
如何快速启动一个Java Web编程框架
查看>>
MSP430单片机存储器结构总结
查看>>
文本框过滤特殊符号
查看>>
教育行业安全无线网络解决方案
查看>>
7个杀手级的开源监测工具
查看>>
软件架构学习小结
查看>>
C语言实现UrlEncode编码/UrlDecode解码
查看>>
返回用户提交的图像工具类
查看>>
树链剖分 BZOJ3589 动态树
查看>>
挑战程序设计竞赛 P131 区间DP
查看>>
【例9.9】最长公共子序列
查看>>