[英]No bean named 'myUserDetailsService' is defined Spring Security with hibernate and Spring MVC
I am following this tutorial but i need to make querys without the hbm.xml files and instead use dao and service layers. 我正在遵循本教程,但是我需要在没有hbm.xml文件的情况下进行查询,而是使用dao和service层。 I have already generated models for both users and user_roles tables using hibernate.
我已经使用休眠为user和user_roles表生成了模型。 I also created the dao's and service layers.
我还创建了dao和服务层。
The problem is i get an error that says No bean named 'myUserDetailsService' is defined. 问题是我收到一条错误消息,提示未定义名为“ myUserDetailsService”的bean。 I know i have to create a bean but i can't figure out how to do it.
我知道我必须创建一个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
I have uploaded the full project to github. 我已经将整个项目上传到了github。 Any help/ advice is apreciated.
任何帮助/建议都非常重要。
https://github.com/kunal7L/Spring-MVC-Security-Hibernate https://github.com/kunal7L/Spring-MVC-Security-Hibernate
My beans: 我的豆子:
<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 MyUserDetailsService Class: 完整的MyUserDetailsService类:
public class MyUserDetailsService implements UserDetailsService { 公共类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;
}
} }
The error is very clear: 错误非常清楚:
No bean named 'myUserDetailsService' is defined
没有定义名为“ myUserDetailsService”的Bean
In your file spring-security.xml, you have referred to a bean named myUserDetailsService: 在文件spring-security.xml中,您引用了一个名为myUserDetailsService的bean:
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService" >
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
However, you have commented out the part which define that service in spring-database.xml 但是,您已经注释掉了spring-database.xml中定义该服务的部分
<!-- <bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService"> -->
<!-- <property name="userDao" ref="userDao" /> -->
<!-- </bean> -->
That's why the error happens 这就是为什么会发生错误
I solved it by understanding Hoàng Long's logic. 我通过理解霍隆的逻辑解决了这个问题。 I knew i was missing a bean but i just didn't know what to reference inside it.
我知道我缺少一个豆子,但我只是不知道在其中引用什么。 So i googled it and found out that MyUserDetailsService implements on UserDetailsService which is a part of Spring Security.
所以我用谷歌搜索它,发现MyUserDetailsService在UserDetailsService上实现,后者是Spring Security的一部分。 So the only thing left to do was to add the bean with no property at all.
因此,剩下要做的就是添加没有属性的bean。
<bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService" >
</bean>
Most likely you have issue with you Dao object, i am not sure how you set up but if you use annotation then you must configure you persistance.xml and put it in the META-INF folder, and you need to make the application context know about it this link will help you to understand your issue better http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial just look for the Hibernate datasource configuration and how to use it in your Dao layer. 很有可能您的Dao对象存在问题,我不确定您如何设置,但是如果使用批注,则必须配置persistanceance.xml并将其放在META-INF文件夹中,并且需要使应用程序上下文已知关于此链接,此链接将帮助您更好地了解您的问题http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial仅查找Hibernate数据源配置以及如何使用它在您的刀层中。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.