You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What's JSON Web Token? JSON Web Token (JWT, pronounced jot) is a relatively new token format used in space-constrained environments such as HTTP Authorization headers. JWT is architected as a method for transferring security claims based between parties.
server side 以koa为例
varkoa=require('koa');varjwt=require('koa-jwt');varapp=newKoa();// Middleware below this line is only reached if JWT token is valid // use jwt 下面的中间件 只能在token验证后才能到达// unless the URL starts with '/public' app.use(jwt({secret: 'shared-secret'}).unless({path: [/^\/public/]}));// Unprotected middleware app.use(function(ctx,next){if(ctx.url.match(/^\/public/)){ctx.body='unprotected\n';}else{returnnext();}});// Protected middleware app.use(function(ctx){if(ctx.url.match(/^\/api/)){ctx.body='protected\n';}});app.listen(3000);
Now we have the JWT saved on sessionStorage. If the token is set, we are going to set the Authorization header for every outgoing request done using $http. As value part of that header we are going to use Bearer <token>.
项目中碰到了个token失效的问题,明明有不停发心跳的.之前对token这个也是一知半解(虽然现在仍是),自己一点点看了项目中的代码才稍微清晰了一些.这次简单记录下
JSON Web Token
server side 以koa为例
项目伪代码
在login完成登录返回用户信息时可将生成的token存入Storage中.
客户端
token rolling
实际中会需要用户持续保持登录要与服务器长连接.会通过客户端每隔一段时间向服务器发送一次'心跳请求',
服务端接受到请求后会查看token过期时间.快接近token过期时间的时候,再设定新的过期时间重新生成token,将新token返回给客户端.客户端收到(监听到)新token后,用新token替换请求头
Authorization
中的token,达到token续期的目的;每次发送请求 检查
响应头
中是否有 之前服务端设置的token-rolling
(const = response.headers.get('token-rolling');
) 有就将token-rolling的值存入Storage中,下次请求就用新的token啦参考:
The text was updated successfully, but these errors were encountered: