错误描述
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.
我在做一个springboot的多模块开发工程,在登录与权限认证中使用shiro框架却屡屡报错UnavailableSecurityManagerException。我很确保我有将SecurityManager用@Bean注入到容器中,@Configuration也有写,实在不明白发生了什么错误。
分析问题
在我耐心排错下,用Debug一个一个地找,终于发现,springboot项目居然没有扫描到@Configuration!因此没有初始化shrio配置类!究竟为什么会这样呢?@SpringBootApplication标签会扫描其包下包括子包下的所有注解,而我的登录模块没有放到同名包下,因此没有扫描到。比如说,我的springboot启动类位于com.transportation包下,那该包下的所有类的注解都能被扫描到,其他的模块的也可以扫描到,然而我的登录模块的文件没有放到com.transportation包下,就没有被扫描到。
解决问题
- 方法一:
在登录模块中新建目录com.transportation,将所有的文件放到该包下(推荐做法) - 方法二:
@ComponentScan注解声明要扫描指定的包,需要注意的是,该注解与@SpringBootApplication会冲突,也就是说,会以@ComponentScan为准,不再扫描springboot启动类下的其他文件。当然,可以指定多个包的。