websocket - 集成 Shiro 和 WebSocket 接口时遇到的问题
问题描述
如题,我在Shiro管理的Springboot项目中开发WebSocket时,需要在socket接口中获取当前登录用户的信息
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> );
推荐阅读
- javascript - window.open with _blank 在 Firefox 中打开两个标签页
- javascript - 如果参数是另一个函数的返回值,该函数将不起作用
- linux - 使用 grep 精确匹配单词
- r - 将数据框列名更改为小写时使用 set_names 与 mutate(colnames)
- android - DataSnapShot 对象的值返回 0 而不是实际值
- azure - Azure 容器注册表 - 删除除 2 之外的所有映像
- python-3.x - 使用元组对实例的“装饰”列表进行排序 - Zelle Ex。11.9
- ubuntu - Unable to Run Amethyst Starter Template: 'VkError(ERROR_INCOMPATIBLE_DRIVER)'
- macos - SceneKit 在 macOS Catalyst 应用程序中是否可以继续工作?
- android - android design library 28.0.0 causes app to crash