使用Shiro进行Spring集成测试会导致UnavailableSecurityManagerException

时间:2015-01-11 04:09:22

标签: spring integration-testing shiro

我正在进行spring webapp的集成,但是shiro无法正常加载。

我的问题与问题org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code类似。但是,它的解决方案不适合我。

在我的web.xml中,Shiro的配置如下:

<filter>
    <filter-name>shiroFilter</>
    <filter-calss>org.springframework.web.filter.DelegatingFilterProxy</>
    <init-param>
        <param-name>targetFilterLifecycle</>
        <param-value>true</>
    </>
</>

我的spring配置applicationContext.xml是这样的:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="..."/>
    <property name="sessionManager" ref="..."/>
</bean>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.shiroFilterFactoryBean">
    <property name="filters">
        <map>
            <entry key="ssl" value-ref="..."/>
            <entry key="login" value-ref="..."/>
        </map>
    </>
    <property name="securityManager" ref="securityManager"/>
    <property .../>
</bean>

在春天的控制器中,我像这样使用Shiro:

@RequestMapping(...)
public String login(){
    ...
    Subject sb = SecurityUtils.getSubject();
    if(sb.getPrincipal()!=null && sb.isAuthenticated()){
        return "";
    }
    return "";
}

它可以在我的网络应用中表现良好。但是,当我和spring进行集成测试时,问题就出现了。我的测试代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHirerchy({
    load the previous applicationContext.xml
})
public class LoginIntegrationTest{

    @Autowired WebApplicationContext wac;

    MockMvc mockmvc;

    @Before
    public void setup(){
        this. mockmvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testLogin(){
        this.mockmvc.perform("/login")
               .andExpect(...);
    }
}

而且,例外是:

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.

似乎shiro没有正确的做法。有没有人有与Shiro集成测试的经验,请告诉我。非常感谢你。

0 个答案:

没有答案
相关问题