我正嘗試使用Ionic 3/Angular 4.3爲現有Web應用程序構建移動客戶端。在使用withCredentials的設備上未發送Ionic 3中的Cookie:true
我們使用cookie進行會話和CSRF保護。
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from "rxjs";
import { CookieService } from 'ngx-cookie';
import 'rxjs/add/operator/map';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private cookies: CookieService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
var authReq = this.cookies.get('XSRF-TOKEN') ? request.clone({
withCredentials: true,
headers: request.headers.set('X-XSRF-TOKEN', this.cookies.get('XSRF-TOKEN'))
}) : request.clone({ withCredentials: true });
return next.handle(authReq);
}
}
測試從瀏覽器的工作原理,只要細如我ionic serve address=localhost
(繞過CORS需要地址標誌)運行:我已經爲了安裝和使用HTTP請求發送令牌餅乾實行以下HttpInterceptor
。
但是,當使用ionic cordova run android
從Android設備進行測試時,Cookie似乎不會被髮送或持續存在。
從上面可以看到,我已將withCredentials
設置爲true
,因爲大多數類似帖子的回覆建議。
服務器端組件是否處理自定義請求標頭要求正確的請求前請求? – CBroe
@CBroe我已經允許在開發服務器上的所有來源,但不是在生產服務器上(我一直在閱讀,這是沒有必要的,因爲設備訪問與原始文件''') – Jaysbays
談論https:// stackoverflow .com/a/13997235/1427878 – CBroe