如何使用 "SecurityUtils.getSubject()" 修复 "org.apache.shiro.UnavailableSecurityManagerException" 错误

How to fix "org.apache.shiro.UnavailableSecurityManagerException" error with "SecurityUtils.getSubject()"

  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)

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

1.My 连接问题 getSessionId() enter image description here

2.getSessionId() 这是公司 API 的一个功能。在这个函数中我调用 org.apache.shiro.SecurityUtils.getSubject() enter link 描述在这里

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

    4.It 是 shiro 站点 enter link description here

  2. 上的小说明和详细说明