Junit+Spring MockMvc+Shiro时出现SessionContext和SecurityManager的错误解决方式

坑爹的错误啊,找了好久的解决方法终于找到了
错误原因:
ShiroFIlter在junit,mockmvc环境中没有被加入
修改方法:
在junit中加入before 中添加 ShiroFilterFactoryBean

    @Before
    public void setUp() throws Exception {
        DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(context);
        builder.addFilters((Filter) context.getBean("shiroFilter"));
        mvc = builder.build(); 
    }

shiro配置中的ShiroFilterFactoryBean

    @Bean(name="shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        System.out.println("ShiroConfiguration.shirFilter()");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/assets/**", "anon");
        shiroFilterFactoryBean.setLoginUrl("/");
        shiroFilterFactoryBean.setSuccessUrl("/index.html");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

感写下面的博主提出解决方案,原文分析的十分明了,推荐去看看
Junit+Spring MockMvc+Shiro时出现SessionContext和SecurityManager的错误解决方式

你可能感兴趣的:(java,spring,shiro)