溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么解決springboot+shiro+thymeleaf頁面級元素的權限控制問題

發布時間:2022-01-25 13:43:30 來源:億速云 閱讀:175 作者:iii 欄目:開發技術

今天小編給大家分享一下怎么解決springboot+shiro+thymeleaf頁面級元素的權限控制問題的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

    springboot+shiro+thymeleaf頁面級元素的權限控制

    我用的springboot2.1版本。

    學習了很多大神的總結,基本上都是一樣的,shiro權限框架,前端驗證是為jsp設計的,其中的tag只能用于jsp系列的模板引擎。

    使用了thymeleaf作為前端模板引擎,使用HTML文件,沒法引入shiro的tag lib,此時如果要使用shiro的話,可以引入 thymeleaf-extras-shiro.jar這個拓展包來曲線實現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>
    <h4>用戶添加界面</h4>
    <p shiro:hasRole="admin"> 有權限</p>
    <p shiro:hasRole="test">無權限</p>
    </body>
    </html>

    其他的配置,和shiro用于后臺權限控制的配置一樣,然后就可以了,事實上我也是這樣做的,但是遇到一個問題,

    一直報這個異常

    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這個類,事實上是這個類是有的,只是在編譯的時候能找到,所以沒有異常,在運行的時候找不到這個類,所以就異常了,仔細檢查了好多遍,最后我改了springboot的版本,然后就不報錯了,開始我使用的是最新2.0版本,改到1.5就正常了

    下面貼一下關于shiro用到的包

    希望遇到同樣問題的同學繞過個坑

    <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常見權限控制標簽使用

    1、未認證并且未記住的用戶

    <!-- 當前用戶是否為“游客”,如果是未認證,也未記住的用戶,那么就顯示該 p 標簽中的內容 -->
    <p shiro:guest="">Please <a href="login.html" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >login</a></p>

    2、未認證,已記住的用戶

    <!-- 未認證的用戶但已記住,與下面的 authenticated 標簽相對應。
        與 guest 標簽的區別是,該標簽包含已記住用戶。 -->
    <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、認證通過或已記住的用戶

    <!-- 認證通過或已記住的用戶,則顯示該 p 標簽中的內容 -->
    <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、認證通過,未記住的用戶

    <!-- 已認證通過,但未記住的用戶。這是與 user 標簽的區別所在。 -->
    <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、當前用戶信息

    <!-- 輸出當前用戶信息,通常為登錄帳號信息 -->
    <p>Hello, <shiro:principal />, how are you today?</p>

    6、驗證當前用戶是否具有該角色

    <!-- 驗證當前用戶是否具有該 admin 角色,若擁有,則顯示 a 標簽的內容 -->
    <a shiro:hasRole="admin" href="admin.html" rel="external nofollow" >Administer the system</a>

    7、驗證當前用戶是否不具有該角色

    <!-- 與 hasRole 標簽邏輯相反,當用戶不屬于該 developer 角色時顯示 -->
    <p shiro:lacksRole="developer">
        Sorry, you are not allowed to developer the system.
    </p>

    8、驗證當前用戶是否同時具有以下所有角色

    <!-- 驗證當前用戶是否同時具有以下所有角色 -->
    <p shiro:hasAllRoles="developer, product">
        You are a developer and a admin.
    </p>

    9、驗證當前用戶是否具于以下任意一個角色

    <!-- 驗證當前用戶是否具于以下任意一個角色 -->
    <p shiro:hasAnyRoles="admin, vip, developer">
        You are a admin, vip, or developer.
    </p>

    10、驗證當前用戶是否擁有指定權限

    <!-- 驗證當前用戶是否擁有 update 權限 -->
    <a shiro:hasPermission="update" href="createUser.html" rel="external nofollow" >添加用戶</a>

    11、驗證當前用戶是否不擁有指定權限

    <!-- 與 hasPermission 標簽邏輯相反,當前用戶不擁有指定權限 delete -->
    <p shiro:lacksPermission="delete">
        Sorry, you are not allowed to delete user accounts.
    </p>

    12、驗證當前用戶是否同時擁有以下所有角色

    <!-- 驗證當前用戶是否同時擁有以下所有角色 -->
    <p shiro:hasAllPermissions="add, update">
        You can see or add users.
    </p>

    13、驗證當前用戶是否擁有以下任意一個權限

    <!-- 驗證當前用戶是否擁有以下任意一個權限 -->
    <p shiro:hasAnyPermissions="add, update, delete">
        You can see or delete users.
    </p>

    以上就是“怎么解決springboot+shiro+thymeleaf頁面級元素的權限控制問題”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女