【发布时间】:2020-07-04 13:09:09
【问题描述】:
我的 API 服务器在 Lumen 框架上运行,并且我在那里安装了一个 CORS 中间件,并附带一个管理 JSON POST 数据的中间件。在服务器端,一切都很完美。
现在,对于 UI,我使用 Angular 9 和 Material 布局。我有一个非常简单的 login 组件:它基本上验证输入,然后调用与我的 API 服务器通信的服务 AuthService。
AuthService 非常简单:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject, Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { HttpClient, HttpHeaders, HttpErrorResponse, HttpParams } from '@angular/common/http';
export interface Token {
token: string;
token_type: string;
expires_in: number;
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
public isAuthenticated = new BehaviorSubject<boolean>(false);
constructor(
private http: HttpClient,
private router: Router
) {}
async checkAuthenticated() {
const authToken = localStorage.getItem('access_token');
return (authToken !== null);
}
getToken() {
return localStorage.getItem('access_token');
}
async login(username: string, password: string) {
const api = 'http://api.elektron.test/v1/login';
const headers = new HttpHeaders({'Content-Type': 'application/json; charset=UTF-8'});
const body = JSON.stringify({
email: username,
password
});
// tslint:disable-next-line:max-line-length
return this.http.post(api, body)
.subscribe((res: any) => {
if(res.token) {
this.isAuthenticated.next(true);
localStorage.setItem('access_token', res.token);
}
}, error => {
console.error(error);
}, () => {
console.log('etstamente');
});
}
async logout(redirect: string) {
const removeToken = localStorage.removeItem('access_token');
if (removeToken == null) {
this.isAuthenticated.next(false);
this.router.navigate([redirect]);
}
}
handleError(error: HttpErrorResponse) {
let msg = '';
if (error.error instanceof ErrorEvent) {
// client-side error
msg = error.error.message;
} else {
// server-side error
msg = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
return throwError(msg);
}
}
在this.http.post 中,我可以在第三个参数中传递额外的标头,我已经尝试过了,但是我面临的问题是,无论我传入哪个标头,当我调用该请求时,@987654323 @ 永远不会发送。
我尝试的另一个想法是使用拦截器:
import {HttpInterceptor, HttpRequest, HttpHandler, HttpHeaders} from '@angular/common/http';
import { AuthService } from './auth.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) { }
intercept(req: HttpRequest<any>, next: HttpHandler) {
const token = this.authService.getToken();
req = req.clone({
responseType: 'json'
});
if (token) {
req = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + token) });
}
if (!req.headers.has('Content-Type')) {
req = req.clone({
setHeaders: {
'Content-Type': 'application/json',
}
});
}
// setting the accept header
req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
return next.handle(req);
}
}
当它到达那里的任何 req.clone 语句时,我最终的行为与之前对 POST 请求的解释相同。
所以,我不知道我在这里做错了什么或导致这种情况的原因。在 Chrome 浏览器中,当我尝试查看请求标头时,在网络选项卡下的控制台中,当应用上述这些情况时,它会声明 显示临时标头 - 我找到了一些 Stack Overflow 答案在那个问题上,但他们都没有解决我的问题。
我花了 5-6 个小时在网上搜索解决方案,尝试了一些我的想法,但都无济于事。
编辑:此问题与 Angular 无关,而与服务器和后端配置以及处理预检请求有关。
【问题讨论】:
标签: angular http-headers lumen preflight http-options-method