kinaesthesia
kinaesthesia

Reputation: 703

Shiro standalone application

This is my 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>

When I am trying to test it :

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

    SecurityUtils.getSecurityManager()

}

I got this error :

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.

Upvotes: 0

Views: 5290

Answers (1)

Les Hazlewood
Les Hazlewood

Reputation: 19517

You have to create a Spring environment first before you can reference objects defined in it. This is done automatically for you in Spring web applications, but if you have a standalone app (as indicated above), you have to start Spring yourself.

Try this:

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

}

Upvotes: 3

Related Questions