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
If you want to maintain the behavior of ExtractJwt.fromAuthHeader() when switching to v3.3.0, simply replace it with ExtractJwt.fromAuthHeaderWithScheme('jwt') in your implementation.
关于jwt, 见: #106
关于passport-local见#126
关于curl见: #107
passport-jwt是最没必要使用的一个中间件, 直接使用jsonwebtoken 或者使用passport-local + jsonwebtoken实现即清楚又简单.
这里是一篇单用jsonwebtoken的教程: https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens
折腾的笔记, 备份.
passport-jwt定义
关于passport-jwt, 官方定义如下:
A Passport strategy for authenticating with a JSON Web Token.
This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.
用法
生成Token: indexRoutes.js
定义Strategy: passport.jwt.js
使用Strategy: server-app.js
解释说明
POST /login的时候和passport-local不同, 不需要passport参与 直接取username/password经DB验证成功则响应token, 否则响应401
请求API的时候, passport-jwt参与取request header中的Authorization信息, 解密成功则next(null, jwt)将请求交给实际的API调用方法, 解密失败则done(null, false), 直接响应401给client.
passport-jwt和passport-local用法上的区别
passport-jwt:
a.
在POST /login时生成tokenb.
在GET /api/v1时使用strategy验证请求中的token是否有效.passport-local:
a.
在POST /login时使用strategy验证请求中的user/pass是否有效(有效则调用serializeUser, 还有自定义的callback.b.
在GET /api/v1时调用deserializeUser, 通过req.isAuthenticated()判断用户是否登录.passport-local中的callback:
另一种更简单的方式是使用: https://github.com/auth0/express-jwt
passport-jwt 2.x和3.x的区别
If you want to maintain the behavior of
ExtractJwt.fromAuthHeader()
when switching to v3.3.0, simply replace it withExtractJwt.fromAuthHeaderWithScheme('jwt')
in your implementation.References
The text was updated successfully, but these errors were encountered: