在使用SpringBoot集成shiro时,遇到过一个问题,在启动时,shiroFilter中使用的bean都报WARN异常:
其中shiroFilter使用一般的配置:
shiroDbRealm使用@autowired
注入了AdminService
,它使用Spring AOP实现了动态数据源切换,根据WARN日志提示,该bean不能被aop增强,测试发现动态数据源确实不成功切换。
这是为什么呢?
搜索到答案是,ShiroFilterFactoryBean
实现了BeanPostProcessor
,而Spring AOP是用BeanPostProcessor
实现的,所以其他BeanPostProcessor
及其引用的bean都不能被aop增强。
Classes that implement the
BeanPostProcessor
interface are special, and so they are treated differently by the container. AllBeanPostProcessors
and their directly referenced beans will be instantiated on startup, as part of the special startup phase of theApplicationContext
, then all thoseBeanPostProcessors
will be registered in a sorted fashion - and applied to all further beans. Since AOP auto-proxying is implemented as aBeanPostProcessor
itself, noBeanPostProcessors
or directly referenced beans are eligible for auto-proxying (and thus will not have aspects ‘woven’ into them.
For any such bean, you should see an info log message: “Bean ‘foo’ is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)”.
但是realm中一定要使用数据库查询进行认证和鉴权,我这里改成了使用一个实现了ApplicationContextAware
的SpringContextUtil
中拿。
参考:
Tracking down cause of Spring’s “not eligible for auto-proxying”