【问题标题】:Spring Security : no bean is named "springSecurityFilterChain" defined errorSpring Security:没有bean被命名为“springSecurityFilterChain”定义错误
【发布时间】:2015-07-06 23:04:55
【问题描述】:

我在 Maven Webapp 项目中设置 Spring Security 时遇到问题。每当我在服务器上运行应用程序时,它都会显示以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

现在我知道这个问题已经被问过了,我从昨天开始尝试了各种解决方案,但没有成功。

请注意,spring-security.xmlspring-database.xml 位于 WEB-INF 目录中名为 spring 的文件夹中,而 applicationContext.xml 在 WEB-INF 目录中。

这是我的配置文件:

web.xml:

<web-app>


<display-name>Archetype Created Web Application</display-name>
<context-param>
    <param-name>contextConfiguration</param-name>
    <param-value>
            /WEB-INF/spring/spring-security.xml
            /WEB-INF/spring/spring-database.xml
            /WEB-INF/applicationContext.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>

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

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

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

spring-security.xml:

<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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">

<http auto-config="true" use-expressions="true">
    <access-denied-handler error-page="/403" />
    <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
    <form-login 
        login-page="/login" 
        default-target-url="/admin" 
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" invalidate-session="true"/>
    <!-- enable csrf protection -->
    <csrf/>
</http>


<authentication-manager>
  <authentication-provider>
    <jdbc-user-service data-source-ref="dataSource"
      users-by-username-query=
        "select login,pwd, enabled from utilisateur where login=?"
      authorities-by-username-query=
        "select login, role from role where login =?" />
  </authentication-provider>
</authentication-manager>

applicationContext.xml:

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/vues/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.myApp.controllers" />

</beans:beans>

spring-database.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/wfmconf" />
        <property name="username" value="root" />
        <property name="password" value="The_Master123456" />
    </bean>

</beans>

编辑:

我已经在我的 web.xml 中使用正确的 URL 指定了 applicationContext 和 spring-security 配置文件作为上下文参数

【问题讨论】:

标签: xml spring maven jakarta-ee spring-security


【解决方案1】:

我发现了我的问题。 context-param 名称应该是 contextConfigLocation 而不是 contextConfiguration 并且 applicationContext.xml 应该被指定为 servlet 的 init-param。

所以正确的 web.xml 应该是:

<web-app>


<display-name>Archetype Created Web Application</display-name>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/spring/spring-security.xml
            /WEB-INF/spring/spring-database.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>

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

<servlet>
    <servlet-name>appServlet</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>1</load-on-startup>
</servlet>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多