gpt4 book ai didi

angular - 删除一些 http 调用的授权 header Angular

转载 作者:行者123 更新时间:2023-12-03 13:50:54 25 4
gpt4 key购买 nike

我是 angular 的新手,正在尝试设置 HTTP 授权 header 。到目前为止,如果 token 有效,我可以为所有 API 设置授权 header 。我想要的是仅为某些 API 设置 header ,即使 token 可用。

app.module.ts

@NgModule({
declarations: [AppComponent],
imports: [
HttpClientModule
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
]
})

jwt.interceptor.ts
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';

import { AuthenticationService } from '@/_services';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
const currentUser = this.authenticationService.currentUserValue;
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}

return next.handle(request);
}
}

主页-http.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class HomeHttpService {
constructor(private http: HttpClient) { }

getAll(url: string, paramsVal?: any): Observable<any[]> {
const options = {params: paramsVal};
return this.http.get<any[]>(url, options);
}

public getByID(url: string, id: number | string): Observable<any> {
return this.http.get<any>(`${url}/${id}`);
}

public delete(url: string): Observable<any> {
return this.http.delete<any>(url).pipe(
map(response => {
return response;
})
);
}

public post(data = [], url: string): Observable<any> {
return this.http.post<any>(url, data);
}

public getExternal(url: string): Observable<any> {
return this.http.get<any>(url);
}

public put(data: any, url: string): Observable<any> {
return this.http.put<any>(url, data);
}
}

home.service.ts
import { Injectable } from '@angular/core';

import { HomeHttpService } from '../home-http.service';
import { Observable } from 'rxjs';

@Injectable()
export class HomePageService {
constructor(private apiservice: HomeHttpService) {}
private basePath = `${url}`;

getAlldata(data): Observable<any> {
return this.apiservice.getAll(this.basePath, data);
}

如何设置我的代码,以便对于某些 API,我可以删除某些 API 的授权 header

最佳答案

您可以使用 request.url属性来过滤你需要的东西。
最简单的方法之一:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
const currentUser = this.authenticationService.currentUserValue;
if (request.url.indexOf('some APIs path') === 0 && currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}

return next.handle(request);
}

关于angular - 删除一些 http 调用的授权 header Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com