controller
/**
* 校验验证码
*
* @param param
* @param token
* @return
*/
@PostMapping("phone/checkVerificationCode")
public ResponseEntity<Map<String, Object>> checkVerificationCode(@RequestBody Map<String, String> param,
@RequestHeader("Authorization") String token) {
try {
String code = param.get("verificationCode");
Boolean bool = this.myCenterService.checkVerificationCode(code, token);
Map<String, Object> result = new HashMap<>();
result.put("verification", bool);
return ResponseEntity.ok(result);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
======================================================
service
public Boolean checkVerificationCode(String code, String token) {
//校验token
User user = this.userService.queryUserByToken(token);
if(ObjectUtil.isEmpty(user)){
return false;
}
//校验验证码,先查询redis中的验证码
String redisKey = "CHECK_CODE_" + user.getMobile();
String value = this.redisTemplate.opsForValue().get(redisKey);
if(StrUtil.equals(code, value)){
//将验证码删除
this.redisTemplate.delete(redisKey);
return true;
}
return false;
}