【问题标题】:How to have Aurelia child routers with their own authorize step如何让 Aurelia 子路由器具有自己的授权步骤
【发布时间】:2017-04-27 10:18:58
【问题描述】:

我有两个子路由器,一个用于管理员访问,一个用于公共访问 公共访问路由添加了 AuthorizeStep 并检查用户是否已登录。

管理员访问使用不同的身份验证,因此要检查管理员用户是否经过身份验证,我需要进行不同的检查。

我的 app.ts 带有“父”路由:

 configureRouter(config, router) {
    config.title = 'My site';
    config.addPipelineStep('authorize', AuthorizeStep);
    config.map([
      { route: '', redirect: 'Public' },
      { route: 'Public', moduleId: 'public/views/home', name:'public', title: 'Public Portal - Home' },
      { route: 'Admin', moduleId: 'admin/home', name:'admin', title: 'Admin - Home' }
    ]);

    this.router = router;
  }
}

公共子路由器看起来像:

configureRouter(config, router) {
    config.map([
        { route: '', moduleId: './home', name: 'home', title: 'Home', auth: true },
        { route: 'login', moduleId: './login', name: 'login', title: 'Login' },
        { route: 'page2/:id', moduleId: './page2', name: 'page2', title: 'Page 2', auth: true }
    ]);

    this.router = router;
 }

主页和页面 2 路由在哪里进行身份验证。

管理子路由器看起来像:

configureRouter(config, router) {
        config.map([
            { route: 'page1', moduleId: './page1/home', name: 'page1', title: 'page 1', auth: true },
            { route: 'page2', moduleId: './page2/home', name: 'page2', title: 'page 2', auth: true }
        ]);

        this.router = router;
  }

只有经过身份验证的路由。 我希望能够做类似的事情:

configureRouter(config, router) {
        config.addPipelineStep('authorize', AdminAuthorizeStep);
        config.map([
            { route: 'page1', moduleId: './page1/home', name: 'page1', title: 'page 1', auth: true },
            { route: 'page2', moduleId: './page2/home', name: 'page2', title: 'page 2', auth: true }
        ]);

        this.router = router;
  }

addPipelineStep 发生在子路由器而不是父路由器上的位置,但这不起作用,或者我无法使其工作,甚至可能吗?还是我只能有一个 AuthorizeStep 并在其中处理逻辑以确定它是访问路由的管理员还是公共用户?

【问题讨论】:

  • 定义“不起作用”,
  • 页面根本不渲染,只是一个空白页面,好像路由中断了(控制台没有错误)
  • 如果您将日志记录设置设置为开发模式 - 控制台不会出现错误就不会失败
  • 我添加了:aurelia.use .standardConfiguration() .developmentLogging() 我仍然没有在控制台中收到任何错误,(我确实像以前一样收到了所有调试消息)
  • AdminAuthorizeStep 定义在哪里?

标签: authentication authorization aurelia authorize


【解决方案1】:

这对于子路由器来说似乎是不可能的。在 aurelia-router.js 中:

if (pipelineSteps.length) {
  if (!router.isRoot) {
    throw new Error('Pipeline steps can only be added to the root router');
  }
...

我确实在控制台中收到错误: ERROR [app-router] Error: Pipeline steps can only be added to the root router

【讨论】:

  • 感谢您的回复。最后,为了得到我需要的东西,我在一个 AuthorizeStep 中处理了公共和管理员访问的所有逻辑。我本质上是检查正在访问的路由,然后根据它确定授权策略。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多