天天看點

Spring Boot 整合 Shiro(四)thymeleaf模闆權限控制 附源碼前言1.添加依賴2.修改ShiroConfig 3.配置4.界面引入shiro5.測試6.項目源碼

前言

本篇是根據筆者上篇《Spring Boot 整合 Shiro(三)Kaptcha驗證碼 附源碼》進行修改,若有不明白之處,請轉上篇了解。

1.添加依賴

<!--thymeleaf和shiro整合包-->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--thymeleaf轉換HTML網頁格式包 加完之後能夠識别thymeleaf以外的擴充标簽-->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
           

2.修改ShiroConfig

shiro與thymeleaf結合

@Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
           

 3.配置

支援網頁格式

spring:
  thymeleaf:
    mode: HTML5
           

4.界面引入shiro

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

配置相應權限的标簽, shiro:hasPermission驗證目前使用者是否擁有權限,有則顯示。

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8"></meta>
    <title>成功</title>
</head>
<body>
<h1>登入成功</h1>
<a href="/loginOut" target="_blank" rel="external nofollow" >退出</a>
<a href="#" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  shiro:hasPermission="user:show">show</a>
<a href="#" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  shiro:hasPermission="user:show1">show1</a>
</body>
</html>
           

5.測試

界面初始設定了show和show1,因為模拟的賬戶隻設定了user:show、user:admin,是以界面隻有show顯示出來

Spring Boot 整合 Shiro(四)thymeleaf模闆權限控制 附源碼前言1.添加依賴2.修改ShiroConfig 3.配置4.界面引入shiro5.測試6.項目源碼

6.項目源碼

源碼下載下傳

繼續閱讀