每次发送 http 请求时,如何在 header 上包含身份验证令牌?
how to include auth token on header everytime http request send?
我用的是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"}
我在我的页面上测试从需要身份验证的服务器检索数据,但发送到服务器的 header 似乎没有令牌。
this.httpClient.get<User[]>(this.apiUrl+'user/list');
这是我从浏览器
复制的header
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
我在邮递员中测试了令牌值,它是有效的并且有效。
但是每次我做 http 请求时如何告诉 nebular 发送令牌?
我阅读了它应该自动完成的文档。我错过了什么吗?
找到解决方案:
在 app.module.js 我需要在 NB_AUTH_TOKEN_INTERCEPTOR_FILTER
return false
我希望他们把这个放在文档中
providers: [AuthGuard,
{ provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER, useValue: function () { return false; }, },
{ provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true },]
我用的是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"}
我在我的页面上测试从需要身份验证的服务器检索数据,但发送到服务器的 header 似乎没有令牌。
this.httpClient.get<User[]>(this.apiUrl+'user/list');
这是我从浏览器
复制的headerGET /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
我在邮递员中测试了令牌值,它是有效的并且有效。 但是每次我做 http 请求时如何告诉 nebular 发送令牌? 我阅读了它应该自动完成的文档。我错过了什么吗?
找到解决方案:
在 app.module.js 我需要在 NB_AUTH_TOKEN_INTERCEPTOR_FILTER
return false我希望他们把这个放在文档中
providers: [AuthGuard,
{ provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER, useValue: function () { return false; }, },
{ provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true },]