【问题标题】:getting exception: No bean named 'springSecurityFilterChain' is defined出现异常:未定义名为“springSecurityFilterChain”的 bean
【发布时间】:2012-08-20 20:25:39
【问题描述】:

我正在从参考资料中学习弹簧安全性。发布 3.1.2.RELEASE。如前所述,我已经像这样配置了security:http 标签

security-context.xml

<security:http auto-config="true">
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    </security:http>

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:*-context.xml</param-value>
  </context-param>

  <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>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>security</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>security</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

security-servlet.xml

<context:component-scan base-package="com.pokuri.security.mvc.controllers"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

但是当我启动应用程序时,我得到了这个异常。如果我删除安全配置,我的 Spring Web 应用程序工作正常。我在stackoverflow中遇到了同样的问题。但没有运气。

【问题讨论】:

  • 将其添加到root application context 或DispatcherServlet application context。您可以通过扩展 AbstractAnnotationConfigDispatcherServletInitializer 轻松做到这一点。

标签: spring spring-mvc spring-security


【解决方案1】:

我认为您的问题的原因可能是在您启动 Web 应用程序时未加载您用于 Spring Security 的 xml 配置文件。

要解决这个问题,您应该像这样在 web.xml 中指定所有 XML 配置文件:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>

如果您的配置文件在类路径中(不是 WEB-INF 文件夹或其子文件夹),那么您可以通过这种方式指定配置文件列表;

...
<param-value>
    classpath:applicationContext.xml,
    classpath:spitter-security.xml
</param-value>
...

你还需要添加特殊的监听器来加载你的配置文件:

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

【讨论】:

  • 但我在 web.xml 中有上下文参数和监听器配置
  • 是的,这是我的上下文参数的问题。我将参数值作为 classpath*:*-context.xml 给出,这不是获取 security-context.xml 的正确方法。因此将通配符更改为类路径:**/*-context.xml。现在一切正常
  • 注意:我必须将它添加到根应用程序上下文(而不是应用程序 servlet 上下文)。
  • @dimas 我也有类似的问题。你愿意帮我解决这个问题吗?这是链接:stackoverflow.com/questions/22643577/…
【解决方案2】:

我刚刚按照 Spring 的要求在 applicationContext.xml 中添加了 bean 定义:

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>

【讨论】:

    【解决方案3】:

    将此添加到您的 web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.xml</param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
            <!-- filter declaration for 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>
    

    【讨论】:

      【解决方案4】:

      如果它对任何人有帮助,我已经重命名了我的一个包,但 Eclipse 不会自动更新您的 @ComponentScan 路径,因此请确保您也进行更改:

      @ComponentScan(basePackages = "com.package.spring")
      

      【讨论】:

        【解决方案5】:

        从 Spring Security 5.2.1-SNAPSHOT 开始,当我没有在安全 XML 配置中声明 &lt;http/&gt; 元素时,我发生了这个错误。

        我正在尝试一个示例,我有类似的配置

            <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">
        
            <user-service>
                <user name="user" password="{noop}password" authorities="ROLE_USER" />
            </user-service>
        </b:beans>
        

        我必须更改它以添加&lt;http/&gt;,如下所示。

        <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">
        
            <http />
            <user-service>
                <user name="user" password="{noop}password" authorities="ROLE_USER" />
            </user-service>
        </b:beans>
        

        【讨论】:

          猜你喜欢
          • 2011-07-02
          • 1970-01-01
          • 2016-01-02
          • 2015-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多