SpringBoot 中为freemarker加载shiroTargets标签库

本文介绍如何在SpringBoot项目中使用Maven依赖集成Shiro与Freemarker,实现用户权限验证和角色检查等功能。通过配置FreeMarkerConfigurer Bean,可以在Freemarker模板中使用Shiro标签进行用户身份验证。

maven依赖


		<dependency>
			<groupId>net.mingsoft</groupId>
			<artifactId>shiro-freemarker-tags</artifactId>
			<version>1.0.0</version>
		</dependency>

在SpringBoot的配置类中

//在freemarker中加载shiro target
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(){
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("classpath:/templates");
        Map<String, Object> variables = new HashMap<>(1);
        variables.put("shiro", new ShiroTags());
        configurer.setFreemarkerVariables(variables);
        System.out.println();
        Properties settings = new Properties();
        settings.setProperty("default_encoding", "utf-8");
        settings.setProperty("number_format", "0.##");
        configurer.setFreemarkerSettings(settings);
        return configurer;
    }

,添加了这个bean后,在application.properties中的配置会失效,默认配置也会失效,因此setTemplateLoaderPath也必须要配置

 

这样就可以在freemarker中使用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>
     
因为字数限制 下面目录只是部分 day10-Redis面试篇│ Redis面试篇-01.Redis主从-搭建主从集群.mp4│ Redis面试篇-02.Redis主从-主从同步原理.mp4│ Redis面试篇-03.Redis主从-主从集群优化.mp4│ Redis面试篇-04.Reids主从-哨兵原理.mp4│ Redis面试篇-05.Redis主从-搭建哨兵集群.mp4│ Redis面试篇-06.Redis分片集群-搭建分片集群.mp4│ Redis面试篇-07.Redis分片集群-散列插槽.mp4│ Redis面试篇-08.Redis数据结构-RedisObject.mp4│ Redis面试篇-09.Redis数据结构-SkipList.mp4│ Redis面试篇-10.Redis数据结构-SortedSet.mp4│ Redis面试篇-11.Redis内存回收-过期KEY处理.mp4│ Redis面试篇-12.Redis内存回收-内存淘汰策略.mp4│ Redis面试篇-13.Redis缓存-缓存一致性.mp4│ Redis面试篇-14.Redis缓存-缓存穿透.mp4│ Redis面试篇-15.Redis缓存-缓存雪崩.mp4│ Redis面试篇-16.Redis缓存-缓存击穿.mp4│ ├─day11-微服务面试篇│ 微服务面试篇-01.分布式事务-CAP和BASE.mp4│ 微服务面试篇-02.分布式事务-AT模式脏写问题.mp4│ 微服务面试篇-03.分布式事务-TCC模式.mp4│ 微服务面试篇-04.分布式事务-最大努力通知.mp4│ 微服务面试篇-05.注册中心-环境隔离.mp4│ 微服务面试篇-06.注册中心-服务分级模型.mp4│ 微服务面试篇-07.注册中心-Eureka和Nacos.mp4│ 微服务面试篇-08.远程调用-源码跟踪及负载均衡原理.mp4│ 微服务面试篇-09.远程调用-切换负载均衡算法.mp4│ 微服务面试篇-10.服务保护-线程隔离方案对比.mp4│ 微服务面试篇-11.服务保护-滑动窗口计数器算法.mp4│ 微服务面试篇-12.服务保护-漏桶算法.mp4│ 微服务面试篇-13.服务保护-令牌桶算法.mp4│ 完结撒花.mp4│└─资料.zip
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值