【问题标题】:Protect only certain routes with koa-jwt使用 koa-jwt 仅保护某些路由
【发布时间】: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 添加到该特定路由器。你试过吗?
  • 嗨摩尔达!我这样做了,但在错误的地方。现在我弄清楚我必须把它放在哪里。昨晚太晚了。非常感谢您的评论。

标签: node.js jwt koa


【解决方案1】:

这就是答案:

我必须把它放在我声明 protectedRoutes 的文件中

protectedRoutes.use(jwt({ secret: '我的密钥' }))

感谢和抱歉,如果它这么明显。

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 2017-11-15
    • 2021-03-17
    • 2021-07-19
    • 2021-07-28
    • 2019-01-25
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多