解决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中加入如下依赖:


   com.github.theborakompanioni
   thymeleaf-extras-shiro
   1.2.1

然后在配置shiro的configuration时,加入

@Bean
public ShiroDialect shiroDialect() {
   return new ShiroDialect();
}

然后在需要控制的页面元素的页面头上加上xmlns:




    
    Add


用户添加界面

有权限

无权限

其他的配置,和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用到的包

希望遇到同样问题的同学绕过个坑


   org.apache.shiro
   shiro-core
   1.3.2


   org.apache.shiro
   shiro-spring
   1.4.0


   org.apache.shiro
   shiro-ehcache
   1.2.5


   com.github.theborakompanioni
   thymeleaf-extras-shiro
   1.2.1

shiro整合thymeleaf常见权限控制标签使用

1、未认证并且未记住的用户


Please login

2、未认证,已记住的用户


    Please login in order to update your credit card information.

3、认证通过或已记住的用户


    Welcome back John! Not John? Click here to login.

4、认证通过,未记住的用户


    Hello, , how are you today?

Update your contact information

5、当前用户信息


Hello, , how are you today?

6、验证当前用户是否具有该角色

7、验证当前用户是否不具有该角色


    Sorry, you are not allowed to developer the system.

8、验证当前用户是否同时具有以下所有角色


    You are a developer and a admin.

9、验证当前用户是否具于以下任意一个角色


    You are a admin, vip, or developer.

10、验证当前用户是否拥有指定权限

11、验证当前用户是否不拥有指定权限


    Sorry, you are not allowed to delete user accounts.

12、验证当前用户是否同时拥有以下所有角色


    You can see or add users.

13、验证当前用户是否拥有以下任意一个权限


    You can see or delete users.

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

你可能感兴趣的:(解决springboot+shiro+thymeleaf页面级元素的权限控制问题)