集成Shiro和WebSocket接口时遇到问题

问题描述

标题所示,当我在Shiro管理的Springboot项目中开发WebSocket时,我需要在套接字接口中获取当前登录用户的信息

public static Subject getSubjct() {
    return SecurityUtils.getSubject();
}

此行中的错误:SecurityUtils.getSubject();

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 com.runtime.system.util.ShiroUtils.getSubjct(ShiroUtils.java:18)
at com.runtime.system.util.ShiroUtils.getUser(ShiroUtils.java:22)

在传统的Api界面中不存在此问题。我感到非常苦恼。请帮助我,谢谢!

解决方法

开箱即用Shiro不知道如何处理正在运行异步请求的线程。

您将需要将数据与异步请求(通常是主题)相关联,然后通过Runnable / Callable执行所需的操作:

Subject subject = <get the subject from your async context>;
subject.execute( <your runnable> );

请参阅:https://shiro.apache.org/subject.html#thread-association