From 94aaedf986cffec1d2ecfa968390cbb2b726f8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Thu, 23 May 2024 08:34:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9token=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/src/core/config.rs | 4 ++-- library/src/token.rs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/library/src/core/config.rs b/library/src/core/config.rs index b8408cc..df543cc 100644 --- a/library/src/core/config.rs +++ b/library/src/core/config.rs @@ -43,8 +43,8 @@ pub struct Logger { #[derive(Clone, Debug, Deserialize)] pub struct Jwt { pub secret: String, - pub expires: usize, - pub refresh_expires: usize, + pub expires: i64, + pub refresh_expires: i64, } #[macro_export] diff --git a/library/src/token.rs b/library/src/token.rs index ac7a892..cef355c 100644 --- a/library/src/token.rs +++ b/library/src/token.rs @@ -1,3 +1,4 @@ +use chrono::{TimeDelta, Utc}; use serde::{Deserialize, Serialize}; use crate::config; @@ -5,13 +6,13 @@ use crate::config; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Claims { sub: i64, // 用户ID - exp: usize, // Token过期时间戳 + exp: i64, // Token过期时间戳 } pub fn generate_token(sub: i64) -> String { let claim = Claims { sub, - exp: config!().jwt.expires, + exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.expires).unwrap()).timestamp(), }; generate(claim) } @@ -19,7 +20,7 @@ pub fn generate_token(sub: i64) -> String { pub fn generate_refresh_token(sub: i64) -> String { let claim = Claims { sub, - exp: config!().jwt.refresh_expires, + exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.refresh_expires).unwrap()).timestamp(), }; generate(claim) }