Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify password with Spring

I have found out how to hash the password of some one and persist it in the database with SpringMVC:

BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String pw = passwordEncoder.encode("test");

Now the question is, how I can verify the password coming from the request to let the user login my web app? After some research I saw, that there are a lot of ways to do this. Some solutions works with user roles.

What my webapps should do is to offer my users a login page where they can register (here I would persist the password with the code shown above). After registering they should be able to login, which means I need to verify the password from the login form. Is there any state of the art example out there?

like image 200
kemal89 Avatar asked Aug 19 '15 21:08

kemal89


Video Answer


1 Answers

This is how a raw password can be matched to an encoded one:

passwordEncoder.matches("rawPassword", user.getPassword()),

But as others say, coding Security on your own is cumbersome, and so I'd recommend using Spring Security instead. Yes, it does take effort to learn, but you can find good tutorials on it. Here is a unique tutorial on it (disclaimer: I'm the author).

like image 174
Sanjay Avatar answered Oct 27 '22 16:10

Sanjay