简体   繁体   English

Spring Security .DaoAuthenticationProvider:无法解析对bean的引用

[英]Spring Security .DaoAuthenticationProvider: Cannot resolve reference to bean

I am implementing Spring Security in project. 我正在项目中实施Spring Security。 I have reached an impasse stuck here since hours. 几个小时以来,我陷入了僵局。

I am getting this error 我收到此错误

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

The project setup is very simple 项目设置非常简单

spring-security.xml 弹簧security.xml文件

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

dispatcher-servlet.xml 调度员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 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>

CustomUserDetailsService.java 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;
    }

}

I also tried declaring the bean in both dispatcher-servlet.xml & application-context.xml it doesn't work 我还尝试了在dispatcher-servlet.xml和application-context.xml中都声明该bean不起作用

checked the context-component base package. 检查了上下文组件基本包。 It is scanning all the other classes present just fine. 它正在扫描所有其他存在的类。 When I remove myUserDetailService from authentication provider the server starts just fine without any error. 当我从身份验证提供程序中删除myUserDetailService时,服务器启动正常,没有任何错误。

I am really tired. 我真的很累了。 Can anyone please help me in fixing this? 谁能帮我解决这个问题?

phew... got it working. ...让它工作了。

Moved 移动

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

from dispatcher-servlet to application-context.xml 从调度程序servlet到application-context.xml

Would anyone care to tell why it started working? 有人愿意告诉它为什么开始工作吗?

将定义移动到上下文文件后使之工作的原因是因为在春季,分派器servlet内的定义仅对mvc可见,并且上下文内的定义是全局的(对servlet和安全性而言),这是一个页面,其中其明确说明的https://weblogs.java.net/blog/sgdev-blog/archive/2014/07/05/common-mistakes-when-using-spring-mvc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Security:设置构造函数参数时无法解析对bean的引用 - Spring security : Cannot resolve reference to bean while setting constructor argument Spring Security-找不到类DaoAuthenticationProvider - Spring security - Cannot find class DaoAuthenticationProvider Spring SAML实现无法解析对bean的引用 - Spring SAML implementation cannot resolve reference to bean 无法解析对bean&#39;dataSource&#39;的Spring3 + Struts 2的引用 - Cannot resolve reference to bean 'dataSource' Spring3 + Struts 2 Spring事务管理:无法解析bean&#39;transactionManager&#39;的引用 - Spring transaction management: Cannot resolve reference to bean 'transactionManager' 无法解析对bean MapExecutionContextDao的引用 - Cannot resolve reference to bean MapExecutionContextDao 无法解析对 bean &#39;cacheManager&#39; 的引用 - Cannot resolve reference to bean 'cacheManager' 使用键[0]设置bean属性“ sourceList”时,无法解析对bean&#39;org.springframework.security.web.DefaultSecurityFilterChain#0&#39;的引用 - cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0] 无法解析对bean&#39;sessionFactory&#39;的引用 - Cannot resolve reference to bean 'sessionFactory' Spring Security,Boot:替换默认 DaoAuthenticationProvider - Spring Security, Boot: replace default DaoAuthenticationProvider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM