引言: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"> <!–<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. 分析解決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底層依賴的問題。
若是以爲有用,記得點贊哦!