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

学习了很多大神的总结,基本上都是一样的,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

 

以上为自己一点一点总结,有兴趣的小伙伴可以加我微信咱们一起讨论研究哦

关于springboot+shiro+thymeleaf页面级元素的权限控制的问题_第1张图片

你可能感兴趣的:(springboot,problem)