1

我有这门课

@Service("customUserDetailsService")
@Transactional(readOnly=true)
public class CustomUserDetailsService implements UserDetailsService { ....}

我试图让这项工作

安全 XML

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    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/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
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.carlosocortina.paidosSimple,com.carlosocortina.paidosSimple.dao,com.carlosocortina.paidosSimple.service"/>

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/sec/moderation.html" access="ROLE_ASISTENTE" />  
        <intercept-url pattern="/admin/*" access="ROLE_DOCTOR" />   
        <form-login login-page="/user-login.html" 
            default-target-url="/success-login.html" 
            authentication-failure-url="/error-login.html" />  
        <logout logout-success-url="/index.html" />
    </http>  

    <authentication-manager alias="authenticationManager">  
        <authentication-provider user-service-ref="customUserDetailsService" />
    </authentication-manager>  

</beans:beans>  

但我不断收到此错误:

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 [2]; 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': Cannot resolve reference to bean 'customUserDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customUserDetailsService' is defined

简而言之:没有定义名为“customUserDetailsS​​ervice”的 bean

我不知道为什么,任何人都可以给我和建议。

附加信息

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/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>

    <!-- Spring Security Configuration -->
    <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>

    <!-- Processes application requests -->
    <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/spring/appServlet/servlet-context.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>

</web-app>
4

3 回答 3

1

尝试添加@Configurable到您的服务

于 2013-08-05T22:30:07.487 回答
1

我怀疑 CustomUserDetailsS​​ervice 的包与您的组件扫描声明不匹配:

<context:component-scan base-package="com.carlosocortina.paidosSimple,com.carlosocortina.paidosSimple.dao,com.carlosocortina.paidosSimple.service"/>

或者,@Service("customUserDetailsService")语法可能没有设置 bean id,而是设置了 bean 名称。

如果这两者都不是原因,那么可能 Spring 只是无法引用尚未扫描的 bean(如果是这种情况,有点难过)。

作为替代方案,您可以@Service从 CustomUserDetailsS​​ervice 中删除注释并在 xml 上手动声明您的 bean,例如:

<bean id="customUserDetailsService" class="...">
于 2013-08-05T23:24:23.260 回答
0

这是因为它servlet-context.xml被放置在调度程序 servlet 中。由于 dispatcher servlet 是 parent context 的 child,因此 parent context 没有 child 的依赖关系,所以如果你把servlet-context.xmlinsidecontext参数放在里面,并且你必须有appServlet-servlet.xmlinsideinit参数,它就可以正常工作。

于 2013-12-24T12:47:53.150 回答