angular - 防止 HttpClient 在 PUT、POST 和 PATCH 上读取 cookie
问题描述
如何防止我的 Angular 应用程序尝试读取来自 HttpClient 的 PUT / POST 和 PATCH 请求上的document.cookie ?
- 我的应用程序
iframe
在另一个不允许访问 cookie 的 Web 应用程序中运行!
我无法控制这个环境/应用程序。 - GET 请求工作没有任何问题。
- 我正在使用 Angular 6.0.2
错误
put
, post
&patch
请求HttpClient
产生以下错误。
backend.service.ts:127 DOMException:无法从“文档”读取“cookie”属性:文档已被沙盒化,并且缺少“允许同源”标志。在 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:102)在 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)
代码
putTest()
,postTest()
并patchTest()
因上述异常而失败。
getTest()
作品。
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: false
};
@Injectable({ providedIn: 'root' })
export class BackendService {
constructor(
private http: HttpClient,
private messageService: MessageService
) { }
putTest(): Observable<any> {
console.log('PUT test');
return this.http.put(BackendUrl.updateDeviceToControl, mockDevicePropertyData, httpOptions)
.pipe(
tap(_ => console.log('Success')),
catchError(this.handleError<any>('PUT test'))
);
}
patchTest(): Observable<any> {
console.log('PATCH test');
return this.http.patch(BackendUrl.updateDeviceToControl, mockDevicePropertyData, httpOptions)
.pipe(
tap(_ => console.log('Success')),
catchError(this.handleError<any>('PATCH test'))
);
}
postTest(): Observable<any> {
console.log('POST test');
return this.http.post(BackendUrl.updateDeviceToControl, mockDevicePropertyData, httpOptions)
.pipe(
tap(_ => console.log('Success')),
catchError(this.handleError<any>('POST test'))
);
}
getTest(): Observable<any> {
console.log('GET test');
return this.http.get(BackendUrl.updateDeviceToControl)
.pipe(
tap(_ => console.log('Success')),
catchError(this.handleError<any>('GET test'))
);
}
}
解决方案
如果我禁用对传出请求的XSRF 保护支持, Put / Post & Patch 请求将起作用,默认情况下启用并尝试读取 cookie XSRF-TOKEN
。
@NgModule({
imports: [
HttpClientModule,
HttpClientXsrfModule.disable(),
]
})
推荐阅读
- video - 使用 gstreamer 对 H.265 进行逐行扫描
- c - 子进程死亡后父进程不从 FIFO 中读取
- python - 从 Heroku 上托管的 Django 应用程序连接到 Mongodb
- terminal - 如何在 Ubuntu 上启动 Vivado?
- python - 在用户想要的任何时候按 Esc 关闭程序的最佳方法是什么?
- python - 在Python中通过Opencv检测梯形、菱形、正方形、四边形、平行四边形
- forms - 网页辅助功能 - 控制提交按钮不在
- jquery - 关闭后无法打开模态框 - Jquery
- vb.net - 将选定行绑定到新数据行并使用导入行 VB.NET
- python - 已解决:在 Numba 中使用全局变量的替代方法