spring - 设置 bean 属性 'org.springframework.security.web.DefaultSecurityFilterChain#0' 时无法解析对 bean 'sourceList' 的引用

标签 spring spring-security spring-bean

我需要使用我的自定义身份验证提供程序类添加 spring 安全性。这是我的安全配置文件。

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

    <context:component-scan base-package="com.me.web.controllers"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="myProvider" class="com.me.web.controllers.CustomAuthenticationProvider">

    </bean>

    <security:http auto-config='true'>
        <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
        <security:http-basic />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider user-service-ref='myProvider'/>
    </security:authentication-manager>

    <mvc:annotation-driven />
</beans>

这是我的 web.xml 中与身份验证相关的部分:

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

但是我得到以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [5]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.omd.web.controllers.CustomAuthenticationProvider' to required type 'org.springframework.security.core.userdetails.UserDetailsService' for property 'userDetailsService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.omd.web.controllers.CustomAuthenticationProvider] to required type [org.springframework.security.core.userdetails.UserDetailsService] for property 'userDetailsService': no matching editors or conversion strategy found

猜猜问题出在哪里,或者我应该调查哪里?

最佳答案

user-service-ref='myProvider'

您的提供者不是 UserDetailsS​​ervice,它应该是

<authentication-manager> <authentication-provider ref="myProvider" /> </authentication-manager>

关于spring - 设置 bean 属性 'org.springframework.security.web.DefaultSecurityFilterChain#0' 时无法解析对 bean 'sourceList' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328473/

相关文章:

java - 使用 Spring MVC 将许多参数从 Controller 传递到服务

spring-mvc - @WithMockUser 忽略用户名、密码字段?

java - 在 Spring 的多个 beans 配置文件上使用 '<import resource=' ?

java - Spring bean 延迟初始化

Spring Boot JPA Like 查询

spring - http出站网关的拦截器

java - 自定义注释初始化方法不起作用

java - 错误: cannot access Filter

java - Spring 启动+安全: No Access-Control-Allow-Origin header when setting allowed origins

java - HazelcastInstance bean - 正确的销毁方法是什么?