防止HttpClient在PUT,POST&上读取cookie。补丁

时间:2018-05-31 10:05:28

标签: angular typescript cookies angular-httpclient

如何阻止我的Angular应用尝试从HttpClient上的PUT / POST和PATCH请求中读取 document.cookie

  • 我的应用在其他网络应用中的iframe内运行,不允许访问Cookie! 我无法控制此环境/应用。
  • GET请求工作没有任何问题。
  • 我使用的是Angular 6.0.2

错误

putpost&来自patch的{​​{1}}请求会产生以下错误。

  

backend.service.ts:127 DOMException:无法读取' cookie'   来自' Document'的文件:该文件是沙箱并且没有   '允许-同源'旗。       在HttpXsrfCookieExtractor.push ../ node_modules/@angular/common/fesm5/http.js.HttpXsrfCookieExtractor.getToken   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:27596:37)       在HttpXsrfInterceptor.push ../ node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:27633:39)       在HttpInterceptorHandler.push ../ node_modules / @ angular / common / fesm5   /http.js.HttpInterceptorHandler.handle   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:27004:33)       在HttpInterceptingHandler.push ../ node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:27677:27)       在MergeMapSubscriber.project(http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:26755:184)       在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / mergeMap.js.MergeMapSubscriber._tryNext   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:110070:27)       在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / mergeMap.js.MergeMapSubscriber._next   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:110060:18)       在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:101403:18)       在Observable._subscribe(http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:104821:20)       在Observable.push ../ node_modules / rxjs / _esm5 / internal / Observable.js.Observable._trySubscribe   (http://localhost:8080/44812272_a91e_4126_ad7b_6a54454 ... _sc-lightcontrol.jar / vendor.js:100628:25)

代码

HttpClientputTest()postTest()因上述例外而失败 patchTest()有效。

getTest()

1 个答案:

答案 0 :(得分:0)

如果我为传出请求禁用XSRF protection support,PUT / POST和PATCH请求会起作用,默认情况下会启用该传递请求并尝试读取cookie XSRF-TOKEN。

@NgModule({
...,
imports: [
  HttpClientModule,
  HttpClientXsrfModule.disable(),
],
...
})

HttpClientXsrfModule.disable()