BCrypt密码加密
BCrypt比MD5安全,因为其内部引入加盐(29个随机字符)了机制。
import org.springframework.security.crypto.bcrypt.BCrypt;
public class testBCrypt {
public static void main(String[] args) {
//1.加密(用户密码加密:当把用户的密码存入数据库中时,进行加密)
//获取盐
String gensalt = BCrypt.gensalt();
//基于当前的盐对密码进行加密
String hashpw = BCrypt.hashpw("123456", gensalt);
System.out.println("加密后的密文:"+hashpw);
//2.解密
boolean checkpw = BCrypt.checkpw("123456", hashpw);
System.out.println("解密结果:"+checkpw);
}
}
login方法
@Override
public boolean login(Admin admin) {
//根据登录名获取管理员信息
Admin admin1 = new Admin();
admin1.setLoginName(admin.getLoginName());
admin1.setStatus("1");
Admin adminResult = adminMapper.selectOne(admin1);
if (adminResult == null){
return false;
}else{
//对密码进行校验
return BCrypt.checkpw(admin.getPassword(),adminResult.getPassword());
}
//返回结果
}
@PostMapping("/login")
public Result login(@RequestBody Admin admin){
boolean login = adminService.login(admin);
if (login){ //如果登录成功
HashMap<String, String> info = new HashMap<>();
info.put("username",admin.getLoginName());
//String token = JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getLoginName(), null);
//info.put("token",token);
return new Result(true, StatusCode.OK,"登陆模式",info);
}else {
return new Result(false,StatusCode.LOGINERROR,"用户名或者密码错误");
}
}