使用shiro-spring-boot-web-starter报错bean named 'authorizer' that could not be found

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34741165/article/details/83720666

本人在使用spring boot  + shiro时,为了省时间,懒得写shiro的配置bean,就依赖了shiro-spring-boot-web-starter,结果出了以下问题:

使用的依赖包为

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-web-starter</artifactId>
    <version>1.4.0</version>
</dependency>

控制台日志输出的报错位置如下:

由于底层会调用spring的ConfigurationClassEnhancer$BeanMethodInterceptor拦截器进行判断是否有bean名称为:authorizer的bean,但是又找不到这个bean对象,所以报出了,bean对象没有定义。

那又是为什么会出现这种情况呢?一切都是因为以下注解引起的

很多人很奇怪,为什么是这个注解引起的,它只是表示说,如果没有Authorizer的实现对象时,走此方法实现一个Authorizer的对象,bean对象名称为:authorizer。但是由于我们在使用shiro时,一般都会去实现一个Realm对象,并且一般都是去继承于虚拟类AuthorizingRealm。AuthorizingRealm这个虚拟类正好实现了接口Authorizer,说到这边,大家伙应该明白是什么问题了吧?

spring boot在启动的时候,发现已经有一个Authorizer的实现bean对象了。就没有去走这个方法了,也就没有了bean名称为authorizer的对象了。

说了那么多,笔者有两个解决方案,

方案1:自己实现一个SessionsSecurityManager对象,这样就不会走默认的SessionsSecurityManager实现了。

方案2:给你的Realm对象,也就是继承于AuthorizingRealm(或者是其他的实现了接口Authorizer)的对象,命名为:authorizer。

例如:

如有错误,请留言指出,笔者万分感谢。

猜你喜欢

转载自blog.csdn.net/qq_34741165/article/details/83720666