Ionic 3/Angular 4.3を使用して既存のWebアプリケーション用のモバイルクライアントを構築しようとしています。Ionic 3のCookieがwithCredentialsを使用してデバイスに送信されない:true
セッションやCSRFの保護にCookieを使用しています。私は、HTTPリクエストでトークンクッキーを添付して送信するために、次のHttpInterceptor
を実装している:
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を回避するために必要なアドレスフラグ)を実行すると正常に動作します。
ただし、ionic cordova run android
を使用してAndroid搭載端末からテストした場合、Cookieは送信されず、永続化されていないようです。
上記からわかるように、私はwithCredentials
をtrue
に設定しました。
カスタムリクエストヘッダーが正しく必要とするフライト前リクエストを処理するサーバー側コンポーネントですか? – CBroe
@CBroe私は、プロダクションサーバではなく、devサーバ上のすべての起源を許可しています(これは、 '' ''ファイル '' 'を持つデバイスがアクセスする際には必要ではないことを読んでいます) – Jaysbays
https:// stackoverflow .com/a/13997235/1427878 – CBroe