【问题标题】:Error - No bean named 'springSecurityFilterChain' available错误 - 没有名为“springSecurityFilterChain”的 bean 可用
【发布时间】:2019-10-20 19:21:15
【问题描述】:

当我运行一个项目时,我得到了这个错误。我有点正确地写了所有东西,但是有一天我无法修复我没有正确编写的错误。项目根本没有启动,这个错误总是出现。我使用netbeans和tomcat。请帮帮我 enter image description here

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <error-page>

        <location>/errors</location>
    </error-page>


    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>






<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>




        <!-- Loads Spring Security config file -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

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






</web-app>

Spring-Security.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
    xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://www.springframework.org/schema/security
                     http://www.springframework.org/schema/security/spring-security-4.2.xsd">

    <http auto-config="true">
        <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />

        <!-- user-defined login form redirection -->
        <form-login login-page="/login" default-target-url="/" authentication-failure-url="/login?error" />

        <!-- logout url -->
        <logout logout-success-url="/login?logout" />

        <!-- csrf disabled -->
        <csrf disabled="true" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="1234" authorities="ROLE_ADMIN" />                
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

MVC-Dispatcher-Servlet.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="adil.java.schoolmaven" />

    <!-- Resolves Views Selected For Rendering by @Controllers to *.jsp Resources in the /WEB-INF/ Folder -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    欢迎来到 Stack Overflow。 :)

    您的 web.xml 中似乎缺少 ContextLoaderListener。

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

    另外,我认为你必须为你的 spring 应用程序上下文加载多个文件,mvc-dispatcher-servlet.xmlspring-security.xml

    有多种方法可以实现这一目标。您可以在 web.xml 配置中指定多个路径,也可以将 spring-security 导入 mvc-despatcher-servlet.xml。 更多信息请参考以下链接:

    https://www.baeldung.com/spring-web-contexts

    Splitting applicationContext to multiple files

    如果这对您没有帮助,请尝试在 stackoverflow 中搜索“No bean springSecurityFilterChain”,您可能会找到很多解决方案。

    【讨论】:

      【解决方案2】:

      您需要在web.xml 中指定ContextLoaderListener

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

      【讨论】:

        猜你喜欢
        • 2018-11-16
        • 2017-12-16
        • 2016-12-13
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        相关资源
        最近更新 更多