【问题标题】:Spring Security .DaoAuthenticationProvider: Cannot resolve reference to beanSpring Security .DaoAuthenticationProvider:无法解析对 bean 的引用
【发布时间】:2014-05-09 22:30:59
【问题描述】:

我正在项目中实施 Spring Security。几个小时以来,我陷入了僵局。

我收到了这个错误

Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'UserDAOImpl' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailService' is defined

项目设置非常简单

spring-security.xml

<authentication-manager>
        <authentication-provider user-service-ref="myUserDetailService">
      </authentication-provider>
    </authentication-manager>

调度程序-servlet.xml

<context:component-scan base-package="app.com,app.com.controller,app.com.dao,app.com.service,app.com.model"/>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
            p:basename="messages"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

ApplicationContext.xml

  <context:annotation-config />

    <!-- <context:property-placeholder> XML element automatically registers a new PropertyPlaceholderConfigurer 
    bean in the Spring Context. -->
    <context:property-placeholder location="classpath:database.properties" />

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> 

    <!-- Creating DataSource -->
      <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
      </bean>

    <!-- To persist the object to database, the instance of SessionFactory interface is created. 
SessionFactory is a singleton instance which implements Factory design pattern. 
SessionFactory loads hibernate.cfg.xml and with the help of TransactionFactory and ConnectionProvider 
implements all the configuration settings on a database. -->

<!-- Configuring SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>app.com.model.User</value>
                <value>app.com.model.Roles</value>
                <value>app.com.BaseEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

<!-- Configuring Hibernate Transaction Manager -->
    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    </beans>

CustomUserDetailsS​​ervice.java

@Service("myUserDetailService")
@Transactional
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDAO userDAO;

    /*@Override
    public UserDetails loadUserByUsername(String arg0)
            throws UsernameNotFoundException, DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

}

我还尝试在 dispatcher-servlet.xml 和 application-context.xml 中声明 bean 不起作用

检查了上下文组件基础包。它正在扫描所有其他存在的类。当我从身份验证提供程序中删除 myUserDetailService 时,服务器启动正常,没有任何错误。

我真的很累。谁能帮我解决这个问题?

【问题讨论】:

    标签: java spring hibernate spring-mvc spring-security


    【解决方案1】:

    呸...让它工作了。

    移动

    <context:component-scan base-package="app.com,app.com.controller,app.com.dao,app.com.service,app.com.model"/>
    
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
                p:basename="messages"/>
    

    从 dispatcher-servlet 到 application-context.xml

    有人愿意告诉它为什么开始工作吗?

    【讨论】:

      【解决方案2】:

      在将定义移动到上下文文件后,您之所以可以进行此操作,是因为在 Spring 中,调度程序 servlet 内的定义仅对 mvc 可见,而上下文内的定义是全局的(对于 servlet 和安全性),这里是一个清楚解释的页面https://weblogs.java.net/blog/sgdev-blog/archive/2014/07/05/common-mistakes-when-using-spring-mvc

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 2013-01-04
        • 1970-01-01
        • 2022-01-14
        • 2014-08-24
        • 2014-09-29
        • 1970-01-01
        • 2023-01-19
        • 1970-01-01
        相关资源
        最近更新 更多