springboot整合shiro之thymeleaf使用shiro標簽的方法
thymeleaf介紹
簡單說, Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個極吸引人的特點:
1.Thymeleaf 在有網(wǎng)絡和無網(wǎng)絡的環(huán)境下皆可運行,即它可以讓美工在瀏覽器查看頁面的靜態(tài)效果,也可以讓程序員在服務器查看帶數(shù)據(jù)的動態(tài)頁面效果。這是由于它支持 html 原型,然后在 html 標簽里增加額外的屬性來達到模板+數(shù)據(jù)的展示方式。瀏覽器解釋 html 時會忽略未定義的標簽屬性,所以 thymeleaf 的模板可以靜態(tài)地運行;當有數(shù)據(jù)返回到頁面時,Thymeleaf 標簽會動態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動態(tài)顯示。
2.Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現(xiàn)JSTL、 OGNL表達式效果,避免每天套模板、該jstl、改標簽的困擾。同時開發(fā)人員也可以擴展和創(chuàng)建自定義的方言。
3.Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,可以快速的實現(xiàn)表單綁定、屬性編輯器、國際化等功能。
我們緊接著 上一篇 文章,我們使用賬號 jack 和賬號 Tom 來分別登錄,在上一篇文章測試中可以看到,這兩個賬號無論哪一個登錄,首頁頁面都會顯示 add 頁面和 update 頁面兩個超鏈接,而對于這兩個賬號來說,一個擁有訪問 add 頁面的權限,一個擁有訪問 update 頁面的權限。那么問題來了,如何才能根據(jù)不同用戶的身份角色信息來顯示不同的頁面內(nèi)容呢?這就要使用 shiro 標簽了
thymeleaf 模板引擎使用 shiro 標簽
引入依賴
<dependency>
<groupId>com.github.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會按從上到下的順序匹配驗證,匹配了就不再繼續(xù)驗證
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/layer/**", "anon");// 靜態(tài)資源放行
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/jquery/**", "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" />
</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> 標簽,故而進行了兩次授權操作

到此這篇關于springboot整合shiro之thymeleaf使用shiro標簽的文章就介紹到這了,更多相關springboot整合shiro使用shiro標簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Springboot詳解如何整合使用Thymeleaf
- springboot+thymeleaf整合阿里云OOS對象存儲圖片的實現(xiàn)
- SpringBoot整合thymeleaf 報錯的解決方案
- SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
- Springboot使用thymeleaf動態(tài)模板實現(xiàn)刷新
- 淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
- springboot如何使用thymeleaf模板訪問html頁面
- springboot中thymeleaf模板使用詳解
- SpringBoot?整合Thymeleaf教程及使用方法
相關文章
java如何自定義List中的sort()排序,用于日期排序
這篇文章主要介紹了java如何自定義List中的sort()排序,用于日期排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot中的聲明式事務+切面事務+編程式事務詳解
這篇文章主要介紹了SpringBoot中的聲明式事務+切面事務+編程式事務詳解,事務管理對于企業(yè)應用來說是至關重要的,當出現(xiàn)異常情況時,它也可以保證數(shù)據(jù)的一致性,需要的朋友可以參考下2023-08-08
如何解決HttpServletRequest.getInputStream()多次讀取問題
這篇文章主要介紹了如何解決HttpServletRequest.getInputStream()多次讀取問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
JavaEE開發(fā)基于Eclipse的環(huán)境搭建以及Maven Web App的創(chuàng)建
本文主要介紹了如何在Eclipse中創(chuàng)建的Maven Project,本文是JavaEE開發(fā)的開篇,也是基礎。下面內(nèi)容主要包括了JDK1.8的安裝、JavaEE版本的Eclipse的安裝、Maven的安裝、Tomcat 9.0的配置、Eclipse上的M2Eclipse插件以及STS插件的安裝。2017-03-03
springboot Controller直接返回String類型帶來的亂碼問題及解決
文章介紹了在Spring Boot中,當Controller直接返回String類型時可能出現(xiàn)的亂碼問題,并提供了解決辦法,通過在`application.yaml`中設置請求和響應的編碼格式,并在自定義配置類中進行配置,可以有效解決這一問題2024-11-11
Spring Cache優(yōu)化數(shù)據(jù)庫訪問的項目實踐
本文主要介紹了Spring Cache優(yōu)化數(shù)據(jù)庫訪問的項目實踐,將創(chuàng)建一個簡單的圖書管理應用作為示例,并演示如何通過緩存減少對數(shù)據(jù)庫的頻繁查詢,感興趣的可以了解一下2024-01-01

