gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:57:00 26 4
gpt4 key购买 nike

I have created Api for Verify mobile and i want to put some logic so that i can restrict the user who try to verify otp after 4 hours. I have created two Apis first one send otp to user and the input parameter is mobile number. Second API verify that mobile number by comparing the otp inserted by user and that stored in database during first API

@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);
}
}

}

最佳答案

这里给您一个非答案:了解如何编写有用的日志消息以及如何使用 debuggers 等工具或profilers .

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

必须退后一步

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

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

关于java - 如何限制用户在 OTP 生成 4 小时后通过 OTP 验证手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43977921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com