如何在 OTP 生成 4 小时后限制用户通过 OTP 验证手机



>我已经为验证移动创建了API,我想放一些逻辑 我可以限制在 4 小时后尝试验证 OTP 的用户。我 创建了两个 API第一个向用户发送 OTP 和输入 参数为手机号码。第二个 API 通过比较用户插入的 OTP 和第一个 API 期间存储在数据库中的 OTP 来验证该手机号码

@RestController
@RequestMapping("/api/v1")
public class MobileController2 {

private String To = null;
OtpGenerator otp = new OtpGenerator();
@Autowired
private MobileRepository mobileRepository;
Sms sms = new Sms();
Date date = new Date();
Timestamp timestamp1 = new Timestamp(date.getTime());
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

@PostMapping(value = "/mobile", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Mobile> createMobile(@RequestBody Mobile mobile) {
int hashcode = otp.RandomOtp();
this.To = mobile.getMob();
String Message = hashcode + " is your Pharmerz verification code ";
if (mobileRepository.findByUserid(mobile.getUserid()) != null) {
Mobile mobileprevious = mobileRepository.findByUserid(mobile.getUserid());
mobileprevious.setMob(mobile.getMob());
mobileprevious.setHASHCODE("" + hashcode);
mobileprevious.setUpdated(mobile.getUpdated());
mobileprevious.setVERIFIED(0);
mobileRepository.save(mobileprevious);
sms.sms_generation(To, Message);
return new ResponseEntity<Mobile>(mobileprevious, HttpStatus.OK);
} else {
mobile.setHASHCODE("" + hashcode);
mobile.setVERIFIED(0);
mobileRepository.save(mobile);
sms.sms_generation(To, Message);
return new ResponseEntity<Mobile>(mobile, HttpStatus.OK);
}
}

@PostMapping(value = "/verifymobile", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Mobile> verifyMobile(@RequestBody Mobile mobile) {
String userid = mobile.getUserid();
String userotp = mobile.getHASHCODE();
Mobile mobileobject = mobileRepository.findByUserid(userid);
if (mobileobject.getHASHCODE().equals(userotp)) {
System.out.println("Matched");
mobileobject.setHASHCODE("");
mobileobject.setVERIFIED(1);
mobileRepository.save(mobileobject);
String Acknowledge = "Thank you for verifying on Pharmerz";
sms.sms_generation(To, Acknowledge);
return new ResponseEntity<Mobile>(mobileobject, HttpStatus.OK);
} else {
System.out.println("Miss matched");
return new ResponseEntity<Mobile>(HttpStatus.BAD_REQUEST);
}
}
}

在这里给你一个不回答:了解如何编写有用的日志消息以及如何使用调试器或探查器等工具。

意思是:没有人可以从远程调试这样的问题。可能有各种各样的根本原因导致您出现这种行为。

你必须退后一步,

  • 了解将字符串"错误日志"放入错误日志中没有任何帮助。
  • 了解打印到控制台...也不是获取代码"日志"的可靠方法。特别是在三个不同的地方有相同的消息"错误或旧的 Otp"时。这就是所谓的代码重复,本身就是一种不好的做法
  • 了解如何使用工具,深入了解应用程序的运行状况

换句话说:在应用程序中记录信息的主要目标是使您能够在问题发生后对其进行调试。正是为了在这种情况下为您提供支持。

相关内容

最新更新