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

引言:html

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

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

 

1.缘由记录apache

如下是我报错的缘由:缓存

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

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

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

2. 分析解决maven

如下是解决这个错误的方法:单元测试

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

采用@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); } }

3.架包致使的

架包也有可能致使“no bean named 'shiroFilter' is defined”,个人项目中还有一个缘由致使了“no bean named 'shiroFilter' is defined”的问题。

在我本身搭建的项目中缺乏了一个spring-web的架包(参照:spring各架包的做用),这个架包会致使报“no bean named 'shiroFilter' is defined”,

可是当把web.xml中的shiro过滤器注释掉之后,不在使用shiro,那就对项目其余功能以及使用不产生影响,这是我困惑的地方?在此记录一下,

对于其中的缘由,若是有大神看到此博客,但愿能解答一下!

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

另外,在记录一个场景,留给有缘人,但愿不要像我同样花了两天时间才搞定:

咱们公司有两个项目,两个项目都使用了shiro,并且都是maven项目,有一个项目在pom.xml中有写spring-web的索引,有一个项目中没有写spring-web的

索引,关键是两个项目都还可以正常使用shiro,我就是参照了没有使用spring-web索引的那个项目搭建的系统(运气太背),因此致使了spring没有建立shiroFilter这个

bean的异常。其实,当我再回过去看没有写spring-web索引的那个项目时,在依赖的架包里面是已经存在spring-web的这个架包的,这颇有多是使用maven自动

导包时,有的架包底层依赖了spring-web这个架包,因此在pom.xml没有写spring-web索引时,自动导入了,恰好解决了shiroFilter底层依赖的问题。

 

若是以为有用,记得点赞哦!

相关文章
相关标签/搜索