【问题标题】:The key 'security.user' is not bound to any value in context application密钥“security.user”未绑定到上下文应用程序中的任何值
【发布时间】:2021-10-31 13:50:56
【问题描述】:

我是 loopback 4 的新手。目前,我正在尝试在我的 loopback 4 项目中进行身份验证和授权。我使用https://github.com/HrithikMittal/loopback4-authorization 示例作为参考。我能够成功生成 JWT 令牌。但是我在授权部分遇到了一些问题。我添加了一个名为“Manager”的新角色,该角色具有获取“UserFeedbackCount”的规定。经理能够成功登录。但授权失败并出现错误“security.user”未绑定到上下文应用程序中的任何值。请帮帮我

谢谢

【问题讨论】:

    标签: authentication authorization loopback loopback4


    【解决方案1】:

    Loopback 文档在逐步进行身份验证和授权方面并不是很有帮助。

    我在这里有一个使用 Firebase 进行 JWT 身份验证的示例项目。您可以阅读分步说明here

    我计划再一步一步地进行授权,希望能尽快完成。现在,我可以向您指出如何前进。如文档中所述,您需要创建一个实现Provider<Authorizer> 接口的类

    // services/AuthorizationService.ts
    
    @injectable({scope: BindingScope.TRANSIENT})
    class AuthorizationService implements Provider<Authorizer>{
      // This returns the value to the callee
      value (): Authorizer {
        return this.authorize.bind(this);
      }
    
      // Implement authorization here
      async authorize (
        context: AuthorizationContext,
        metadata: AuthorizationMetadata,
      ) {
         // TODO implement authorization here
      }
    }
    

    然后,您可以在 application.ts 构造函数中将该类绑定到授权提供程序密钥。

    // Add this at the bottom of the application.ts constructor
     // bind authorization component to the options and tell which
        // class is the authorization provider
        // then need to tag it as AUTHORIZER
        this.configure(AuthorizationBindings.COMPONENT).to(authorizationOptions);
        this.component(AuthorizationComponent);
        this.bind('authorizationProviders.my-authorizer-provider')
          .toProvider(AuthorizationService)
          .tag(AuthorizationTags.AUTHORIZER);
    

    对于实际授权服务,您可以推出自己的或使用 Casbin 作为他们文档中提到的 Loopback。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2021-06-26
      • 2021-10-24
      • 2021-04-11
      • 2021-01-04
      相关资源
      最近更新 更多