检查短信验证码

main
XiaoHH 2 years ago
parent 954f9eec14
commit 4f3e06237b

@ -3,6 +3,7 @@ package com.greateme.passenger.controller;
import com.greateme.entity.respose.ResponseEntity;
import com.greateme.passenger.service.PassengerVerificationService;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
@ -42,4 +43,16 @@ public class PassengerVerificationController {
return ResponseEntity.error("生成短信验证码失败");
}
}
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@PostMapping("/check")
public ResponseEntity<Boolean> checkVerificationCode(@RequestBody PassengerVerificationCodeVO passengerVerificationCode) {
Boolean checkResult = this.verificationService.checkPhoneVerificationCode(passengerVerificationCode);
return ResponseEntity.success(checkResult);
}
}

@ -1,6 +1,7 @@
package com.greateme.passenger.service;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
/**
* <p>
@ -21,4 +22,12 @@ public interface PassengerVerificationService {
* @return token
*/
String generationPhoneVerificationCode(PassengerVerificationCodeDTO verificationCode);
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
Boolean checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode);
}

@ -1,5 +1,6 @@
package com.greateme.passenger.service.impl;
import com.greateme.contant.business.VerificationCodeConstant;
import com.greateme.contant.http.HttpStatus;
import com.greateme.entity.respose.ResponseEntity;
import com.greateme.passenger.service.PassengerVerificationService;
@ -12,7 +13,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Objects;
import java.util.UUID;
/**
@ -58,4 +61,21 @@ public class PassengerVerificationServiceImpl implements PassengerVerificationSe
log.error("获取验证码的时候发生了错误,错误码:{},错误信息:{}", passengerVerificationCodeEntity.getCode(), passengerVerificationCodeEntity.getMessage());
return null;
}
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@Override
public Boolean checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode) {
ResponseEntity<Integer> checkResult = verificationCodeRemote.checkPhoneVerificationCode(passengerVerificationCode);
if (HttpStatus.SUCCESS.getCode() == checkResult.getCode()) {
return Objects.equals(VerificationCodeConstant.VERIFICATION_CODE_RIGHT, checkResult.getDate());
}
log.error("判断验证码是否正确的是否发生了错误,错误码:{},错误信息:{}", checkResult.getCode(), checkResult.getMessage());
return null;
}
}

@ -0,0 +1,39 @@
package com.greateme.contant.business;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-07-05 22:26:23
* @file VerificationCodeConstant.java
*/
public class VerificationCodeConstant {
/**
*
*/
private VerificationCodeConstant() {
}
/**
* redis
*/
public static final String PASSENGER_PHONE_VERIFICATION_CODE_PREFIX = "PASSENGER::PHONE::VERIFICATION::CODE::";
/**
*
*/
public static final Integer VERIFICATION_CODE_RIGHT = 0;
/**
*
*/
public static final Integer VERIFICATION_CODE_TIMEOUT = 1;
/**
*
*/
public static final Integer VERIFICATION_CODE_WARING = 2;
}

@ -27,4 +27,21 @@ public class StringUtil {
public static String generationVerificationCode(int length) {
return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, length - 1)));
}
/**
* true
*
* @param str1 1
* @param str2 2
* @return
*/
public static boolean equal(String str1, String str2) {
if (null == str1 && null == str2) {
return true;
} else if (null != str1) {
return str1.equals(str2);
} else {
return false;
}
}
}

@ -32,4 +32,13 @@ public interface VerificationCodeRemote {
*/
@PostMapping("/verificationCode/passengerPhone/{length}")
ResponseEntity<PassengerVerificationCodeVO> passengerPhone(@RequestBody PassengerVerificationCodeDTO verificationCode, @PathVariable("length") int length);
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@PostMapping("/verificationCode/checkVerificationCode")
ResponseEntity<Integer> checkPhoneVerificationCode(@RequestBody PassengerVerificationCodeVO passengerVerificationCode);
}

