gpt4 book ai didi

java - Apache Shiro 1.4.0 初始化

转载 作者:行者123 更新时间:2023-12-01 09:07:33 25 4
gpt4 key购买 nike

我安装了 Apache Shiro 1.4.0 并关注了 this official tutorial为了设置它。

当我尝试初始化时 SecurityUtilsSecurityManager使用教程中的此代码:

Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);

我收到一条消息 IniSecurityManagerFactory现在不推荐使用 Shiro 的 Environment .

我找不到任何说明如何使用 Environment 初始化 Shiro 的教程,它的文档并没有多大帮助:

An Environment instance encapsulates all of the objects that Shiro requires to function. It is essentially a 'meta' object from which all Shiro components can be obtained for an application.

An Environment instance is usually created as a result of parsing a Shiro configuration file. The environment instance can be stored in any place the application deems necessary, and from it, can retrieve any of Shiro's components that might be necessary in implementing security behavior.

For example, the most obvious component accessible via an Environment instance is the application's securityManager.



那么,我该如何使用这种新的初始化机制呢?

最佳答案

从 Shiro 1.5 开始,现在有 BasicIniEnvironment .它的 Javadoc建议创建 SecurityManager像这样:

Environment env = new BasicIniEnvironment("classpath:shiro.ini");
SecurityManager securityManager = env.getSecurityManager();

然后您可以继续:
SecurityUtils.setSecurityManager(securityManager);

话虽如此,我认为在标准 Web 应用程序中使用 Shiro 时,我认为不应该自己这样做,而是配置 EnvironmentLoaderListenerweb.xml文件:
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>

根据 Javadoc这将使用 EnvrionmentLoader并从 shiro.ini 加载配置通过查看以下位置:
  • /WEB-INF/shiro.ini
  • 类路径:shiro.ini

  • 因此可以简单地输入 shiro.ini在类路径上,添加 Shiro 将自行选择配置。

    关于java - Apache Shiro 1.4.0 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45265622/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com