iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决springboot+shiro+thymeleaf页面级元素的权限控制问题
  • 668
分享到

解决springboot+shiro+thymeleaf页面级元素的权限控制问题

2024-04-02 19:04:59 668人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot+shiro+thymeleaf页面级元素的权限控制一直报这个异常下面贴一下关于shiro用到的包shiro整合thymeleaf常见权限控制标签使用spri

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>
<h3>用户添加界面</h3>
<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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解决springboot+shiro+thymeleaf页面级元素的权限控制问题

本文链接: https://www.lsjlt.com/news/163697.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作