【问题标题】:How to fix "org.apache.shiro.UnavailableSecurityManagerException" error with "SecurityUtils.getSubject()"如何使用“SecurityUtils.getSubject()”修复“org.apache.shiro.UnavailableSecurityManagerException”错误
【发布时间】:2019-10-03 14:35:54
【问题描述】:
  1. 我尝试从位于某个 URL 的网络服务器读取数据。

  2. 在我们公司,我使用电子商务 API,它处理来自网络服务器的数据。

  3. 首先要检索数据,我需要从网络服务器创建数据池。

  4. 要创建数据池,我需要配置连接。

  5. 步骤“我需要配置连接”的一部分是函数 getSession(),它使用 Shiro api(org.apache.shiro.SecurityUtils) 所以每次当我尝试与网络服务器建立连接并使用来自网络服务器的数据时,我都会成为异常“线程“主”中的异常 org.apache.shiro.UnavailableSecurityManagerException:调用代码也无法访问 SecurityManager绑定到 org.apache.shiro.util.ThreadContext 或作为 vm 静态单例。这是无效的应用程序配置。"

在写这个问题之前,我尝试查看日志并阅读关于类和问题的描述。

这一切都在 Windows 10、Java 8、Payara Server(Glassfish) 上运行,带有 EJBAPI 和一些电子商务 API。

我使用的导入

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

ContentConfiguration conf = new ContentConfiguration(
                getSessionId(), 
                Constant.ENTITYMODELL, 
                Constant.EMPTY, 
                context);
protected static Session getSession()
{
    Subject subject = SecurityUtils.getSubject();
    if(subject.isAuthenticated())
        return subject.getSession();
    else
        return null;
}

错误信息

Exception in thread "main" 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:627)
    at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
    at de.kolb.demo.data.ServiceLocator.getSessionId(ServiceLocator.java:15)
    at de.kolb.demo.logic.CommonTest.getCommonData(CommonTest.java:32)
    at de.kolb.demo.presentation.ContentDirector.main(ContentDirector.java:34)

【问题讨论】:

  • 这是测试代码吗? (我在您的堆栈跟踪中看到CommonTest)如果没有,您是否有异常显示来自网络请求的完整跟踪? (或者这是在它自己的线程上分离出来的?)

标签: java glassfish ejb shiro payara


【解决方案1】:

我想得到代表我公司情况的答案,但有很多原则,我将描述与 Shiro hub 中的常见问题相关的问题。

1.我的问题连接到getSessionId()enter image description here

2.getSessionId() 这是来自公司 API 的函数。在这个函数中我调用org.apache.shiro.SecurityUtils.getSubject()enter link description here

  1. 此时我想到了异常消息No SecurityManager accessible to the calling code...。比我在这里查看 Shiro 文档enter link description here。在那里我发现,每次我使用使用身份验证和自动化的应用程序时,我都需要使用“Realm”对象配置“SecurityManager”。

    4. shiro网站enter link description here有一个小说明和详细说明

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2021-05-29
    • 2019-05-26
    相关资源
    最近更新 更多