I tried searching in Google, but I could not find any good examples where a username and password are checked with a database for authentication purposes.
In further simple words, how can I create a simple login form using Spring and Hibernate where the credentials are checked with the database.
Update
Cam anyone come up with a simple example where I can see how the flow goes and how the input data is passed to hibernate?
At first you should define this file
WEB-INF/spring/serurity-context.xml:Now you should create
org.my.UserServiceclass and implement interfaceorg.springframework.security.core.userdetails.UserDetailsService. This interface has one method:And in this method you can use Hibernate in order to load user by userName. If user does not exists – just throw UsernameNotFoundException, otherwise return new intialized UserDetails instance (there you can provide a lot of stuff like user roles, account expiration date, etc…).
Now comes
web.xml:If you have any questions or something goes wrong, feel free to ask 🙂
PS: So with UserDetailsService you don’t have to check password of whether user account is active, etc. You just provide spring-security information about user with provided
userNameand framework validates user itself. If you encode your passwords with MD5 for example, than you can usepassword-encoderlike this:Update
Now we will dive more deeper in
UserService– my (simplified) real world example.UserServiceclass:Now
SecurityUser:And finally
UserDao:As you can see I used
HibernateTemplatehere.