轉載自 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的配置如下
- <!-- spring security 過濾器 -->
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/</url-pattern>
- </filter-mapping>
dispatcher的配置如下
- <servlet>
- <servlet-name>Dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/applicationContext*.xml
- </param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </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里面加上這一段
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext-security.xml</param-value>
- </context-param>
applicationContext-security.xml就是我對於Spring Security的配置項
好啦,這樣一來,啟動tomcat就沒有出錯啦。
但是,但我刷新一下網站的頁面,又報錯了 - -
- 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還有這個的定義
- <filter>
- <filter-name>hibernateFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>sessionFactoryBeanName</param-name>
- <param-value>sessionFactory</param-value>
- </init-param>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </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>改為如下:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext*.xml</param-value>
- </context-param>
再新建一個Bean配置文件給Dispatcher-servlet.xml獨自使用
- <servlet>
- <servlet-name>Dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/Dispatcher-servlet.xml
- </param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
^_^哈哈,這樣一來,啟動tomcat和瀏覽開發的頁面都沒有問題啦!
最后,我只能感嘆一句:初學者,傷不起呀 - -