freemarker使用shiro标签(spring boot)
freemarker使用shiro标签(spring boot)
首先需要写一个类
- /**
- * 集成Shiro标签
- */
- @Component
- public 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<XXX> xxxList = xxxWSService.findxxxbyUserInfoId(user.getId());
- Set<String> roleNames = new HashSet<String>();
- Set<String> permissionNames = new HashSet<String>();
- 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">
- <button type="submit"class="btn green" style="padding:6px 22px">保 存</button>
- </@shiro.hasAnyRoles>
Shiro包含的标签:
原文 转自: https://blog.csdn.net/sayoko06/article/details/80897658
freemarker使用shiro标签(spring boot)的更多相关文章
- Spring Boot使用模板freemarker【从零开始学Spring Boot(转)
视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à ...
- shiro 和 spring boot 的集成
1 添加依赖 使用 shiro-spring-boot-web-starter 在 spring boot 中集成 shiro 只需要再添加一个依赖 <dependency> <gr ...
- Shiro+JWT+Spring Boot Restful简易教程
序言 我也是半路出家的人,如果大家有什么好的意见或批评,请务必issue下. 项目地址:https://github.com/Smith-Cruise/Spring-Boot-Shiro . 如果想要 ...
- Freemarker 的 Shiro 标签使用详解
一.引入依赖(已解决版本冲突) <!-- shiro-freemarker-tags start --> <dependency> <groupId>net.min ...
- Shiro结合Spring boot开发权限管理系统
前一篇文章说了,我从开始工作就想有一个属于自己的博客系统,当然了,我想的是多用户的博客,大家都可以发文章记笔记,我最初的想法就是这样. 博客系统搭建需要使用的技术: 1.基于Spring boot 2 ...
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...
- Spring boot整合shiro框架
ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...
- (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】
(本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...
- Spring Boot Shiro
Shiro 核心 API Subject:用户主体(每次请求都会创建Subject). principal:代表身份.可以是用户名.邮件.手机号码等等,用来标识一个登录主体的身份. credentia ...
随机推荐
- day21——面向对象初识、结构、从类名研究类、从对象研究类、logging模块进阶版
day21 面向对象的初识 面向对象第一个优点: 对相似功能的函数,同一个业务下的函数进行归类,分类. 想要学习面向对象必须站在一个上帝的角度去分析考虑问题. 类: 具有相同属性和功能的一类事物. 对 ...
- 【C语言】学不会的指针
指针 前言: 指针是C语言程序的核心,刚开始学指针,嗯....这样呀,貌似不难呀:之后开始用指针,&p,p,*p,**p,这些指针在用的时候,额.....什么东东?每次都要想半天,特别是遇到双 ...
- es常用操作
1.查看所有索引 _cat/indices?v 2.删除索引 DELETE my_index 3.查询缓存 curl /my_index/_search?request_cache=true' -d' ...
- Oracle数据库基本知识-原理,实例,表空间,用户,表
1.数据库原理及sql 数据库:是人们存放数据,访问数据,操作数据的存储仓库. DB:数据库,按存储结构来组织,存储和管理的数据仓库 DBMS:数据库管理系统,管理数据库的软件 SQL:结构化查询语言 ...
- js 不同浏览器的类型判断 navigator.userAgent
一.通过navigator.userAgent来进行浏览器类型判断 // 判断浏览器内核.手机系统等,使用 browser.userAgent.mobile var browser = { userA ...
- 微信小程序文档
提示框: wx.showToast(OBJECT) 显示消息提示框 OBJECT参数说明: 示例代码: wx.showToast({ title: '成功', icon: 'success', dur ...
- MVC视图中 TextBoxFor 数据格式化
@Html.TextBoxFor(m => m.Birthday,"{0:yyyy-MM-dd}", new { @class = "m-wrap small&qu ...
- Kubernetes第十一章--部署微服务电商平台
- .net 后台以post方式调用微信公众平台接口
public class Fresult { public int errcode { get; set; } public string errmsg { get; set; } public st ...
- SQL Server 用一张表的数据更新另一张表的数据(转载)
文章一:SQL Server中如何基于一个表的数据更新另一个表的对应数据的SQL语句脚本 https://codedefault.com/2017/sql-server-update-from-a-s ...