我想檢查我的 JWT 令牌是否仍然有效(exp 仍然在未來)但我不確定我是否以正確的方式做到了。我的檢查功能 -public boolean checkForValidExpField(String jwtToken) throws JsonProcessingException, IOException { //split by . String[] split_string = jwtToken.split("\\."); //get body String base64EncodedBody = split_string[1]; Base64 base64Url = new Base64(true); String body = new String(base64Url.decode(base64EncodedBody)); //body to json ObjectMapper mapper = new ObjectMapper(); JsonNode actualObj = mapper.readTree(body); //get exp String exp = actualObj.get(JWT_EXP_KEY).asText(); //JWT_EXP_KEY = exp //to long long expLong = Long.parseLong(exp) * 1000; //get current TS long currentTime = System.currentTimeMillis(); //check return expLong >= currentTime; }這是只檢查 exp 字段的正確方法嗎?我自己寫的,所以我只是想確定一下。
1 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
聲明exp
(以及其他與時間相關的聲明,如nbf
)是數字日期:
JWT文檔(RFC)定義了數字日期:
一個 JSON 數值,表示從 1970-01-01T00:00:00Z UTC 到指定的 UTC 日期/時間的秒數,忽略閏秒。
因此,您確實可以比較代碼中顯示的 long 值,或者從 exp 值中減去當前時間以獲得剩余時間。
添加回答
舉報
0/150
提交
取消