引言:
本人在使用spring集成shiro是總是報“no bean named 'shiroFilter' is defined”,網上的所有方式挨個試了一遍,又檢查了一遍,
還是沒有解決,最后,抱着試試看的心態,采用單元調試的方法終於解決了。
1.原因記錄
以下是我報錯的原因:
報錯部分標記為紅色,也被我注釋掉。
第一個報錯的地方是緩存管理器配置的有問題,第二個報錯的地方是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"> <!–<property name="cacheManager" ref="ehCacheManager"/>–> <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. 分析解決
以下是解決這個錯誤的方法:
網上已有的方法,不在贅述。
采用@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底層依賴的問題。
如果覺得有用,記得點贊哦!