首页 > php > JWT 避坑指南:nbf 验签失效问题的解决
2021
11-14

JWT 避坑指南:nbf 验签失效问题的解决

背景


最近sentry中有一个lumen项目经常收到一个报错,如下:


Not Before (nbf) timestamp cannot be in the future



如果隔几秒再请求(例如使用 Chrome 开发者工具中的 Replay XHR),就会成功。


nbf 字段的原理

查看上面的报错信息,会发现有一个 nbf,nbf 是 JWT 协议中的一个字段,是 Not Before 的缩写,表示 JWT Token 在这个时间之前是无效的,一般来讲会设置成签发的时间。这里产生了一个猜想,多服务器环境时候,服务器之间时间如果不一致,一台服务器签发的 token 如果立刻被发往另一台服务器验证,就很容易产生 nbf 字段验证不通过的问题。其实 JWT 协议已经考虑到了这类问题,所以协议中在 nbf 这一节专门提到了可以使用一个 small leeway 来解决这个问题。

4.1.5. "nbf" (Not Before) Claim

The "nbf" (not before) claim identifies the time before which the JWT 
MUST NOT be accepted for processing. The processing of the "nbf" claim 
requires that the current date/time MUST be after or equal to the 
not-before date/time listed in the "nbf" claim. Implementers MAY provide
 for some small leeway, usually no more than a few minutes, to account 
for clock skew. Its value MUST be a number containing a NumericDate 
value. Use of this claim is OPTIONAL.

也就说是因为多台服务器时间不一致导致的


解决问题

配置 Linux 自动时间同步,具体可以参考以前写的这篇文章: https://www.phpmianshi.com/?id=3758


本文》有 0 条评论

留下一个回复