No bean named 'springSecurityFilterChain' is defined


轉載自 http://blog.csdn.net/yuanzhuohang/article/details/7233752 
 本人按照mkyong.com的example配置spring security3,死活不成功,后來通過查看tomcat日志文件,找到異常信息,搜索到本篇文章,在本文章的提示下解決了spring security3配置的問題

今天配置Spring Security的時候遇到了這樣的問題 No bean named 'springSecurityFilterChain' is defined在我的web.xml中關於springSecurityFilterChain的配置如下

[html]  view plain copy
  1. <!-- spring security 過濾器 -->  
  2. <filter>  
  3.     <filter-name>springSecurityFilterChain</filter-name>  
  4.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>springSecurityFilterChain</filter-name>  
  8.     <url-pattern>/</url-pattern>  
  9. </filter-mapping>  

dispatcher的配置如下

[html]  view plain copy
  1. <servlet>  
  2.     <servlet-name>Dispatcher</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param>  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>  
  7.             /WEB-INF/applicationContext*.xml  
  8.         </param-value>  
  9.     </init-param>  
  10.     <load-on-startup>0</load-on-startup>  
  11. </servlet>  
  12. <servlet-mapping>  
  13.     <servlet-name>Dispatcher</servlet-name>  
  14.     <url-pattern>/</url-pattern>  
  15. </servlet-mapping>  

google了一下找到的解決方案都大同小異

而且有相當多是復制粘貼過來的

對照他們的解決方案,例如這個http://hi.baidu.com/286177943/blog/item/bf362f95d2432f10d21b70fe.html

網上有很多份這樣的拷貝版,但是這種文章只是提出了解決方案,但沒有說明原因,這讓作為初學者的我感到十分蛋疼 - -

1.據我目前所遇到的問題,Spring Security產生包沖突的可能性不大,最多包沖突問題的是slf4j,這個東西折磨了我好一段時間呀。

2.我的applicationContext-security.xml里面已經定義了<http auto-config="true" />標簽,第二種情況排除。

3. 配置文件的文件路徑沒有打錯,相信Spring不會對例如我這種/WEB-INF/applicationContext*.xml使用正則表達式表示的文件路徑解析錯誤吧

4. 我的springSecurityFilterChain沒有寫成SpringSecurityFilterChain


很遺憾,以上的幾種解決方案都不能解決我的問題


繼續google

這個網頁上所提到的解決方案似乎能解決我的問題:http://forum.springsource.org/showthread.php?87265-No-bean-named-springSecurityFilterChain-is-defined

這上面主要說了,要在web.xml里面加上這一段

[html]  view plain copy
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>/WEB-INF/applicationContext-security.xml</param-value>  
  4. </context-param>  

applicationContext-security.xml就是我對於Spring Security的配置項


好啦,這樣一來,啟動tomcat就沒有出錯啦。


但是,但我刷新一下網站的頁面,又報錯了  - -

[plain]  view plain copy
  1. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined  

竟然說找不到id為sessionFactory Bean的定義,我命名就在/WEB-INF/applicationContext-dao.xml中定義了呀 - -

這又是什么原因呀?

不過后來我結合之前使用解決No bean named 'springSecurityFilterChain' is defined方法來想想,一定是在Servlet Dispatcher中所初始化的Bean不能被以外的servlet或者filter使用了。

后來我發現我的web.xml還有這個的定義

[html]  view plain copy
  1. <filter>  
  2.     <filter-name>hibernateFilter</filter-name>  
  3.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  4.     <init-param>  
  5.         <param-name>sessionFactoryBeanName</param-name>  
  6.         <param-value>sessionFactory</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.         <param-name>singleSession</param-name>  
  10.         <param-value>true</param-value>  
  11.     </init-param>  
  12. </filter>  


我想想,定義hibernateFilter是為了實現在Web層對Hibernate Session的延遲加載操作,hibernateFilter肯定需要用到sessionFactory,而sessionFactory按我的配置文件來看,只在Dispatcher內部定義使用。hibernateFilter怎么可能取到在Dispatcher里所定義的Bean呢。是不是和<context-param>、<init-param>有關呢。

再google一下這兩個東西,果然如此,請看http://xy-z487.iteye.com/blog/255198

<init-param>初始化的對象只在servlet內部可以使用。

<context-param>初始化的對象可以提供全局使用。

這樣一來,解決這個問題的方法也出來了。思路就是讓需要全局使用的Bean都能全局使用。

<context-param>改為如下:

[html]  view plain copy
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>/WEB-INF/applicationContext*.xml</param-value>  
  4. </context-param>  

再新建一個Bean配置文件給Dispatcher-servlet.xml獨自使用

[html]  view plain copy
  1. <servlet>  
  2.     <servlet-name>Dispatcher</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param>  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>  
  7.             /WEB-INF/Dispatcher-servlet.xml  
  8.         </param-value>  
  9.     </init-param>  
  10.     <load-on-startup>0</load-on-startup>  
  11. </servlet>  
  12. <servlet-mapping>  
  13.     <servlet-name>Dispatcher</servlet-name>  
  14.     <url-pattern>/</url-pattern>  
  15. </servlet-mapping>  

^_^哈哈,這樣一來,啟動tomcat和瀏覽開發的頁面都沒有問題啦!

最后,我只能感嘆一句:初學者,傷不起呀 - -


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM