当前位置: 首页 > 知识库问答 >
问题:

Spring安全 - 组织.Spring框架.豆子.工厂.豆创建异常: 创建名称为“组织Spring框架.安全.过滤器链”的 Bean 时出错

苏嘉歆
2023-03-14

你好,我正在尝试按照本教程在我的项目中实现Spring Securityhttp://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/#config,在运行我的程序时,我遇到了以下异常-

org.springframework.beans.factory。BeanCreationException:创建名为“org.springframework.security”的bean时出错。filterChains”:bean初始化失败;嵌套异常是java.lang.NoSuchFieldError:NULL,位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractautoWireCapbleBeanFactory.java:532)

我的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">

    <display-name>Spring MVC Application</display-name>

    <!-- Spring root -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
         org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>org.baeldung.spring</param-value>
    </context-param>

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

    <!-- Spring child -->
    <servlet>
        <servlet-name>api</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>api</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

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

    <!-- <welcome-file-list> -->
    <!-- <welcome-file>index.html</welcome-file> -->
    <!-- </welcome-file-list> -->

</web-app>

我的webSecurityConfig.xml看起来像-

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”

<http use-expressions="true" entry-point-ref="restAuthenticationEntryPoint">
    <intercept-url pattern="/api/**" access="isAuthenticated()" />

    <sec:form-login authentication-success-handler-ref="mySuccessHandler" />

    <logout />
</http>

<beans:bean id="mySuccessHandler" class="org.baeldung.security.MySavedRequestAwareAuthenticationSuccessHandler" />

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="temporary" password="temporary" authorities="ROLE_ADMIN" />
            <user name="user" password="userPass" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

我知道这可能是org.springframework.beans.factory.BeanCreationException的重复:创建名称为“org.springframework.security.filterChains”的bean时出错,但我尝试了这个解决方案,但没有成功,这可能是因为我还是一个Spring新手。非常感谢任何帮助。

我正在org.baeldung.spring框架中从SecSecurityConfig.java加载websecurityconfig.xml,它看起来像-

package org.baeldung.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({ "classpath:webSecurityConfig.xml" })
@ComponentScan("org.baeldung.security")
public class SecSecurityConfig {

    public SecSecurityConfig() {
        super();
    }

}

共有1个答案

邵奇
2023-03-14

查看stacktrace信息,似乎您的类路径中有冲突的框架jar。当使用maven使用< code>mvn dependency:tree来确定使用了哪些依赖项时,我怀疑在您的类路径中有一个旧的spring-beans.jar。

 类似资料: