没有名为' myUserDetailsS​​ervice'使用hibernate和Spring MVC定义了Spring Security

时间:2015-05-06 16:30:43

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

我关注this tutorial但我需要在没有hbm.xml文件的情况下进行查询,而是使用dao和服务层。我已经使用hibernate为users和user_roles表生成了模型。我还创建了dao和服务层。

问题是我得到一个错误,上面写着没有bean命名为' myUserDetailsS​​ervice'被定义为。我知道我必须创建一个bean,但我无法弄清楚如何去做。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailsService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    ... 93 more

我已将完整项目上传到github。任何帮助/建议都是相关的。

https://github.com/kunal7L/Spring-MVC-Security-Hibernate

我的豆子:

 <bean id="userDao" class="com.mkyong.users.dao.UserDaoImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
    <bean id="userService" class="com.mkyong.users.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

<!--    <bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService"> -->
<!--        <property name="userDao" ref="userDao" /> -->
<!--    </bean> -->

Full MyUserDetailsS​​ervice类:

public class MyUserDetailsS​​ervice实现UserDetailsS​​ervice {

private UserService userservice;

@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

    // Programmatic transaction management
    /*
    return transactionTemplate.execute(new TransactionCallback<UserDetails>() {

        public UserDetails doInTransaction(TransactionStatus status) {
            com.mkyong.users.model.User user = userDao.findByUserName(username);
            List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRole());

            return buildUserForAuthentication(user, authorities);
        }

    });*/

    Users user = userservice.findByUserName(username);
    List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRoleses());

    return buildUserForAuthentication(user, authorities);


}

// Converts com.mkyong.users.model.User user to
// org.springframework.security.core.userdetails.User
private User buildUserForAuthentication(Users user, List<GrantedAuthority> authorities) {
    return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities);
}

private List<GrantedAuthority> buildUserAuthority(Set<UserRoles> userRoles) {

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

    // Build user's authorities
    for (UserRoles userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
    }

    List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

    return Result;
}

}

3 个答案:

答案 0 :(得分:2)

错误非常明确:

  

没有名为&#39; myUserDetailsS​​ervice&#39;已定义

在你的文件spring-security.xml中,你引用了一个名为myUserDetailsS​​ervice的bean:

ORDER BY

但是,您已在spring-database.xml中注释掉了定义该服务的部分

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailsService" >
        <password-encoder hash="bcrypt" />    
    </authentication-provider>
</authentication-manager>

发生错误的原因

答案 1 :(得分:1)

我通过理解HoàngLong的逻辑解决了这个问题。我知道我错过了一个豆,但我只是不知道在里面引用了什么。所以我用Google搜索并发现MyUserDetailsS​​ervice在UserDetailsS​​ervice上实现,这是Spring Security的一部分。所以唯一要做的就是添加没有属性的bean。

<bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService" >

    </bean>

答案 2 :(得分:0)

你最有可能遇到Dao对象的问题,我不知道你是如何设置的,但是如果你使用注释那么你必须配置你persistance.xml并把它放在META-INF文件夹中,你需要制作应用程序上下文了解此链接将帮助您更好地了解您的问题 http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial 只需查看Hibernate数据源配置以及如何在Dao层中使用它。

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url"
            value="jdbc:mysql://localhost:3306/TestDB" />
        <beans:property name="username" value="pankaj" />
        <beans:property name="password" value="pankaj123" />
    </beans:bean>