一、简单入门测试
BCrypt不支持反运算,只支持密码校验
package com.william.test;
import org.springframework.security.crypto.bcrypt.BCrypt;
/**
* @author :lijunxuan
* @date :Created in 2019/7/11 11:34
* @description :
* @version: 1.0
*/
public class TestBcrypt {
public static void main(String[] args) {
//获取严值
String gensalt = BCrypt.gensalt();
//加密
String encoder = BCrypt.hashpw("123456", gensalt);
System.out.println("pwd===="+encoder);
/**
* 验证密码的正确性
*/
boolean isCheckpw = BCrypt.checkpw("123456", "$2a$10$IOWE94GVyfjnQfvUIqIbXeEVF9XKjK/3Zua6JuXiVgO1ZAkcK9NwS");
System.out.println("检查判断密码的结果:"+isCheckpw);
}
}
二、场景引入,管理员登录密码验证
(1)AdminController
/**
* 管理员登录
* @param admin
* @return
*/
@PostMapping(value = "/login")
public Result login(@RequestBody Admin admin){
boolean isLogin =adminService.isLogin(admin);
if (isLogin){
return new Result(true,StatusCode.OK,"登录成功",ResultMap);
}else {
return new Result(false,StatusCode.ERROR,"登录失败");
}
}
(2)AdminService
/**
* 管理员登录
* @param admin
* @return
*/
Boolean isLogin(Admin admin);
(3)AdminServiceImpl
/**
* 用户登录
* @param admin
* @return
*/
@Override
public Boolean isLogin(Admin admin) {
if (admin==null){
return false;
}
Admin admin1 = new Admin();
//获取用户输入的明文用户名
admin1.setLoginName(admin.getLoginName());
admin1.setStatus("1");
//从数据库中查询出用户对象
Admin adminDB = adminMapper.selectOne(admin1);
//如果根据用户名查询不到数据,则判断用户名输入错误
if (adminDB==null){
return false;
}else {
//如果用户名输入正确则验证密码是否正确
return BCrypt.checkpw(admin.getPassword(),adminDB.getPassword());
}
}