Pao*_*fan 16 csrf typescript x-xsrf-token angular angular6
我已经阅读了关于SO 的文档和所有相关问题,但是Angular的XSRF机制仍然不适合我:我绝不能自动附加X-XSRF-TOKEN标头发出POST请求.
我有一个带登录表单的Angular 6应用程序.
它是Symfony(PHP 7.1)网站的一部分,Angular应用程序页面,当从Symfony提供时,发送正确的Cookie(XSRF-TOKEN
):
我的app.module.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 {
}
Run Code Online (Sandbox Code Playgroud)
然后,在Service的方法中,我正在进行以下http请求(this.http
是一个实例HttpClient
):
this.http
.post<any>('api/login', {'_username': username, '_pass': password})
.subscribe(/* handler here */);
Run Code Online (Sandbox Code Playgroud)
post请求永远不会发送X-XSRF-TOKEN标头.为什么?
Pao*_*fan 28
问题再一次是Angular糟糕的文档.
事实是,只有在服务器端使用以下选项生成cookie时,Angular才会添加X-XSRF-TOKEN
标头:XSRF-TOKEN
/
false
(这非常重要,完全没有记录)此外,Angular应用程序和被调用的URL必须位于同一服务器上.
Leo*_*Leo 21
在我的团队中,问题是我们使用的是绝对路径而不是相对路径。
所以不要使用绝对路径,如:
this.http.post<any>("https://example.com/api/endpoint",data)
Run Code Online (Sandbox Code Playgroud)
用
this.http.post<any>("api/endpoint",data)
Run Code Online (Sandbox Code Playgroud)
或使用
this.http.post<any>("//example.com/api/endpoint",data)
Run Code Online (Sandbox Code Playgroud)
这是因为 HttpClientXsrfModule 上的 Angular 代码显式忽略了绝对路径(请参阅)
稍微偏离主题,但对于来到这里的其他人,我在后端解决了以下问题(对于spring-boot
)
/**
* CORS config - used by cors() in configure() DO NOT CHANGE the METDHO NAME
*
* @return
*/
@Bean()
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Lists.newArrayList("http://localhost:4200"));
configuration.setAllowedMethods(Lists.newArrayList("GET", "POST", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Lists.newArrayList("x-xsrf-token", "XSRF-TOKEN"));
configuration.setMaxAge(10l);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你应该把这个服务放在前端{ withCredentials: true }
前任:
this.http.put<class>(url, body, { withCredentials: true });
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17319 次 |
最近记录: |