Springboot整合Shiro报UnavailableSecurityManagerException异常解决办法

项目是用springboot做脚手架,用shiro做安全框架,今天使用quartz跑定时任务,发现会报这个错误,虽然不影响定时任务执行,但是每次执行定时任务都会抛这个异常。

完整报错日志:

2020-03-03 20:45:42 ERROR Async-Thread5 org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler Unexpected exception occurred invoking async method: public void aoshu.bi.monitor.service.impl.LogServiceImpl.saveLog(org.aspectj.lang.ProceedingJoinPoint,java.lang.reflect.Method,javax.servlet.http.HttpServletRequest,java.lang.String,long)
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)
	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626)
	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
	at aoshu.bi.monitor.service.impl.LogServiceImpl.saveLog(LogServiceImpl.java:82)
	at aoshu.bi.monitor.service.impl.LogServiceImpl$$FastClassBySpringCGLIB$$36ecb067.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.transaction.interceptor.TransactionInterceptor$$Lambda$745/75108401.proceedWithInvocation(Unknown Source)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor$$Lambda$847/1282083659.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)
重点关注这一行

查看Shiro SecurityUtils源码

    public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
        SecurityManager securityManager = ThreadContext.getSecurityManager();
        if (securityManager == null) {
            securityManager = SecurityUtils.securityManager;
        }
        if (securityManager == null) {
            String msg = "No SecurityManager accessible to the calling code, either bound to the " +
                    ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application " +
                    "configuration.";
            throw new UnavailableSecurityManagerException(msg);
        }
        return securityManager;
    }

发现是ThreadContext.getSecurityManager() 的值为空

我的ShiroConfiguration配置类中的SecurityManager bean的配置发现,在配置了SecurityManager之后,没有将其绑定到ThreadContext中。
于是在配置完SecurityManager后、返回该bean之前,添加下面这句就可以实现绑定:

ThreadContext.bind(securityManager);

完整SecurityManager 配置:

@Bean
public SecurityManager securityManager(ShiroRealm shiroRealm) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    // 配置 SecurityManager,并注入 shiroRealm
    securityManager.setRealm(shiroRealm);
    // 配置 shiro session管理器
    securityManager.setSessionManager(sessionManager());
    // 配置 缓存管理类 cacheManager
    securityManager.setCacheManager(cacheManager());
    // 配置 rememberMeCookie
    securityManager.setRememberMeManager(rememberMeManager());

    ThreadContext.bind(securityManager);
    return securityManager;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值