Shiro错误之No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util

提示: 

No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.

意思就是:你还没有登录,所以我不能给你提供当前登录人的信息

具体错误信息如下:

20:43:04.495 ERROR org.apache.juli.logging.DirectJDKLog 175 log - Servlet.service() for servlet [dispatcherServlet] threw exception 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.
	at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) ~[shiro-core-1.4.2.jar:1.4.2]
	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626) ~[shiro-core-1.4.2.jar:1.4.2]
	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56) ~[shiro-core-1.4.2.jar:1.4.2]

分析流程:

找到抛出错误的位置:at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) 

SecurityUtils

debug发现当前线程没有创建SecurityManager,所以问题是:请求进来了,但是SecurityManager没初始化。

为什么没有初始化?因为没有正确登录。

只有在正确的调用这个登录方法后subject.login(usernamePasswordToken),才会存在对应的主体信息。

所以需要排除调用 /login接口之前,从shiro调用SecurityUtils.getSubject()获取主体信息

这里查看报错代码,自己写的代码报错的位置:com.fast.admin.interceptor.LoginInterceptor.preHandle(LoginInterceptor.java:37

Caused by: 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.
	at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) ~[shiro-core-1.4.2.jar:1.4.2]
	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626) ~[shiro-core-1.4.2.jar:1.4.2]
	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56) ~[shiro-core-1.4.2.jar:1.4.2]
	at com.fast.framework.utils.ShiroUtil.getSubjct(ShiroUtil.java:20) ~[classes/:?]
	at com.fast.framework.utils.ShiroUtil.getUser(ShiroUtil.java:33) ~[classes/:?]
	at com.fast.admin.interceptor.LoginInterceptor.preHandle(LoginInterceptor.java:37) ~[classes/:?]

在这个地方避免调用登录接口之前调用SecurityUtils.getSubject(),修复后解决问题。

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值