Shiro用户鉴权框架 子线程获取不到用户信息问题解决

报错:

ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - 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)
	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626)
	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)

解决:

// 当前登录用户信息
Subject subject = SecurityUtils.getSubject();
Runnable work = new Runnable() {
    @Override
    public void run() {
        System.out.println("用户信息:" + JSON.toJSONString(ShiroUtils.getUserEntity()));
    }
};
// 与子线程关联
work = subject.associateWith(work);
Executor executor = Executors.newCachedThreadPool();
// 执行
executor.execute(work);
posted @ 2022-05-06 11:28  勇不停歇  阅读(240)  评论(0)    收藏  举报