【发布时间】:2020-07-23 06:54:10
【问题描述】:
我无法弄清楚如何使用 koa-jwt 仅保护某些路由。
我不想这样做,因为我永远不会收到 404 错误:
this.app.use(unprotectedRoutes.routes())
this.app.use(jwt({ secret: 'my secret key' }))
this.app.use(protectedRoutes.routes())
unprotectedRoutes 中不存在的路由会通过 throw jwt,我会收到 Authenticated 错误。
我想要做的是 jwt 只适用于 protectedRoutes
提前致谢!
【问题讨论】:
-
假设
protectedRoutes是一个路由器实例,您应该能够使用protectedRoutes.use(jwt({ secret: 'my secret key' }))仅将 jwt 添加到该特定路由器。你试过吗? -
嗨摩尔达!我这样做了,但在错误的地方。现在我弄清楚我必须把它放在哪里。昨晚太晚了。非常感谢您的评论。