博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
freemarker使用shiro标签(spring boot)
阅读量:4028 次
发布时间:2019-05-24

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

首先需要写一个类

/** * 集成Shiro标签 */@Componentpublic class ShiroTagFreeMarkerConfigurer implements InitializingBean {	@Autowired	private Configuration configuration;	@Autowired	private FreeMarkerViewResolver resolver;	@Override	public void afterPropertiesSet() throws Exception {		// 加上这句后,可以在页面上使用shiro标签		configuration.setSharedVariable("shiro", new ShiroTags());		// 加上这句后,可以在页面上用${context.contextPath}获取contextPath		resolver.setRequestContextAttribute("context");	}}

然后在doGetAuthorizationInfo方法中获取我们想要验证的权限,将权限写入roleNames和PermissionNames中

@Override	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {		logger.info("执行Shiro权限认证");		try {			UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();			if (user != null) {				// 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)				SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();								// 根据用户名id查询xxx权限				List
xxxList = xxxWSService.findxxxbyUserInfoId(user.getId()); Set
roleNames = new HashSet
(); Set
permissionNames = new HashSet
(); for (XXX xxx: xxxList) { permissionNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString()); roleNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString() + "_" + xxx.getXxxName().toString()); } // 将权限提供给info         info.setStringPermissions(permissionNames); // 将角色名称提供给info         info.setRoles(roleNames); info.addStringPermission(""); return info; } } catch (Exception e) { logger.error("执行Shiro权限认证异常!", e.getLocalizedMessage() ); e.printStackTrace(); return null; } // 返回null的话,就会导致任何用户访问被拦截的请求时,都会自动跳转到unauthorizedUrl指定的地址 return null; }

最后就可以在前端freemarker模板中使用shiro标签,有xxx角色的人员才可以看到"保存"按钮;

<@shiro.hasAnyRoles name="app_${xxx.id?c}_1,app_${xxx.id?c}_2">	

Shiro包含的标签:

    guest标签:验证当前用户是否为“访客”,即未认证(包含未记住)的用户;
shiro标签:<shiro:guest>
</shiro:guest>  ;
freemark中: <@shiro.guest>  
</@shiro.guest> 
    user标签:认证通过或已记住的用户 shiro标签:<shiro:user> </shiro:user>  ;freemark中: <@shiro.user> </@shiro.user> 
     
authenticated标签:已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。 shiro标签:
<shiro:authenticated> 
</shiro:authenticated>;
freemark中: 
<@shiro.authenticated>
</@shiro.authenticated>
    notAuthenticated标签:未认证通过的用户。与authenticated标签相对。 shiro标签:<shiro:notAuthenticated> </shiro:notAuthenticated>;freemark中: <@shiro.notAuthenticated></@shiro.notAuthenticated>
    
principal标签
输出当前用户信息,通常为登录帐号信息  shiro标签:
Hello,  <@shiro.principal property="name" />  ;
freemarker中: 
 Hello,  <@shiro.principal property="name" />, how are you today?     
    
hasRole标签
:验证当前用户是否属于该角色 ,shiro标签:
 <shiro:hasRole name="administrator"> 
 Administer the system 
</shiro:hasRole> ;
freemarker中:
<@shiro.hasRole name=”admin”>Hello admin!</@shiro.hasRole> 
     
hasAnyRoles标签
验证当前用户是否属于这些角色中的任何一个,角色之间逗号分隔 ,shiro标签:
 <shiro:hasAnyRoles name="admin,user,operator"> 
 Administer the system 
</shiro:hasAnyRoles> ;
freemarker中:
<@shiro.hasAnyRoles name="admin,user,operator">Hello admin!</@shiro.hasAnyRoles>
     
hasPermission标签
验证当前用户是否拥有该权限 ,shiro标签:
 <shiro:hasPermission name="/order:*"> 
 订单 
</shiro:hasPermission> ;
freemarker中:
<@shiro.hasPermission name="/order:*">订单/@shiro.hasPermission> 
    
lacksRole标签
:验证当前用户不属于该角色,与hasRole标签想反,shiro标签: <shiro:hasRole name="admin">  Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name="admin">Hello admin!</@shiro.hasRole> 
     
lacksPermission标签:验证当前用户不拥有某种权限,与hasPermission标签是相对的,
shiro标签:
 <shiro:lacksPermission name="/order:*"> 
trade 
</shiro:lacksPermission> ;
freemarker中:
<@shiro.lacksPermission name="/order:*">trade</@shiro.lacksPermission> 

转载地址:http://ditbi.baihongyu.com/

你可能感兴趣的文章
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
在android上运行native可执行程序
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>
Android之TelephonyManager类的方法详解
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>