As Spring Security 3.1 provides a new convenient PasswordEncoder and an implementation StardardPasswordEncoder, I try to implement them into current project. However, there is question about how the PasswordEncoder should work when save/verify user password. Here is code for the new PasswordEncoder:
private static final String SALT_AS_TOP_SECRET = "mysalt";
private static final PasswordEncoder encoder = new StandardPasswordEncoder(
SALT_AS_TOP_SECRET);
public static String encrypt(String rawPassword) {
return encoder.encode(rawPassword);
}
public static boolean match(String rawPassword, String password) {
return encoder.matches(rawPassword, password);
}
My applicaiton is Spring with JPA, so there is a User entity implements UserDetails with basic getter/setter for the password, also there is a service class implement the UserDetailService in the service layer.
I am confused about the UserDetailService only requires a loadUserByUsername
method which doesn't check password. So here are other questions:
- Where shall I place the
match
method from PasswordEncoder? - Shall I implement another interface for the service class?
- Shall I give any logic within User entity when setting password?
- As the new PasswordEncoder interface is independent to other security package, I bet there is no need to initiate/inject the bean from xml? I try to pu the salt inside the class instead of external storage.
Please give a hint, thanks in advance.