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
// app.jsapp.use((ctx,next)=>{returnnext().catch((err)=>{if(err.status===401){ctx.status=401;ctx.body="Protected resource, use Authorization header to get access\n";}else{throwerr;}});});
Koa2集成 JWT 鉴权
涉及到的关键中间件:
koa-jwt
、jsonwebtoken
通过 koa-jwt 进行路由筛选
注意:如果使用
koa-jwt
的同时还使用了koa2-cors
中间件,需要将koa2-cors
中间件的安装放置在koa-jwt
上方,否则会导致复杂请求的OPTIONS
请求被鉴权干掉。统一处理 jwt 鉴权失败的路由返回
通过 jsonwebtoken 创建 token
在登录接口将
token
返回,下一次客户端只需携带Authorization: bearer [token]
请求头即可正常访问接口。The text was updated successfully, but these errors were encountered: