导入Bcrypt依赖
用于加密解密用户密码
<!-- https://mvnrepository.com/artifact/org.mindrot/jbcrypt --> <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency>
基本和之前的手机号校验登录差不多 换汤不换药
用户登录流程:
BCrypt.checkpw() 用户校验用户密码 若密码匹配则返回true
controller
public GraceJSONResult adminLogin(AdminLoginVO adminLoginVO, HttpServletRequest request, HttpServletResponse response, BindingResult bindingResult) { // 1. 首先查询字段是否合法 if (bindingResult.hasErrors()) { Map<String, String> bindResultErrors = super.getBindResultErrors(bindingResult); return GraceJSONResult.errorMap(bindResultErrors); } // 2. 首先根据用户名查询 用户在database中是否有记录 AdminUser usernameByAdmin = adminService.findUsernameByAdmin(adminLoginVO.getUsername()); if (usernameByAdmin == null) { return GraceJSONResult.errorCustom(ResponseStatusEnum.ADMIN_NOT_EXIT_ERROR); } // 3. 查看用户名密码是否匹配 boolean flag = BCrypt.checkpw(adminLoginVO.getPassword(), usernameByAdmin.getPassword()); if (flag) { String token = UUID.randomUUID().toString(); setCookie(request, response, "atoken", token, COOKIE_AGE); setCookie(request, response, "aid", usernameByAdmin.getId(), COOKIE_AGE); setCookie(request, response, "aname", usernameByAdmin.getAdminName(), COOKIE_AGE); redisOperator.set(REDIS_ADMIN_TOKEN + ":" + usernameByAdmin.getId(), token); return GraceJSONResult.ok(); } else { return GraceJSONResult.errorCustom(ResponseStatusEnum.ADMIN_NOT_EXIT_ERROR); } }
service直接调用mapper 根据用户名查询用户并返回
public AdminUser findUsernameByAdmin(String username) { QueryWrapper<AdminUser> adminUserQueryWrapper = new QueryWrapper<>(); adminUserQueryWrapper.eq("username", username); return adminMapper.selectOne(adminUserQueryWrapper); }