Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自动注册routes的时候中间件的时机错误 #17

Open
JeasonSun opened this issue Jun 5, 2020 · 2 comments
Open

自动注册routes的时候中间件的时机错误 #17

JeasonSun opened this issue Jun 5, 2020 · 2 comments

Comments

@JeasonSun
Copy link

JeasonSun commented Jun 5, 2020

server/src/routes/index.js中

// 注册路由中间件, 需要在注册路由地址之前使用
loginRouter.use(PrivilegeChecker.checkLogin)
loginProjectRouter.use(PrivilegeChecker.checkPrivilege)

注册中间件在注册路由地址之前使用,的确会提前检测login和privilege,但是对于应该返回404的路由,就无法正常通过了。

建议优化: 注册中间件的时机,应该注册路由地址之后,加入URL进行过滤。
代码如下:

for (let url of Object.keys(routerConfigMap)) {
    let routerConfig = routerConfigMap[url];
    if (routerConfig.needLogin) {
        // 需要登录
        loginRouter.use(url, PrivilegeChecker.checkLogin);
        if (routerConfig.needProjectId) {
            // 需要校验项目权限
            loginProjectRouter.use(url, PrivilegeChecker.checkPrivilege);
            Logger.log(`需要登录,也需要检验项目权限(Method: ${routerConfig.methodType}) =>`, url);
            registerRouterByMethod(loginProjectRouter, routerConfig, url)
        } else {
            // 不需要校验项目权限
            Logger.log(`需要登录,但不需要检验项目权限(Method: ${routerConfig.methodType}) =>`, url)
            registerRouterByMethod(loginCommonRouter, routerConfig, url)
        }
    } else {
        Logger.log(`不需要登录(Method: ${routerConfig.methodType}) =>`, url)
        // 不需要登录
        registerRouterByMethod(withoutLoginRouter, routerConfig, url)
    }
}
@alphawq alphawq pinned this issue Jul 23, 2020
@alphawq
Copy link
Contributor

alphawq commented Jul 23, 2020

404路由是在前端处理的,没有复现出404路由无法正常通过的问题,或许可以给出复现此问题的步骤

@JeasonSun
Copy link
Author

复现说明:

  1. /api/user/detail 由于已经注册该路由,规定需要登录,不需要检测项目ID,用postman请求,会返回下图,符合预期。
    image

  2. 假设我现在请求了一个没有注册过的路由,/api/abc/abc,前端在写接口的时候也常有不小心拼写错误的情况,预期应该返回404,告知该路由不存在。但是用postman请求,仍然会返回'请先登录',不符合实际预期,最好是返回404错误
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants