No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.Thread

1 篇文章 0 订阅
1 篇文章 0 订阅

在使用Shiro前提下,开始是想在controller使用SecurityUtils.getSubject().getPrincipal()中获取当前用户信息,但结果报红了

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘studentController’ defined in file
[H:\lxt\target\classes\com\dang\controller\StudentController.class]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.dang.controller.StudentController]: Constructor
threw exception; nested exception is
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. This
is an invalid application configuration.

然后就开始排查呗
能出现这个错误的情况有一下几种

  • 没有在相关shiro类中使用注解@Bean

是spring 的环境中没有把shiro 某些需要的类表示为Bean ,导致Spring无法管理到自然获取不到,这个错误通常在ShiroConfig.java里面。
在这里插入图片描述

  • 自定义Filter要在shiroFilter后面

在这里插入图片描述
但是这些都没有问题,于是就推测是SecurityUtils.getSubject().getPrincipal()使用的场景不对,一开始为了方便直接在类里面定义了一个属性,初始化时就直接使用User user = (User) SecurityUtils.getSubject().getPrincipal();,此时获取不到当前用户信息。
SecurityUtils.getSubject().getPrincipal()只有在方法内,即处理转发用户请求时才会有Shiro.session的作用域,不在方法内获取的Shiro.session并不知道你是哪个用户,方法外并没有他的作用域,也就没有session了。
既然是这样,那就在方法中再定义,结果没有报错,测试也通过了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值