2017-05-15 59 views
0

我已經創建了驗證移動的Api,並且我想要放一些邏輯以便我可以限制嘗試在4小時後驗證otp的用戶 。我 創建了兩個apis 第一個給用戶發送otp和輸入 參數是手機號碼。 其次API由第一API如何限制用戶OTP發生4小時後通過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); 
     } 
    } 

} 
+0

對我的回答有任何反饋?我知道你可能希望我;但我很確定:沒有其他的東西會出現在這裏... – GhostCat

+0

我已經通過檢查區域時間的差異來解決此問題。 –

+0

我認爲:我的回答很有幫助,但並未真正解決您的問題? – GhostCat

回答

4

期間可由用戶插入和存儲在數據庫中的OTP比較這裏給你一個非答案驗證手機號碼:學習如何編寫有用的日誌信息和如何使用工具,如debuggersprofilers

含義:沒有人可以從遠程調試這樣的問題。可能有各種各樣的根源給你這種行爲。

必須退後一步,

  • 明白,把字符串「錯誤日誌」到你的錯誤日誌沒有幫助什麼
  • 瞭解到打印到控制檯...也不是獲得代碼的「日誌」的可靠方法。尤其是當在三個不同的地方有相同的信息「錯誤或舊的Otp」時。這就是所謂的代碼複製和本身一個壞習慣
  • 瞭解如何使用工具爲您提供關於應用程序的健康的見解。

在換句話說:首要目標的您的應用程序內錄入信息是使你他們發生後調試問題。正是在這種情況下支持你。