本文目錄一覽:
java jwt如何刷新過期時間
客戶端
auth_header = JWT.encode({ user_id: 123, iat: Time.now.to_i, # 指定token發布時間 exp: Time.now.to_i + 2 # 指定token過期時間為2秒後,2秒時間足夠一次HTTP請求,同時在一定程度確保上一次token過期,減少replay attack的概率;}, “my shared secret”)
RestClient.get(“”, authorization: auth_header)
服務端
class ApiController ActionController::Base
attr_reader :current_user
before_action :set_current_user_from_jwt_token
def set_current_user_from_jwt_token
# Step 1:解碼JWT,並獲取User ID,這個時候不對Token簽名進行檢查
# the signature. Note JWT tokens are *not* encrypted, but signed.
payload = JWT.decode(request.authorization, nil, false) # Step 2: 檢查該用戶是否存在於數據庫
@current_user = User.find(payload[‘user_id’])
# Step 3: 檢查Token簽名是否正確.
JWT.decode(request.authorization, current_user.api_secret)
# Step 4: 檢查 “iat” 和”exp” 以確保這個Token是在2秒內創建的.
now = Time.now.to_i if payload[‘iat’] now || payload[‘exp’] now # 如果過期則返回401
end
rescue JWT::DecodeError
# 返回 401
endend
java微信的accesstoken怎麼嬡緩竺小時更新
微信接口獲取的access token 有效期是7200秒,很簡單的方法就是你獲取到之後,緩存到redis中,設置過期時間不超過7200秒,然後每當需要使用的時候先去redis拿,如果拿到了就直接用,沒拿到就說明已經過期了,就再去微信獲取一個新的,緩存到redis中
java 如何判斷傳過來的token是否過期
簡單的就是把token放到session里,如果會話過期,token也就會過期
使用的時候只要判斷當前會話是否有效,token是否與客戶端傳過來的token等就可以了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198285.html