解決springboot+shiro+thymeleaf頁面級元素的權(quán)限控制問題
springboot+shiro+thymeleaf頁面級元素的權(quán)限控制
我用的springboot2.1版本。
學(xué)習(xí)了很多大神的總結(jié),基本上都是一樣的,shiro權(quán)限框架,前端驗證是為jsp設(shè)計的,其中的tag只能用于jsp系列的模板引擎。
使用了thymeleaf作為前端模板引擎,使用HTML文件,沒法引入shiro的tag lib,此時如果要使用shiro的話,可以引入 thymeleaf-extras-shiro.jar這個拓展包來曲線實現(xiàn)shiro的前端驗證。
在pom.xml中加入如下依賴:
<dependency> ? ?<groupId>com.github.theborakompanioni</groupId> ? ?<artifactId>thymeleaf-extras-shiro</artifactId> ? ?<version>1.2.1</version> </dependency>
然后在配置shiro的configuration時,加入
@Bean
public ShiroDialect shiroDialect() {
? ?return new ShiroDialect();
}然后在需要控制的頁面元素的頁面頭上加上xmlns:
<!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>Add</title> </head> <body> <h3>用戶添加界面</h3> <p shiro:hasRole="admin"> 有權(quán)限</p> <p shiro:hasRole="test">無權(quán)限</p> </body> </html>
其他的配置,和shiro用于后臺權(quán)限控制的配置一樣,然后就可以了,事實上我也是這樣做的,但是遇到一個問題,
一直報這個異常
org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'shiroDialect' defined in class path resource[com/gaox/config/ShiroConfig.class]: Bean instantiation via factory methodfailed; nested exception is org.springframework.beans.BeanInstantiationException:Failed to instantiate [at.pollux.thymeleaf.shiro.dialect.ShiroDialect]: Factorymethod 'shiroDialect' threw exception; nested exception isjava.lang.NoClassDefFoundError:org/thymeleaf/processor/attr/AbstractTextChildModifierAttrProcessor
atorg.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
atorg.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atorg.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atorg.springframework.boot.SpringApplication.run(SpringApplication.java:327)[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1255)[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1243)[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
atcom.gaox.ShiroDemoApplication.main(ShiroDemoApplication.java:10) [classes/:na]
這個異常是說找不到AbstractTextChildModifierAttrProcessor這個類,事實上是這個類是有的,只是在編譯的時候能找到,所以沒有異常,在運行的時候找不到這個類,所以就異常了,仔細(xì)檢查了好多遍,最后我改了springboot的版本,然后就不報錯了,開始我使用的是最新2.0版本,改到1.5就正常了
下面貼一下關(guān)于shiro用到的包
希望遇到同樣問題的同學(xué)繞過個坑
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>1.2.1</version> </dependency>
shiro整合thymeleaf常見權(quán)限控制標(biāo)簽使用
1、未認(rèn)證并且未記住的用戶
<!-- 當(dāng)前用戶是否為“游客”,如果是未認(rèn)證,也未記住的用戶,那么就顯示該 p 標(biāo)簽中的內(nèi)容 --> <p shiro:guest="">Please <a href="login.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >login</a></p>
2、未認(rèn)證,已記住的用戶
<!-- 未認(rèn)證的用戶但已記住,與下面的 authenticated 標(biāo)簽相對應(yīng)。 ?? ?與 guest 標(biāo)簽的區(qū)別是,該標(biāo)簽包含已記住用戶。 --> <p shiro:notAuthenticated=""> ? ? Please <a href="login.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >login</a> in order to update your credit card information. </p>
3、認(rèn)證通過或已記住的用戶
<!-- 認(rèn)證通過或已記住的用戶,則顯示該 p 標(biāo)簽中的內(nèi)容 --> <p shiro:user=""> ? ? Welcome back John! Not John? Click <a href="login.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >here</a> to login. </p>
4、認(rèn)證通過,未記住的用戶
<!-- 已認(rèn)證通過,但未記住的用戶。這是與 user 標(biāo)簽的區(qū)別所在。 --> <p shiro:authenticated=""> ? ? Hello, <span shiro:principal=""></span>, how are you today? </p> <a shiro:authenticated="" href="updateAccount.html" rel="external nofollow" >Update your contact information</a>
5、當(dāng)前用戶信息
<!-- 輸出當(dāng)前用戶信息,通常為登錄帳號信息 --> <p>Hello, <shiro:principal />, how are you today?</p>
6、驗證當(dāng)前用戶是否具有該角色
<!-- 驗證當(dāng)前用戶是否具有該 admin 角色,若擁有,則顯示 a 標(biāo)簽的內(nèi)容 --> <a shiro:hasRole="admin" href="admin.html" rel="external nofollow" >Administer the system</a>
7、驗證當(dāng)前用戶是否不具有該角色
<!-- 與 hasRole 標(biāo)簽邏輯相反,當(dāng)用戶不屬于該 developer 角色時顯示 --> <p shiro:lacksRole="developer"> ? ? Sorry, you are not allowed to developer the system. </p>
8、驗證當(dāng)前用戶是否同時具有以下所有角色
<!-- 驗證當(dāng)前用戶是否同時具有以下所有角色 --> <p shiro:hasAllRoles="developer, product"> ? ? You are a developer and a admin. </p>
9、驗證當(dāng)前用戶是否具于以下任意一個角色
<!-- 驗證當(dāng)前用戶是否具于以下任意一個角色 --> <p shiro:hasAnyRoles="admin, vip, developer"> ? ? You are a admin, vip, or developer. </p>
10、驗證當(dāng)前用戶是否擁有指定權(quán)限
<!-- 驗證當(dāng)前用戶是否擁有 update 權(quán)限 --> <a shiro:hasPermission="update" href="createUser.html" rel="external nofollow" >添加用戶</a>
11、驗證當(dāng)前用戶是否不擁有指定權(quán)限
<!-- 與 hasPermission 標(biāo)簽邏輯相反,當(dāng)前用戶不擁有指定權(quán)限 delete --> <p shiro:lacksPermission="delete"> ? ? Sorry, you are not allowed to delete user accounts. </p>
12、驗證當(dāng)前用戶是否同時擁有以下所有角色
<!-- 驗證當(dāng)前用戶是否同時擁有以下所有角色 --> <p shiro:hasAllPermissions="add, update"> ? ? You can see or add users. </p>
13、驗證當(dāng)前用戶是否擁有以下任意一個權(quán)限
<!-- 驗證當(dāng)前用戶是否擁有以下任意一個權(quán)限 --> <p shiro:hasAnyPermissions="add, update, delete"> ? ? You can see or delete users. </p>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java gRPC攔截器簡單實現(xiàn)分布式日志鏈路追蹤器過程詳解
有請求的發(fā)送、處理,當(dāng)然就會有攔截器的需求,例如在服務(wù)端通過攔截器統(tǒng)一進(jìn)行請求認(rèn)證等操作,這些就需要攔截器來完成,今天松哥先和小伙伴們來聊一聊gRPC中攔截器的基本用法,后面我再整一篇文章和小伙伴們做一個基于攔截器實現(xiàn)的JWT認(rèn)證的gRPC2023-03-03
PipedWriter和PipedReader源碼分析_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了PipedWriter和PipedReader源碼分析_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下2017-05-05
淺談String類型等值比較引起的“==”、“equals()”和“hashCode”思考
這篇文章主要介紹了淺談String類型等值比較引起的“==”、“equals()”和“hashCode”思考。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
原因分析IDEA導(dǎo)入Spring-kafka項目Gradle編譯失敗
這篇文章主要為大家介紹分析了IDEA導(dǎo)入Spring-kafka項目Gradle中編譯失敗原因及解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
java實現(xiàn)兩張圖片2D翻轉(zhuǎn)動畫效果
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)兩張圖片2D翻轉(zhuǎn)動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Elasticsearch查詢Range Query語法示例
這篇文章主要為大家介紹了Elasticsearch查詢Range Query語法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

