0

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

<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>

当我尝试测试它时:

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

    SecurityUtils.getSecurityManager()

}

我收到了这个错误:

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.
4

1 回答 1

3

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

试试这个:

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);

}
于 2013-01-18T01:21:38.717 回答