栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将授权标头添加到Angular http请求?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将授权标头添加到Angular http请求?

关于在 Angular > 4中处理身份验证标头的最佳方法,最好

Http Interceptors
是将其添加到每个请求中,然后再
Guards
用于保护路由。

AuthInterceptor
是我在应用程序中使用的的完整示例:

验证拦截器

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';import { Injectable } from '@angular/core';import { Observable } from 'rxjs/Observable';import { AuthService } from './auth.service';@Injectable()export class AuthInterceptor implements HttpInterceptor {  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {    req = req.clone({      setHeaders: {        'Content-Type' : 'application/json; charset=utf-8',        'Accept'       : 'application/json',        'Authorization': `Bearer ${AuthService.getToken()}`,      },    });    return next.handle(req);  }}

您需要在中将拦截器注册

app.module
为提供程序:

app.module.ts

import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';import { AuthInterceptor } from '../auth/auth.interceptor';...imports: [    HttpClientModule,    ...],providers: [    {      provide : HTTP_INTERCEPTORS,      useClass: AuthInterceptor,      multi   : true,    },    ...],...

你可以阅读有关该方法进一步在这个岗位。


关于 Go 方面,这很可能是您发送的
请求 标头与 CORS 允许的标头不匹配的情况。
您应该尝试的第一件事是允许所有这些:

headersOk := handlers.AllowedHeaders([]string{"*"})originsOk := handlers.AllowedOrigins([]string{"*"})methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})

并且,如果问题消失了,请尝试根据客户发送的内容仔细构造您的 CORS



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号