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

标签 java spring-boot time clickatell

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/

相关文章:

python - 在 Python 中将日期时间(例如 2016-01-01、2016-01-11)转换为整数天(例如 0、11)

java : corrupted zip file created when copy using nio

java - 如何对字符串进行哈希处理,以便在任何地方修改字符时它几乎不会改变?

java - Spring REST 文档测试中未使用自定义 Jackson 模块

string - 将json格式时间戳转换为golangs time.Time

c# - 代表时间的最佳类(class)

java - 什么时候需要初始化字符串?

Java 整数常量 - 拆箱

页面上的 Spring MVC + Thymeleaf div 未生成

java - 如何在浏览器中显示上传的图片URL