这是我在春季实现自己的更改密码功能时使用的
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class Utility {
private static BCryptPasswordEncoder passwordEcorder = new BCryptPasswordEncoder();
public String bcryptEncryptor(String plainText) {
return passwordEcorder.encode(plainText);
}
public Boolean doPasswordsMatch(String rawPassword,String encodedPassword) {
return passwordEcorder.matches(rawPassword, encodedPassword);
}
}
然后在代码中匹配两个密码,即rawPassword = password1234和codedPassword = $2a $10 $zxvEq8XzYEYtNjbkRsJEbukHeRx3XS6MDXHMu8cNuNsRfZJWwswDy
代码将像这样(在实用程序类的某个自动连接对象上调用):
doPasswordsMatch( rawPassword, encodedPassword );
如果密码匹配,这将返回TRUE,否则它将是FALSE