我正在遵循this tutorial,但是我需要在没有hbm.xml文件的情况下进行查询,而是使用dao和service层。我已经使用休眠为user和user_roles表生成了模型。我还创建了dao和服务层。
问题是我收到一条错误消息,提示未定义名为“ myUserDetailsService”的bean。我知道我必须创建一个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> -->
完整的MyUserDetailsService类:
公共类MyUserDetailsService实现UserDetailsService {
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;
}
}
最佳答案
错误非常清楚:
没有定义名为“ myUserDetailsService”的bean
在文件spring-security.xml中,您引用了一个名为myUserDetailsService的bean:
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService" >
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
但是,您已经注释掉了spring-database.xml中定义该服务的部分
<!-- <bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService"> -->
<!-- <property name="userDao" ref="userDao" /> -->
<!-- </bean> -->
这就是为什么会发生错误