@ -43,6 +43,18 @@ public class VerificationCodeFallback implements FallbackFactory<VerificationCod
log.error("调用远程发送验证码的接口发生了错误,手机号码:{},验证码长度:{}", verificationCode.getPassengerPhone(), length);
return ResponseEntity.error();
}
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@Override
public ResponseEntity<Integer> checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode) {
log.error("跳用远程验证验证码是否正确的接口发生错误验证码的token{},验证码:{}", passengerVerificationCode.getVerificationToken(), passengerVerificationCode.getVerificationCode());
return ResponseEntity.error();
}
};
}
}

@ -35,4 +35,15 @@ public class VerificationCodeController {
public ResponseEntity<PassengerVerificationCodeVO> passengerPhone(@RequestBody PassengerVerificationCodeDTO verificationCode, @PathVariable("length") int length) {
return ResponseEntity.success(this.verificationCodeService.generationPassengerPhone(verificationCode, length));
}
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@PostMapping("/checkVerificationCode")
public ResponseEntity<Integer> checkPhoneVerificationCode(@RequestBody PassengerVerificationCodeVO passengerVerificationCode) {
return ResponseEntity.success(this.verificationCodeService.checkPhoneVerificationCode(passengerVerificationCode));
}
}

@ -23,4 +23,12 @@ public interface VerificationCodeService {
* @return
*/
PassengerVerificationCodeVO generationPassengerPhone(PassengerVerificationCodeDTO verificationCode, int length);
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
Integer checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode);
}

@ -1,12 +1,13 @@
package com.greateme.verification.service.impl;
import com.greateme.contant.business.VerificationCodeConstant;
import com.greateme.util.StringUtil;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.dto.PassengerVerificationCodeRedisDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
import com.greateme.verification.service.VerificationCodeService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@ -32,10 +33,8 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
@Autowired
private RedisTemplate redisTemplate;
/**
* redis
*/
private static final String PASSENGER_PHONE_VERIFICATION_CODE_PREFIX = "PASSENGER::PHONE::VERIFICATION::CODE::";
@Value("${business.verification-code.timeout-minute}")
private Integer verificationCodeTimeoutMinute;
/**
*
@ -56,11 +55,35 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
verificationCodeRedis.setPassengerPhone(verificationCode.getPassengerPhone());
verificationCodeRedis.setToken(token);
// 将 verificationCodeRedis 存入redis
this.redisTemplate.opsForValue().set(PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + token, verificationCodeRedis, 5, TimeUnit.MINUTES);
this.redisTemplate.opsForValue().set(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + token, verificationCodeRedis, this.verificationCodeTimeoutMinute, TimeUnit.MINUTES);
// 将token和code返回
PassengerVerificationCodeVO result = new PassengerVerificationCodeVO();
result.setVerificationCode(code);
result.setVerificationToken(token);
return result;
}
/**
*
*
* @param passengerVerificationCode
* @return bool
*/
@Override
public Integer checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode) {
// 获取短信验证码的token并在redis当中获取正确的短信验证码
String verificationToken = passengerVerificationCode.getVerificationToken();
PassengerVerificationCodeRedisDTO verificationCodeRedis = (PassengerVerificationCodeRedisDTO) this.redisTemplate.opsForValue().get(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + verificationToken);
if (null == verificationCodeRedis) {
return VerificationCodeConstant.VERIFICATION_CODE_TIMEOUT;
} else {
String verificationCode = verificationCodeRedis.getVerificationCode();
if (StringUtil.equal(verificationCode, passengerVerificationCode.getVerificationCode())) {
this.redisTemplate.delete(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + verificationToken);
return VerificationCodeConstant.VERIFICATION_CODE_RIGHT;
} else {
return VerificationCodeConstant.VERIFICATION_CODE_WARING;
}
}
}
}

Loading…
Cancel
Save