我已经阅读了the docs篇以及所有相关的问题,但Angular的XSRF机制对我来说仍然不起作用:我无法在自动附加X-XSRF-TOKEN头的情况下发出POST请求.
我有一个带登录表单的Angular 6应用程序.
它是Symfony(PHP7.1)网站的一部分,当Symfony提供Angular应用程序页面时,会发送正确的Cookie(XSRF-TOKEN
):
我的apply.单元ts包括正确的模块:
// other imports...
import {HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";
// ...
@NgModule({
declarations: [
// ...
],
imports: [
NgbModule.forRoot(),
BrowserModule,
// ...
HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',
headerName: 'X-CSRF-TOKEN'
}),
// other imports
],
providers: [],
entryComponents: [WarningDialog],
bootstrap: [AppComponent]
})
export class AppModule {
}
然后,在服务的方法中,我发出以下http请求(this.http
是HttpClient
的一个实例):
this.http
.post<any>('api/login', {'_username': username, '_pass': password})
.subscribe(/* handler here */);
post请求从不发送X-XSRF-TOKEN头.为什么?