gf-jwt icon indicating copy to clipboard operation
gf-jwt copied to clipboard

LogoutHandler黑名单缓存时间计算不正确

Open fidding opened this issue 1 year ago • 0 comments

LogoutHandler黑名单缓存时间计算不正确

image


func (mw *GfJWTMiddleware) setBlacklist(ctx context.Context, token string, claims jwt.MapClaims) error {
	// The goal of MD5 is to reduce the key length.
	token, err := gmd5.EncryptString(token)

	if err != nil {
		return err
	}

	exp := int64(claims["exp"].(float64))

	// save duration time = (exp + max_refresh) - now
	duration := time.Unix(exp, 0).Add(mw.MaxRefresh).Sub(mw.TimeFunc()).Truncate(time.Second)

	key := mw.BlacklistPrefix + token
	// global gcache
	err = blacklist.Set(ctx, key, true, duration)

	if err != nil {
		return err
	}

	return nil
}

其中exp是毫秒,在执行time.Unix的第一个参数单位是秒,就会导致算出来的时间多了3个0

于是写入数据库的缓存时间就会是292yr

fidding avatar Jun 21 '24 08:06 fidding