am使用angular-jwt拦截我的请求并添加JWT令牌,但这似乎没有发生。
这是我的角度信息:
Angular CLI: 6.0.8
Node: 9.11.2
OS: linux x64
Angular: 6.0.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.1
typescript 2.7.2
webpack 4.8.3
这是我的app.module.ts
:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
whitelistedDomains:['127.0.0.1:8000'],
blacklistedRoutes: ['127.0.0.1:8000/api/v1/accounts/login/', '127.0.0.1:8000/api/v1/signup/'],
authScheme: 'JWT ',
throwNoTokenError: true
}
}),
CommonModule,
}
这是我的package.json
的一部分:
"@angular/router": "^6.0.3",
"@auth0/angular-jwt": "^2.0.0",
对不起,我忘了提到我在tokenGetter
中定义了一个app.module.ts
函数:
export function tokenGetter() {
return localStorage.getItem('token');
}
答案 0 :(得分:0)
似乎您尚未定义tokenGetter
功能。
export function tokenGetter() {
return localStorage.getItem('access_token');
}
答案 1 :(得分:0)
现在,您只需要使用Angular的HTTP客户端进行请求
import { HttpClient } from '@angular/common/http';
export class AppComponent {
constructor(public http: HttpClient) {}
ping() {
this.http
.get('http://example.com/api/things')
.subscribe(data => console.log(data), err => console.log(err));
}
}
答案 2 :(得分:0)
The reason is not found为何不发送身份验证标头,但是我们可以使用HttpInterceptor来做到这一点。
关于Angular Authentication: Using the Http Client and Http Interceptors,有一篇很棒的文章。
我们需要创建一个从TokenInterceptor
类派生的HttpInterceptor
(为此,我已经创建了特殊文件夹Interceptors
)
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent,
HttpInterceptor} from '@angular/common/http';
import { AuthService } from '../services/auth.service';
import { Observable } from 'rxjs';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
{
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken}`
}
});
return next.handle(request);
}
}
,然后有必要将以上TokenInterceptor
添加到app.module.ts
中:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule,
CommonModule,
FormsModule,
ReactiveFormsModule,
RouterModule.forRoot([
{ path: '', component: TheComponent }
]),
JwtModule.forRoot({
config: {
tokenGetter: function tokenGetter() {
console.log('tokenGetter called = ' + localStorage.getItem('authToken'));
return localStorage.getItem('authToken');
},
whitelistedDomains: ['localhost:4200'],
blacklistedRoutes: []
}
})
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
AuthService
的代码:
export class AuthService extends AbstractRestService<any> {
/* The other code is omitted for the brevity */
get getToken() {
return localStorage.getItem('authToken');
}
}