• 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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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和浏览开发的页面都没有问题啦!

    最后,我只能感叹一句:初学者,伤不起呀 - -

  • 相关阅读:
    《DSP using MATLAB》 示例 Example 9.12
    《DSP using MATLAB》示例 Example 9.11
    《DSP using MATLAB》示例 Example 9.10
    《DSP using MATLAB》示例Example 9.9
    《DSP using MATLAB》示例 Example 9.8
    《DSP using MATLAB》示例Example 9.7
    《DSP using MATLAB》示例 Example 9.6
    《DSP using MATLAB》示例Example 9.5
    《DSP using MATLAB》示例 Example 9.4
    (转载)【C++11新特性】 nullptr关键字
  • 原文地址:https://www.cnblogs.com/laobiao/p/5636047.html
Copyright © 2020-2023  润新知