springboot整合shiro之thymeleaf使用shiro標(biāo)簽的方法
thymeleaf介紹
簡(jiǎn)單說, Thymeleaf 是一個(gè)跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個(gè)極吸引人的特點(diǎn):
1.Thymeleaf 在有網(wǎng)絡(luò)和無網(wǎng)絡(luò)的環(huán)境下皆可運(yùn)行,即它可以讓美工在瀏覽器查看頁(yè)面的靜態(tài)效果,也可以讓程序員在服務(wù)器查看帶數(shù)據(jù)的動(dòng)態(tài)頁(yè)面效果。這是由于它支持 html 原型,然后在 html 標(biāo)簽里增加額外的屬性來達(dá)到模板+數(shù)據(jù)的展示方式。瀏覽器解釋 html 時(shí)會(huì)忽略未定義的標(biāo)簽屬性,所以 thymeleaf 的模板可以靜態(tài)地運(yùn)行;當(dāng)有數(shù)據(jù)返回到頁(yè)面時(shí),Thymeleaf 標(biāo)簽會(huì)動(dòng)態(tài)地替換掉靜態(tài)內(nèi)容,使頁(yè)面動(dòng)態(tài)顯示。
2.Thymeleaf 開箱即用的特性。它提供標(biāo)準(zhǔn)和spring標(biāo)準(zhǔn)兩種方言,可以直接套用模板實(shí)現(xiàn)JSTL、 OGNL表達(dá)式效果,避免每天套模板、該jstl、改標(biāo)簽的困擾。同時(shí)開發(fā)人員也可以擴(kuò)展和創(chuàng)建自定義的方言。
3.Thymeleaf 提供spring標(biāo)準(zhǔn)方言和一個(gè)與 SpringMVC 完美集成的可選模塊,可以快速的實(shí)現(xiàn)表單綁定、屬性編輯器、國(guó)際化等功能。
我們緊接著 上一篇 文章,我們使用賬號(hào) jack 和賬號(hào) Tom 來分別登錄,在上一篇文章測(cè)試中可以看到,這兩個(gè)賬號(hào)無論哪一個(gè)登錄,首頁(yè)頁(yè)面都會(huì)顯示 add 頁(yè)面和 update 頁(yè)面兩個(gè)超鏈接,而對(duì)于這兩個(gè)賬號(hào)來說,一個(gè)擁有訪問 add 頁(yè)面的權(quán)限,一個(gè)擁有訪問 update 頁(yè)面的權(quán)限。那么問題來了,如何才能根據(jù)不同用戶的身份角色信息來顯示不同的頁(yè)面內(nèi)容呢?這就要使用 shiro 標(biāo)簽了
thymeleaf 模板引擎使用 shiro 標(biāo)簽
引入依賴
<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標(biāo)簽時(shí),要用到
@Bean
public ShiroDialect getShiroDialect() {
return new ShiroDialect();
}
@Bean
public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
// 設(shè)置登錄頁(yè)面url
shiroFilterFactoryBean.setLoginUrl("/user/login");
shiroFilterFactoryBean.setSuccessUrl("/user/index");
shiroFilterFactoryBean.setUnauthorizedUrl("/user/unauthorized");
// 注意此處使用的是LinkedHashMap是有順序的,shiro會(huì)按從上到下的順序匹配驗(yàn)證,匹配了就不再繼續(xù)驗(yàn)證
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/layer/**", "anon");// 靜態(tài)資源放行
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/jquery/**", "anon");
// add.html頁(yè)面放行
filterChainDefinitionMap.put("/user/add", "authc");
// update.html必須認(rèn)證
filterChainDefinitionMap.put("/user/update", "authc");
// index.html必須通過認(rèn)證或者通過記住我登錄的,才可以訪問
filterChainDefinitionMap.put("/user/index", "user");
// 設(shè)置授權(quán),只有user:add權(quán)限的才能請(qǐng)求/user/add這個(gè)url
filterChainDefinitionMap.put("/user/add", "perms[user:add]");
filterChainDefinitionMap.put("/user/update", "perms[user:update]");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
}
index.html 頁(yè)面
- 首先要引入
shiro的命名空間:xmlns:shiro=http://www.pollix.at/thymeleaf/shiro - 使用相應(yīng)的
shiro標(biāo)簽
<!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>首頁(yè)</title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" />
</head>
<body>
<h1>首頁(yè)</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 標(biāo)簽說明
https://shiro.apache.org/jsp-tag-library.html
https://github.com/apache/shiro/blob/main/web/src/main/resources/META-INF/shiro.tld
| 標(biāo)簽 | 含義 |
|---|---|
| shiro:principal | 當(dāng)前用戶的登錄信息,用戶名之類 |
| shiro:guest="" | 驗(yàn)證是否是游客,即未認(rèn)證的用戶 |
| shiro:user="" | 驗(yàn)證是否是已認(rèn)證或已記住用戶 |
| shiro:authenticated="" | 驗(yàn)證是否是已認(rèn)證用戶,不包括已記住用戶 |
| shiro:notAuthenticated= “” | 未認(rèn)證用戶,但是 已記住用戶 |
| shiro:lacksRole=“admin” | 表示沒有 admin 角色的用戶 |
| shiro:hasAllRoles=“admin, user1” | 表示需要同時(shí)擁有兩種角色 |
| shiro:hasAnyRoles=“admin, user1” | 表示 擁有其中一個(gè)角色即可 |
| shiro:lacksPermission=“admin:delete” | 類似于 shiro:lacksRole |
| shiro:hasAllPermissions=“admin:delete, admin:edit” | 類似于 shiro:hasAllRoles |
| shiro:hasAnyPermission=“admin:delete, admin:edit” | 類似于 hasAnyRoles |
測(cè)試
測(cè)試一
首先使用賬號(hào) jack 來登錄,查看首頁(yè)頁(yè)面,如下

再看控制臺(tái)日志,如下,注意賬號(hào) jack 擁有的權(quán)限,在 index.html 頁(yè)面有兩個(gè) <shiro:hasPermission> 標(biāo)簽,故而進(jìn)行了兩次授權(quán)操作

測(cè)試二
再使用賬號(hào) Tom 來登錄,查看首頁(yè)頁(yè)面,如下

再看控制臺(tái)日志,如下,注意賬號(hào) Tom 擁有的權(quán)限,在 index.html 頁(yè)面有兩個(gè) <shiro:hasPermission> 標(biāo)簽,故而進(jìn)行了兩次授權(quán)操作

到此這篇關(guān)于springboot整合shiro之thymeleaf使用shiro標(biāo)簽的文章就介紹到這了,更多相關(guān)springboot整合shiro使用shiro標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot詳解如何整合使用Thymeleaf
- springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn)
- SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案
- SpringBoot使用thymeleaf實(shí)現(xiàn)一個(gè)前端表格方法詳解
- Springboot使用thymeleaf動(dòng)態(tài)模板實(shí)現(xiàn)刷新
- 淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
- springboot如何使用thymeleaf模板訪問html頁(yè)面
- springboot中thymeleaf模板使用詳解
- SpringBoot?整合Thymeleaf教程及使用方法
相關(guān)文章
如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps
這兩天因?yàn)轫?xiàng)目需要整合spring、struts2、mybatis三大框架,但啟動(dòng)的時(shí)候總出現(xiàn)這個(gè)錯(cuò)誤,困擾我好久,折騰了好久終于找到問題根源,下面小編給大家分享下問題所在及解決辦法,一起看看吧2016-12-12
如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404
在本篇文章里小編給大家整理的是一篇關(guān)于如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404相關(guān)文章,需要的朋友們學(xué)習(xí)下。2019-11-11
Java數(shù)據(jù)結(jié)構(gòu)徹底理解關(guān)于KMP算法
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)關(guān)于KMP算法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
MyBatis批量插入(insert)數(shù)據(jù)操作
本文給大家分享MyBatis批量插入(insert)數(shù)據(jù)操作知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
Springboot配置Swagger2登錄密碼的實(shí)現(xiàn)
本文主要介紹了Springboot配置Swagger2登錄密碼的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot?Security權(quán)限控制自定義failureHandler實(shí)例
這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

