spring集成shiro报错解决(no bean named 'shiroFilter' is defined)

引言:

本人在使用spring集成shiro是总是报“no bean named 'shiroFilter' is defined”,网上的所有方式挨个试了一遍,又检查了一遍,

还是没有解决,最后,抱着试试看的心态,采用单元调试的方法终于解决了。

以下是我报错的原因:

报错部分标记为红色,也被我注释掉。

第一个报错的地方是缓存管理器配置的有问题,第二个报错的地方是shiroFilter缺少值。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



    <!--1.配置SecurityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--<property name="cacheManager" ref="cacheManager" />-->
        <property name="realm" ref="shiroRealm" />
        <!--<property name="realms">
            <list>
                <ref bean="shiroRealm"/>
            </list>
        </property>-->
    </bean>

    <!-- 2. 配置缓存管理器 cacheManager -->
<!--    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        &lt;!&ndash;<property name="cacheManager" ref="ehCacheManager"/>&ndash;&gt;
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
    </bean>-->

    <!-- 3. 项目自定义的Realm -->
    <bean id="shiroRealm" class="com.interceptor.ShiroRealm" />
    <!-- 4. Shiro生命周期处理器,可以自动的来调用配置在spring IOC 容器中shiro bean的生命周期方法-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
    <!--5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用.-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

    <!--6. 配置 ShiroFilter.
        6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
        若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
    -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/> <!--安全管理-->
        <property name="loginUrl" value="/"/> <!--登录页面-->
        <property name="successUrl" value="/index.jsp"/> <!--登录成功后页面-->
        <property name="unauthorizedUrl" value="/login/login"/>

        <!--
            配置哪些页面需要受保护.以及访问这些页面需要的权限.
            1). anon 可以被匿名访问
            2). authc 必须认证(即登录)后才可能访问的页面.
            3). logout 登出.
            4). roles 角色过滤器
        -->
        <property name="filterChainDefinitions">
            <value>
                <!--请求登录-->
                /login/login = anon
               <!-- /login.jsp = anon
                /shiro/login = anon
                /shiro/logout = logout

                /user.jsp = roles[user]
                /admin.jsp = roles[admin]-->

                <!--静态资料-->
                /static/login/**    = anon (由于粗心,当时写少了)
                /static/js/**       = anon

                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean>
</beans>

以下是解决这个错误的方法:

网上已有的方法,不在赘述。

采用@Test单元测试去进行精确定位错误

public class MybatisTest {
    /**
     * ClassPathXmlApplicationContext:是ApplicationContext的一个实现类
     */
    private ApplicationContext ac = null;
//    ClassPathXmlApplicationContext ac = null;  

    /**
     * 在所有测试方法之前执行这个标签
     */
    @Before
    public void init() {
        ac = new ClassPathXmlApplicationContext("spring/spring-shiro.xml");
    }

    /**
     * shiro异常调试
     */
    @Test
    public void testShiro(){
//        DefaultWebSecurityManager securityManager = ac.getBean("securityManager", DefaultWebSecurityManager.class);
//        System.out.println(securityManager);
//        LifecycleBeanPostProcessor lifecycleBeanPostProcessor = ac.getBean("lifecycleBeanPostProcessor", LifecycleBeanPostProcessor.class);
//        System.out.println("lifecycleBeanPostProcessor = " + lifecycleBeanPostProcessor);
    /*    ShiroRealm shiroRealm = ac.getBean("shiroRealm", ShiroRealm.class);
        System.out.println("shiroRealm = " + shiroRealm);*/
      /*  ShiroFilterFactoryBean shiroFilter = ac.getBean("shiroFilter", ShiroFilterFactoryBean.class);
        System.out.println("shiroFilter = " + shiroFilter);*/
        System.out.println("ac = " + ac);
    }
}

如果觉得有用,记得点赞哦!

猜你喜欢

转载自www.cnblogs.com/shiyun32/p/9391818.html