Spring Boot 整合 Shiro(四)thymeleaf模板权限控制 附源码

前言

本篇是根据笔者上篇《Spring Boot 整合 Shiro(三)Kaptcha验证码 附源码》进行修改,若有不明白之处,请转上篇了解。

1.添加依赖


 
 
  1. <!--thymeleaf和shiro整合包-->
  2. <dependency>
  3. <groupId>com.github.theborakompanioni </groupId>
  4. <artifactId>thymeleaf-extras-shiro </artifactId>
  5. <version>2.0.0 </version>
  6. </dependency>
  7. <!--thymeleaf转换HTML网页格式包 加完之后能够识别thymeleaf以外的扩展标签-->
  8. <dependency>
  9. <groupId>net.sourceforge.nekohtml </groupId>
  10. <artifactId>nekohtml </artifactId>
  11. <version>1.9.22 </version>
  12. </dependency>

2.修改ShiroConfig

shiro与thymeleaf结合


 
 
  1. @Bean
  2. public ShiroDialect shiroDialect(){
  3. return new ShiroDialect();
  4. }

 3.配置

支持网页格式


 
 
  1. spring:
  2. thymeleaf:
  3. mode: HTML5

4.界面引入shiro

<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
 
 

配置相应权限的标签, shiro:hasPermission验证当前用户是否拥有权限,有则显示。


 
 
  1. <!DOCTYPE html>
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
  3. <head>
  4. <meta charset="UTF-8"> </meta>
  5. <title>成功 </title>
  6. </head>
  7. <body>
  8. <h1>登录成功 </h1>
  9. <a href="/loginOut">退出 </a>
  10. <a href="#" shiro:hasPermission="user:show">show </a>
  11. <a href="#" shiro:hasPermission="user:show1">show1 </a>
  12. </body>
  13. </html>

5.测试

界面初始设定了show和show1,因为模拟的账户只设置了user:show、user:admin,所以界面只有show显示出来

 

6.项目源码

源码下载

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值