过期时间增加30秒缓冲时间

This commit is contained in:
liyunjia 2025-01-05 19:18:33 +08:00
parent 4b2744de4e
commit a99589283a
2 changed files with 3 additions and 2 deletions

View File

@ -90,7 +90,7 @@ impl GoogleSocial {
&self, &self,
) -> SocialResult<HashMap<String, GooglePublicKey>> { ) -> SocialResult<HashMap<String, GooglePublicKey>> {
let mut public_keys = self.google_public_keys.lock().await; let mut public_keys = self.google_public_keys.lock().await;
if public_keys.keys.is_empty() || public_keys.refresh_at < Utc::now().timestamp() { if public_keys.keys.is_empty() || public_keys.refresh_at < (Utc::now().timestamp() - 30) {
let response = Client::new().get(GOOGLE_PUBLIC_CERT_URL).send().await?; let response = Client::new().get(GOOGLE_PUBLIC_CERT_URL).send().await?;
let mut google_keys: GooglePublicKeys = response.json().await?; let mut google_keys: GooglePublicKeys = response.json().await?;
tracing::info!("Google公钥获取成功, {:?}", google_keys); tracing::info!("Google公钥获取成功, {:?}", google_keys);

View File

@ -99,7 +99,8 @@ impl WechatSocial {
pub async fn get_access_token(&self) -> SocialResult<WeChatAccessToken> { pub async fn get_access_token(&self) -> SocialResult<WeChatAccessToken> {
let mut wechat_access_token = self.access_token.lock().await; let mut wechat_access_token = self.access_token.lock().await;
if wechat_access_token.access_token.is_empty() if wechat_access_token.access_token.is_empty()
&& wechat_access_token.expires_in < Utc::now().timestamp() // 缓存的access_token过期了为了防止token已经接近过期时间所以这里加了30秒的缓冲时间
&& wechat_access_token.expires_in < (Utc::now().timestamp() - 30)
{ {
let response = reqwest::get(format!( let response = reqwest::get(format!(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}",