JWT How to bypass certain API routes and http methods

蓝咒 提交于 2019-12-11 12:48:52

问题


I can make get JSON-Web-Token to ignore paths using .unless like this.

app.use(expressJWT({secret: config.JWTSECRET}).unless({path: 
['/register', 
'/authentication',
]}));

I have a route with different HTTP methods (get, put, post, delete). I want the GET version of /events to not require a token, but the POST version of /event to require a token. Can I do this without having different routes for GET and POST etc.

/events //GET - no token required
/events //POST - token required

回答1:


If I know right, the express-jwt module is using express-unless to give you .unless method. With that, you can use a custom function to filter the request.

var filter = function(req) {return true;}
app.use(expressJWT({ secret: config.JWTSECRET}).unless(filter));

In the filter function, you can check the route (req.path) and the request type (req.method).



来源:https://stackoverflow.com/questions/35133693/jwt-how-to-bypass-certain-api-routes-and-http-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!