【问题标题】:How to restrict user to verify the mobile by OTP after 4 hours of OTPgeneration如何在 OTP 生成 4 小时后限制用户通过 OTP 验证手机
【发布时间】:2017-05-15 11:08:08
【问题描述】:

我已经为验证移动设备创建了 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);
        }
    }

}

【问题讨论】:

  • 对我的回答有任何反馈吗?我知道你可能希望我;但我很确定:这里不会出现其他任何东西......
  • 我已经通过检查区域时间的差异解决了这个问题。
  • 我认为:我的回答很有帮助,但并没有真正解决您的问题?
  • 不,你的回答真的很受欢迎,它给了我工作的道路
  • 很高兴听到这个消息......但我同意,虽然不是每个答案都必须导致 接受 ;-)

标签: java spring-boot time clickatell


【解决方案1】:

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

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

必须退后一步

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

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

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2021-01-06
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多