我有一个带有几个模块的应用程序,它遵循帖子中建议的体系结构。我需要知道Interceptor的项目文件夹结构中的正确配置和位置,以便使用@auth0/angular库和模块导入结构附加带有Bearer令牌的授权头。我有一个核心模块分解通用模块导入,但是我想将这种自动化的特殊特性限制在我的自定义业务逻辑模块上。预先感谢。
发布于 2020-09-09 21:32:11
import { ModuleWithProviders, NgModule } from '@angular/core';
import { LoadingComponent } from '../components/home/shared/loading/loading.component';
import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { JwtService } from '../util';
export function jwtOptionsFactory(jwtService: JwtService) {
return {
tokenGetter: () => {
return jwtService.getToken();
}
};
}
@NgModule({
imports: [
JwtModule.forRoot({
jwtOptionsProvider: {
provide: JWT_OPTIONS,
useFactory: jwtOptionsFactory,
deps: [JwtService]
}
})
],
declarations: [LoadingComponent],
exports: [JwtModule],
})
export class AuthModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: AuthModule
};
}
}将AuthModule.forRoot()添加到app.module.ts导入中
发布于 2020-09-06 06:14:15
此配置必须位于app.module.ts文件中。例如:
// other imports...
import { JwtModule } from '@auth0/angular-jwt';
export function tokenGetter(): string {
return localStorage.getItem('token');
}
@NgModule({
declarations: [
// your declarations...
],
imports: [
// your imports...
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
whitelistedDomains: ['localhost:5000'],
blacklistedRoutes: ['localhost:5000/api/auth']
}
}),
],
providers: [
// your providers...
],
bootstrap: [
AppComponent
]
})
export class AppModule { }发布于 2020-09-08 09:55:09
如果项目中有延迟加载的模块。然后,您可以使用auth0/angular库的disallowedRoutes特性。
如果不想替换特定路由的授权头,请在disallowedRoutes中列出它们。如果您的初始auth路由位于允许的域上,并且采用基本的auth标头,这将非常有用。这些路由需要以正确的协议为前缀(Http://,https://). )如果要将路由添加到不允许的路由列表中,则可以将其前缀为//。
JwtModule.forRoot({
config: {
// ...
disallowedRoutes: [
"http://localhost:3001/auth/",
"https://foo1.com/bar/",
"//foo1.com/bar/baz",
/localhost:3001\/foo\/far.*/,
], // strings and regular expressions
},
});https://stackoverflow.com/questions/59400285
复制相似问题