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

目录
  • springboot+shiro+thymeleaf页面级元素的权限控制
    • 一直报这个异常
    • 下面贴一下关于shiro用到的包
  • 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>
<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>

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

(0)

相关推荐

  • Springboot 整合shiro实现权限控制的方法

    Author:jeffrey Date:2019-04-08 一.开发环境: 1.mysql - 5.7 2.navicat(mysql客户端管理工具) 3.idea 2017.2 4.jdk8 5.tomcat 8.5 6.springboot2.1.3 7.mybatis 3 8.shiro1.4 9.maven3.3.9 二.数据库设计 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CB46ByC1-1604249108144)(img/shiro01.pn

  • SpringBoot集成Shiro进行权限控制和管理的示例

    shiro apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自家的东西.springboot暂时没有集成shiro,这得自己配. 1 . 添加依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId

  • SpringBoot整合Shiro实现权限控制的代码实现

    1.SpringBoot整合Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 1.1.shiro简介 shiro有个核心组件,分别为Subject.SecurityManager和Realms Subject:相当于当前操作的"用户",这个用户不一定是一个具体的人,是一个抽象的概念,表明的是和当前程序进行交互的任何东西,例如爬虫.脚本.等等.所有的Subject都绑定到SecurityManager上,与 Subject 的所

  • SpringBoot中整合Shiro实现权限管理的示例代码

    之前在 SSM 项目中使用过 shiro,发现 shiro 的权限管理做的真不错,但是在 SSM 项目中的配置太繁杂了,于是这次在 SpringBoot 中使用了 shiro,下面一起看看吧 一.简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序. 三个核心组件: 1.Subject 即"当前操作用户".但是,在 Shi

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

    目录 springboot+shiro+thymeleaf页面级元素的权限控制 一直报这个异常 下面贴一下关于shiro用到的包 shiro整合thymeleaf常见权限控制标签使用 springboot+shiro+thymeleaf页面级元素的权限控制 我用的springboot2.1版本. 学习了很多大神的总结,基本上都是一样的,shiro权限框架,前端验证是为jsp设计的,其中的tag只能用于jsp系列的模板引擎. 使用了thymeleaf作为前端模板引擎,使用HTML文件,没法引入sh

  • 解决springboot+shiro 权限拦截失效的问题

    最近因为项目需要,接触了shiro.新手入门 发现权限拦截失效, 一直以为是以为授权和DB的问题 研究了一个下午,终于发现了问题所在 我的访问路径没有写前面的斜杠!!,而DB中的资源路径是可以省略的,崩溃了吧 但是问题来了,为什么在其他地方可以忽略掉前面的小斜杠呢? 经过几分钟的捣鼓发现,在springboot中,不论是thymeleaf的模板也好(我用的thymeleaf),还是后端代码也好,底层会自动补全这个斜杠 问题解决!! 补充知识:SpringBoot整合shiro的一个完整的小案例

  • Springboot+Vue+shiro实现前后端分离、权限控制的示例代码

    本文总结自实习中对项目的重构.原先项目采用Springboot+freemarker模版,开发过程中觉得前端逻辑写的实在恶心,后端Controller层还必须返回Freemarker模版的ModelAndView,逐渐有了前后端分离的想法,由于之前,没有接触过,主要参考的还是网上的一些博客教程等,初步完成了前后端分离,在此记录以备查阅. 一.前后端分离思想 前端从后端剥离,形成一个前端工程,前端只利用Json来和后端进行交互,后端不返回页面,只返回Json数据.前后端之间完全通过public A

  • ASP.NET对HTML页面元素进行权限控制(一)

    一个HTML页面有很多的元素比如<DIV>,<P>等.这些元素构成了HTML页面.在Web开发中权限控制是每个系统都要用到了.界面每个元素的权限也是需要控制的.比如一个查询用户的界面里面有查询用户按钮,添加用户按钮,删除用户按钮,不同的角色我们得分配不同的权限,比如一般用户只有查询用户按钮的权限:管理员有添加用户按钮,查询用户按钮的权限:超级管理员查询用户按钮,添加用户按钮,删除用户按钮的权限.如何让这三种用户在登录以后得到三种不同的界面形式呢?这时候就需要用到HTML元素进行权限

  • spring boot 1.5.4 集成shiro+cas,实现单点登录和权限控制

    1.添加maven依赖(先安装好cas-server-3.5.2,安装步骤请查看本文参考文章) <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>

  • ASP.NET对HTML页面元素进行权限控制(二)

    这是这个权限控制的第一步,扫描界面把要分配权限的元素的信息获取出来存入到数据库中. 这一步分三小步: (1).标出界面所要分配权限的元素 (2).扫描界面获取所要分配权限的元素信息.(ID,标题,层级关系) (3).存入数据库中. 1.标出界面所要分配权限的元素. 在扫描的时候一开始我觉得很难因为HTML元素过多又有很多层级关系.一开始用的是<div>标签来表示HTML所要分配权限的元素,发现这个方案不行,比如把添加用户按钮加上DIV那么这个按钮的样式就变了还得调样式我现在做的KS系统有将近1

  • ASP.NET对HTML页面元素进行权限控制(三)

    上一篇博客中有些没有考虑到的东西这次更改一下代码如下: 界面前台: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdmShowDIV.aspx.cs" Inherits="ExamSystemV3.Manager.RoleManager.AdmShowDIV" %> <!DOCTYPE html> <ht

  • SpringBoot+Spring Security+JWT实现RESTful Api权限控制的方法

    摘要:用spring-boot开发RESTful API非常的方便,在生产环境中,对发布的API增加授权保护是非常必要的.现在我们来看如何利用JWT技术为API增加授权保护,保证只有获得授权的用户才能够访问API. 一:开发一个简单的API 在IDEA开发工具中新建一个maven工程,添加对应的依赖如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-b

  • springboot 在ftl页面上使用shiro标签的实例代码

    1.首先第一步导入依赖 <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>1.2.1</version> </dependency> 2.在配置shiro权限的方法内加入 @Bean public ShiroDialect sh

  • 详解springboot springsecuroty中的注销和权限控制问题

    目录 1账户注销 1.1在SecurityConfig中加入开启注销功能的代码 1.2在index.html添加注销的按钮 1.3启动项目测试 2权限控制 2.1导入springsecurity和thymeleaf的整合依赖 2.2springboot版本降级 2.3引入约束 2.4修改页面代码 2.5重启程序测试 承接:springboot-springsecuroty:用户认证和授权 1 账户注销 1.1 在SecurityConfig中加入开启注销功能的代码 src/main/java/c

随机推荐