获得异常:没有定义名为'springSecurityFilterChain'的bean

Pok*_*uri 46 spring spring-mvc spring-security

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

安全的context.xml

<security:http auto-config="true">
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    </security:http>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

安全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>
Run Code Online (Sandbox Code Playgroud)

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

dim*_*mas 65

我认为您的问题的原因可能在于,当您启动Web应用程序时,未加载用于Spring安全性的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>
Run Code Online (Sandbox Code Playgroud)

如果你的配置文件在classpath(不是WEB-INF文件夹或它的子文件夹)中,那么你可以用这种方式指定配置文件列表;

...
<param-value>
    classpath:applicationContext.xml,
    classpath:spitter-security.xml
</param-value>
...
Run Code Online (Sandbox Code Playgroud)

此外,您还需要添加将加载配置文件的特殊侦听器:

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

  • 注意:我必须将它添加到根应用程序上下文(而不是app servlet上下文). (3认同)

Lot*_*ava 11

我刚刚在applicationContext.xml中添加了bean定义,就像Spring问的那样:

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
Run Code Online (Sandbox Code Playgroud)


小智 5

将此添加到您的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>
Run Code Online (Sandbox Code Playgroud)