From 2a9fc608a1cec0899aea05359beb7aaeae80a409 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Thu, 26 Dec 2024 23:39:37 +0800 Subject: [PATCH] 1 --- app/lib/aes/check.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/lib/aes/check.go b/app/lib/aes/check.go index a428af8..675659b 100644 --- a/app/lib/aes/check.go +++ b/app/lib/aes/check.go @@ -77,7 +77,8 @@ func CheckSign(c *gin.Context) error { if err != nil { return err } - if currentTimestamp-storedTimestamp < 0 { + + if isMillisecond(storedTimestamp) { //TODO::兼容客户端传毫秒 currentTimestamp = time.Now().UnixMilli() if currentTimestamp-storedTimestamp > 60*5*1000 { // 2分钟 @@ -85,16 +86,23 @@ func CheckSign(c *gin.Context) error { fmt.Println("storedTimestamp>>>>>:", storedTimestamp) return fmt.Errorf("签名过期~") } + if currentTimestamp-storedTimestamp < -60*1000 { //避免客户端传过快时间的误差 + return errors.New("签名超前~") + } } else { if currentTimestamp-storedTimestamp > 60*5 { // 2分钟 fmt.Println("currentTimestamp>>>>>:", currentTimestamp) fmt.Println("storedTimestamp>>>>>:", storedTimestamp) return fmt.Errorf("签名过期!") } + if currentTimestamp-storedTimestamp < -60 { //避免客户端传过快时间的误差 + return errors.New("签名超前!") + } } - if currentTimestamp-storedTimestamp < 0 { - return errors.New("签名有误!") - } return nil } + +func isMillisecond(timestamp int64) bool { + return timestamp > 1e12 && timestamp < 1e13 +}