首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shiro独立应用程序

Shiro独立应用程序
EN

Stack Overflow用户
提问于 2013-01-14 17:25:00
回答 1查看 5.3K关注 0票数 0

这是我的META-INF/spring/beans.xml

代码语言:javascript
复制
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager" />

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
    <property name="arguments" ref="securityManager"/>
</bean>

当我尝试测试它时:

代码语言:javascript
复制
public static void main(String[] args) throws Exception {

    SecurityUtils.getSecurityManager()

}

我得到了这个错误:

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-18 09:21:39

您必须先创建一个Spring环境,然后才能引用其中定义的对象。这在Spring web应用程序中是自动完成的,但是如果你有一个独立的应用程序(如上所述),你必须自己启动Spring。

试试这个:

代码语言:javascript
复制
import org.apache.shiro.mgt.SecurityManager;
...

public static void main(String[] args) throws Exception {

    String resource = "/META-INF/spring/beans.xml";

    ClassPathXmlApplicationContext appCtx = 
        new ClassPathXmlApplicationContext(resource);

    SecurityManager securityManager = 
        (SecurityManager)appCtx.getBean("securityManager");

    SecurityUtils.setSecurityManager(securityManager);

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14315824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档