Cause: 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,然后写了一个定时任务,报错,看报错内容不让执行定时任务。未绑定SecurityManager啥的。
解决方法:
看报错是ThreadContext未绑定SecurityManager,那就帮顶下,在定时任务最开始的代码前加上:
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
ThreadContext.bind(manager);
重新启动项目,定时任务正常运行。
带来的问题与优化:
每次定时任务启动就要new一个DefaultWebSecurityManager,这是个问题;优化下,可以将DefaultWebSecurityManager
作为静态变量,或者放到spring容器中。