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。