我使用的是ngx-admin的最新版本,我已经这样修改了我的app.module.ts:
NbAuthModule.forRoot({
strategies: [
NbPasswordAuthStrategy.setup({
name: 'email',
baseEndpoint: 'https://localhost:5001',
token: {
class: NbAuthJWTToken,
key: 'token'
},
login: {
// ...
endpoint: '/api/Auth/login',
redirect: {
success: '/pages/dashboard',
failure: null, // stay on the same page
},
},
logout:{
redirect: {
success: '/auth/login',
failure: null, // stay on the same page
},
},
register: {
// ...
endpoint: '/api/Auth/register',
},
}),
],
forms: {
login: formSetting,
register: formSetting,
requestPassword: formSetting,
resetPassword: formSetting,
logout: {
redirectDelay: 0,
},
},
}), ],
bootstrap: [AppComponent],
providers: [AuthGuard,
{ provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true },]我可以从我的系统进行登录和注销,我将令牌保存到我的本地存储中,名称为app_auth_token及其包含:
{"name":"nb:auth:jwt:token","ownerStrategyName":"email","createdAt":1621867077000,"value":"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhZG1pbkBteWNhZmUuY29tIiwidW5pcXVlX25hbWUiOiJhZG1pbiIsInJvbGUiOiJBZG1pbiIsIm5iZiI6MTYyMTg2NzA3NywiZXhwIjoxNjIyMDM5ODc3LCJpYXQiOjE2MjE4NjcwNzd9.C06e6bSPhlIl16A3iTtU3yGi8078K1I5aTy-QDVIiX_cHr6-HQQAFjgQRwWuI9tuzCe5G9HC7IFSeyN_dI4BuA"}我在我的页面上测试了从服务器检索需要身份验证的数据,但似乎发送到服务器的头中没有令牌。
this.httpClient.get<User[]>(this.apiUrl+'user/list');下面是我从浏览器复制的标题
GET /api/user/list HTTP/2
Host: localhost:5001
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,id;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Origin: http://localhost:4200
DNT: 1
Connection: keep-alive
Referer: http://localhost:4200/
Pragma: no-cache
Cache-Control: no-cache我收到代码401 UnAuthorized
我在postman中测试了token值,它是有效的和有效的。但是如何在我每次发出http请求时告诉nebular发送令牌呢?我读了文档,它应该是自动完成的。我错过了什么吗?
发布于 2021-05-25 10:21:11
找到解决方案:
在app.module.js中,我需要在NB_AUTH_TOKEN_INTERCEPTOR_FILTER上返回false
我希望他们能把这个写进文件里
providers: [AuthGuard,
{ provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER, useValue: function () { return false; }, },
{ provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true },]https://stackoverflow.com/questions/67680701
复制相似问题