ktor:如何实现“pub-> sec”路由模式

我需要简化在ktor的路由配置中集成身份验证。 同时保持公共/安全通用的路由规则。

我想堆叠所有的路线如下:

  • 公共路线
  • 认证拦截器
  • 安全路线

如公共路线

POST /api/v1/story/{id}/comment GET /api/v1/story/{id} 

安全路线

 GET/PUT/POST/DELETE /api/v1/user/{id} PUT/POST/DELETE /api/v1/story/{id} 

一般来说,公共/安全路由可以基于任何东西 – 头部,方法,前缀等。

目前,我正在叶级路由节点添加身份验证拦截器。 这很麻烦,我正在寻找一个通用的方式来实现路由,如下所示:

 public routes ... authentication { // check if a public route was executed, if so, return // otherwise, verify the jwt token for security // if unauthenticated, return 401 else proceed } ... secured routes 

希望这是有道理的?

我可以在koa(Node.js)中轻松做到这一点。

谢谢,