1. 程式人生 > 程式設計 >springboot整合shiro之thymeleaf使用shiro標籤的方法

springboot整合shiro之thymeleaf使用shiro標籤的方法

thymeleaf介紹

簡單說, Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 P 。相較與其他的模板引擎,它有如下三個極吸引人的特點:

1.Thymeleaf 在有網路和無網路的環境下皆可執行,即它可以讓美工在瀏覽器檢視頁面的靜態效果,也可以讓程式設計師在伺服器檢視帶資料的動態頁面效果。這是由於它支援 html 原型,然後在 html 標籤裡增加額外的屬性來達到模板+資料的展示方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,所以 thymeleaf 的模板可以靜態地執行;當有資料返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。

2.Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、 OGNL表示式效果,避免每天套模板、該jstl、改標籤的困擾。同時開發人員也可以擴充套件和建立自定義的方言。

3.Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美整合的可選模組,可以快速的實現表單繫結、屬性編輯器、國際化等功能。

我們緊接著 上一篇 文章,我們使用賬號 jack 和賬號 Tom 來分別登入,在上一篇文章測試中可以看到,這兩個賬號無論哪一個登入,首頁頁面都會顯示 add 頁面和 update 頁面兩個超連結,而對於這兩個賬號來說,一個擁有訪問 add

頁面的許可權,一個擁有訪問 update 頁面的許可權。那麼問題來了,如何才能根據不同使用者的身份角色資訊來顯示不同的頁面內容呢?這就要使用 shiro 標籤了

thymeleaf 模板引擎使用 shiro 標籤

引入依賴

<dependency>
    <groupId>com..theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

配置類 ShiroConfig

@Configuration
public class ShiroConfig {

    // 安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    // thymeleaf模板引擎中使用shiro標籤時,要用到
    @Bean
    public ShiroDialect getShiroDialect() {
        return new ShiroDialect();
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        // 設定登入頁面url
        shiroFilterFactoryBean.setLoginUrl("/user/login");
        shiroFilterFactoryBean.setSuccessUrl("/user/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/user/unauthorized");

        // 注意此處使用的是LinkedHashMap是有順序的,shiro會按從上到下的順序匹配驗證,匹配了就不再繼續驗證
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();

        filterChainDefinitionMap.put("/layer/**","anon");// 靜態資源放行
        filterChainDefinitionMap.put("/img/**","anon");
        filterChainDefinitionMap.put("//**","anon");
        // add.html頁面放行
        filterChainDefinitionMap.put("/user/add","authc");
        // update.html必須認證
        filterChainDefinitionMap.put("/user/update","authc");
        // index.html必須通過認證或者通過記住我登入的,才可以訪問
        filterChainDefinitionMap.put("/user/index","user");
        // 設定授權,只有user:add許可權的才能請求/user/add這個url
        filterChainDefinitionMap.put("/user/add","perms[user:add]");
        filterChainDefinitionMap.put("/user/update","perms[user:update]");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
}

index.html 頁面

  • 首先要引入shiro 的名稱空間:xmlns:shiro=http://www.pollix.at/thymeleaf/shiro
  • 使用相應的 shiro 標籤
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" />
&l客棧t;/head>
<body>
    <h1>首頁</h1>

    <a shiro:hasPermission="'user:add'" th:href="@{/user/add}" rel="external nofollow" >add</a><br>
    <a shiro:hasPermission="'user:update'" th:href="@{/user/}" rel="external nofollow" >update</a>
    update
    <a th:href="@{/user/logout}" rel="external nofollow" >退出登入</a>
</body>
</html>

shiro 標籤說明

https://shiro.apache.org/jsp-tag-library.html

https://github.com/apache/shiro/blob/main/web/src/main/resources/META-INF程式設計客棧/shiro.tld

標籤 含義
shiro:principal 當前使用者的登入資訊,使用者名稱之類
shiro:guest="" 驗證是否是遊客,即未認證的使用者
shiro:user="" 驗證是否是已認證或已記住使用者
shiro:authenticated="" 驗證是否是已認證使用者,不包括已記住使用者
shiro:notAuthenticated= “” 未認證使用者,但是 已記住使用者
shiro:lacksRole=“admin” 表示沒有 admin 角色的使用者
shiro:hasAllRoles=“admin,user1” 表示需要同時擁有兩種角色
shiro:hasAnyRoles=“admin,user1” 表示 擁有其中一個角色即可
shiro:lacksPermission=“admin:delete” 類似於 shiro:lacksRole
shiro:hasAllPermissions=“admin:delete,admin:edit” 類似於 shiro:hasAllRoles
shiro:hasAnyPermission=“admin:delete,admin:edit” 類似於 hasAnyRoles

測試

測試一

首先使用賬號 jack 來登入,檢視首頁頁面,如下

在這裡插入圖片描述

再看控制檯日誌,如下,注意賬號 jack 擁有的許可權,在 index.html 頁面有兩個 <shiro:hasPermission> 標籤,故而進行了兩次授權操作

在這裡插入圖片描述

測試二

再使用賬號 Tom 來登入,檢視首頁頁面,如下

在這裡插入圖片描述

再看控制檯日誌,如下,注意賬號 Tom 擁有的許可權,在 index.html 頁面有兩個 <shiro:hasPermission> 標籤,故而進行了兩次授www.cppcns.com權操作

在這裡插入圖片描述

原始碼:springboot-shiro

到此這篇關於springboot整合shiro之thymeleaf使用shiro標籤的文章就介紹到這了,更多相關springboot整合shiro使用shiro標籤內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!