I have got stuck at creating usefull form login in SpringMVC using Spring Security. I'm quite newbie in this and also Hibernate. I would like to create simple form login which could provide access to my web application.
I have created my project using SpringSource Tool Suite and selecting Spring Template Project. It uses Maven and I have also generated by Hibernate classes with annotations and hibernate.cfg.xml
. In my database (HSQLDB) I have three tables: users, roles and users_roles. The third one contains user_id and role_id, so it stores information about users' roles. I have generated class by Hibernate successful.
I have started writing my class implementing UserDetailsService. But I don't know how to do this properly. In spring-security.xml
I have defined bean like this:
<bean id="userDetailsService" class="hutter.pl.services.HutterUserDetailsService" />
I would like to use hashing by sha-256 with saltSource.
<bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource">
<property name="userPropertyToUse" value="username"/>
</bean>
<security:authentication-manager>
<security:authentication-provider user-service-ref="userDetailsService">
<security:password-encoder hash="sha-256">
<security:salt-source ref="saltSource" />
</security:password-encoder>
</security:authentication-provider>
</security:authentication-manager>
Should I have used this solution: https://stackoverflow.com/a/1654488/845220 ? Hibernate have generted classes like: RolesHome, Roles, Users, UsersHome, UsersRoles, UsersRolesHome. But i really don't know how to use these Hibernates classes to authorize users.
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UsersHome usersHome = new UsersHome();
//Users user = ...
//...
return null;
}
}
Could you give me some hints?
EDIT:
I have tried to add method public Users findByLogin(String login)
to the UsersHome
class.
public Users findByLogin(String login) {
log.debug("getting Users instance with login: " + login);
try {
Users instance = entityManager.find(Users.class, login);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
And body of my UserDetailsService looks like:
UsersHome usersHome = new UsersHome();
Users user = usersHome.findByLogin(username);
But I have got excpetion:
ERROR: my.package.dao.UsersHome - get failed
java.lang.NullPointerException
at my.package.dao.UsersHome.findByLogin(UsersHome.java:72)
at my.package.services.HutterUserDetailsService.loadUserByUsername(MyUserDetailsService.java:19)