解決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)文章
spring一個項目多個模塊聚合打包問題解決方案(最新推薦)
最近遇到個需求,針對后端解耦模塊較多的項目,想在云端啟動時簡潔些只啟動一個jar文件的情景,本文重點給大家介紹spring一個項目多個模塊聚合打包問題解決方案,感興趣的朋友一起看看吧2023-09-09詳解如何為SpringBoot項目中的自定義配置添加IDE支持
這篇文章主要介紹了詳解如何為SpringBoot項目中的自定義配置添加IDE支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Spring Data JPA踩坑記錄(@id @GeneratedValue)
這篇文章主要介紹了Spring Data JPA踩坑記錄(@id @GeneratedValue),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07