freemarker使用shiro标签(spring boot)

2018年07月03日 14:20:37 niu_sayok 阅读数:348更多

个人分类: freeMarkerShiro
 

首先需要写一个类

  1.  
    /**
  2.  
    * 集成Shiro标签
  3.  
    */
  4.  
    @Component
  5.  
    public class ShiroTagFreeMarkerConfigurer implements InitializingBean {
  6.  
     
  7.  
    @Autowired
  8.  
    private Configuration configuration;
  9.  
     
  10.  
    @Autowired
  11.  
    private FreeMarkerViewResolver resolver;
  12.  
     
  13.  
    @Override
  14.  
    public void afterPropertiesSet() throws Exception {
  15.  
    // 加上这句后,可以在页面上使用shiro标签
  16.  
    configuration.setSharedVariable("shiro", new ShiroTags());
  17.  
    // 加上这句后,可以在页面上用${context.contextPath}获取contextPath
  18.  
    resolver.setRequestContextAttribute("context");
  19.  
    }
  20.  
    }

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

  1.  
    @Override
  2.  
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  3.  
    logger.info("执行Shiro权限认证");
  4.  
    try {
  5.  
    UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
  6.  
    if (user != null) {
  7.  
    // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
  8.  
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  9.  
     
  10.  
    // 根据用户名id查询xxx权限
  11.  
    List<XXX> xxxList = xxxWSService.findxxxbyUserInfoId(user.getId());
  12.  
     
  13.  
    Set<String> roleNames = new HashSet<String>();
  14.  
    Set<String> permissionNames = new HashSet<String>();
  15.  
    for (XXX xxx: xxxList) {
  16.  
    permissionNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString());
  17.  
    roleNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString() + "_" + xxx.getXxxName().toString());
  18.  
    }
  19.  
     
  20.  
    // 将权限提供给info
  21.  
            info.setStringPermissions(permissionNames);
  22.  
    // 将角色名称提供给info
  23.  
            info.setRoles(roleNames);
  24.  
     
  25.  
    info.addStringPermission("");
  26.  
    return info;
  27.  
    }
  28.  
     
  29.  
    } catch (Exception e) {
  30.  
    logger.error("执行Shiro权限认证异常!", e.getLocalizedMessage() );
  31.  
    e.printStackTrace();
  32.  
    return null;
  33.  
    }
  34.  
    // 返回null的话,就会导致任何用户访问被拦截的请求时,都会自动跳转到unauthorizedUrl指定的地址
  35.  
    return null;
  36.  
    }

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

  1.  
    <@shiro.hasAnyRoles name="app_${xxx.id?c}_1,app_${xxx.id?c}_2">
  2.  
    <button type="submit"class="btn green" style="padding:6px 22px">保 存</button>
  3.  
    </@shiro.hasAnyRoles>

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> 

原文 转自:  https://blog.csdn.net/sayoko06/article/details/80897658

freemarker使用shiro标签(spring boot)的更多相关文章

  1. Spring Boot使用模板freemarker【从零开始学Spring Boot(转)

    视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à  ...

  2. shiro 和 spring boot 的集成

    1 添加依赖 使用 shiro-spring-boot-web-starter 在 spring boot 中集成 shiro 只需要再添加一个依赖 <dependency> <gr ...

  3. Shiro+JWT+Spring Boot Restful简易教程

    序言 我也是半路出家的人,如果大家有什么好的意见或批评,请务必issue下. 项目地址:https://github.com/Smith-Cruise/Spring-Boot-Shiro . 如果想要 ...

  4. Freemarker 的 Shiro 标签使用详解

    一.引入依赖(已解决版本冲突) <!-- shiro-freemarker-tags start --> <dependency> <groupId>net.min ...

  5. Shiro结合Spring boot开发权限管理系统

    前一篇文章说了,我从开始工作就想有一个属于自己的博客系统,当然了,我想的是多用户的博客,大家都可以发文章记笔记,我最初的想法就是这样. 博客系统搭建需要使用的技术: 1.基于Spring boot 2 ...

  6. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  7. Spring boot整合shiro框架

    ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...

  8. (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...

  9. Spring Boot Shiro

    Shiro 核心 API Subject:用户主体(每次请求都会创建Subject). principal:代表身份.可以是用户名.邮件.手机号码等等,用来标识一个登录主体的身份. credentia ...

随机推荐

  1. 如何理解 PHP的依赖注入(DI) 和 控制反转(IoC)

    名词解释: IoC - Inversion of Control 控制反转 DI - Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式, ...

  2. DRF框架(三)——media资源路径设置、多表设计复习及补充、序列化组件(ModelSerializer)操作多表(序列化与反序列化)、多表序列化与反序列化整合(重点)

    media资源路径设置  (设置好后把图片放在这个文件夹中,通过链接能访问到图片) 1.先在根目录设置一个media文件夹 2.配置settings.py,加上下面的 MEDIA_URL = '/me ...

  3. *** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS

    Warning提示的原因是 一些未使用的函数被编译进入芯片,浪费了RAM/ROM. 解决的方法: 1.将不用的函数注释: 2.在未使用函数的首尾加条件编译 #ifdef 函数名 和 #endif ,不 ...

  4. MySQL学习一:建表

    目标:创建三张表,学生表student(sid,name,gender), 课程表course(cid,name), 分数mark(mid, sid, cid, gender); 要求sid, cid ...

  5. Jmeter相关参数

    一.线程组 线程组主要包含三个参数:线程数.准备时长(Ramp-Up Period(in seconds)).循环次数. 线程数:虚拟用户数.一个虚拟用户占用一个进程或线程.设置多少虚拟用户数在这里也 ...

  6. 给element添加自定义图标

    element为我们提供了丰富的好用的组件,图标的样式也很多,但还是有一些常用的图标没有在官方图标库里边,比如说微信.淘宝.支付宝等等.那么如何把我们需要的图标添加到进去呢? 因为element有官方 ...

  7. 阿里巴巴 Java 开发手册 (十)MySQL 数据库

    (一) 建表规约 1. [强制]表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint ( 1 表示是,0 表示否). 说明:任何字段如果为非负数,必须 ...

  8. SpringCloud微服务基础学习

    看了蚂蚁课堂的微服务学习,确实学习了不少关于微服务的知识,现在总结学习如下 : SpringCloud微服务基础单点系统架构传统项目架构传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一 ...

  9. 以yarn-client方式提交spark任务,任务一直卡在ACCEPTED状态

    问题背景 spark是以客户端的方式安装的,并没有启动spark的mesos集群,这时候的spark就相当与hive客户端. 以local模型和yarn-cluster方式提交任务,都能正确额执行,但 ...

  10. Java之路---Day10(抽象)

    2019-10-24-23:21:17 目录 1.抽象的方法 2.抽象类 3.抽象类和抽象方法的使用 4.抽象类的注意事项 5.案例代码 1.抽象的方法 What:如果父类当中的方法不确定如何进行{} ...