SAP Spartacus UI 通过 HTTP Interceptor 给请求添加 Authorization 字段的源代码分析

简介: SAP Spartacus UI 通过 HTTP Interceptor 给请求添加 Authorization 字段的源代码分析

Angular HTTP Interceptor 的几种使用场景之中,最常见的就是为 outbound HTTP 请求,添加 Authorization 头部字段。


下面这段示例应用程序有一个 AuthService,它产生一个 Authorization token。在AuthInterceptor 中注入该服务以获取 token,并向每个传出请求添加带有该 token 的 Authorization header:


import { AuthService } from '../auth.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(private auth: AuthService) {}
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // Get the auth token from the service.
    const authToken = this.auth.getAuthorizationToken();
    // Clone the request and replace the original headers with
    // cloned headers, updated with the authorization.
    const authReq = req.clone({
      headers: req.headers.set('Authorization', authToken)
    });
    // send cloned request with header to the next handler.
    return next.handle(authReq);
  }
}


当然,上面先 clone 请求,再设置字段,有更简洁的写法:


// Clone the request and set the new header in one step.
const authReq = req.clone({ setHeaders: { Authorization: authToken } });

SAP Spartacus 的 Angular 代码中也采取了上述的思路来实现。


一个例子:AuthHttpHeaderService


这个类是 AuthInterceptor 的一个 service 类:



我们查看 alterRequest 方法的注释,得知其作用就是给 OCC API 调用的 HTTP 请求添加 Authorization 字段。


首先调用第 147 行的方法 getAuthorizationHeader,判断当前的请求对象 request:


这个方法的实现源代码:



  protected getAuthorizationHeader(request: HttpRequest<any>): string | null {
    const rawValue = request.headers.get('Authorization');
    return rawValue;
  }


因为如果请求头部已经拥有 Authorization 字段,我们就不应该继续添加了。



isOccUrl 的实现代码:


  protected isOccUrl(url: string): boolean {
    return url.includes(this.occEndpoints.getBaseUrl());
  }


如果该请求 url 包含 OCC endpoint,并且还没有 Authorization 字段添加,则进入代码第 150 行,添加 Access Token.


方法 createAuthorizationHeader 接收的参数 token 的类型为 AuthToken,这是一个 interface:




interface 的字段列表:


export interface AuthToken {
  /**
   * Token used for `Authorization` header.
   */
  access_token: string;
  /**
   * Token to refresh the `access_token` when it expires.
   */
  refresh_token?: string;
  /**
   * Time when `access_token` expires.
   */
  expires_at?: string;
  /**
   * Scopes granted by the OAuth server.
   */
  granted_scopes?: string[];
  /**
   * Time when `access_token` was fetched from OAuth server and saved.
   */
  access_token_stored_at: string;
  /**
   * Type of the `access_token`. Most often `Bearer`.
   */
  token_type?: string;
}


可见 AuthToken 接口除了包含 Access Token 之外,还有其超时时间,以及 refresh token 的值。但对于我们当前的场景,我们仅需要关注 access_token 字段。


如下图三个关键点所示:


  • 首先检查 token 输入参数的字段 access_token,如果可用,直接返回该字段值;


  • 如果 access_token 字段不可用,从 Ngrx 这个 memory store 里读取出来,赋给临时变量 currentToken


  • 将 currentToken 的值返回给调用者




相关文章
|
27天前
|
网络架构 开发者 UED
Spartacus 2211 的 provideOutlet 方法扩展 UI
Spartacus 2211 的 provideOutlet 方法扩展 UI
10 0
Spartacus 2211 的 provideOutlet 方法扩展 UI
|
4月前
|
Web App开发 前端开发 JavaScript
使用 Chrome 开发者工具分析 UI5 Web 应用的性能
使用 Chrome 开发者工具分析 UI5 Web 应用的性能
33 0
|
4月前
|
JSON 开发者 数据格式
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
26 0
|
4月前
|
搜索推荐 开发者 UED
关于 SAP Spartacus 层的 UI 设计
关于 SAP Spartacus 层的 UI 设计
40 0
|
4月前
|
开发者 UED
SAP Spartacus BREAKPOINT 枚举类型在 Spartacus layout 实现中的作用
SAP Spartacus BREAKPOINT 枚举类型在 Spartacus layout 实现中的作用
24 0
|
6月前
|
API
什么是 HTTP 响应字段里的 Referrer Policy
什么是 HTTP 响应字段里的 Referrer Policy
19 0
|
6月前
|
JavaScript 中间件 网络安全
HTTP 响应字段 strict-origin-when-cross-origin 的含义介绍
HTTP 响应字段 strict-origin-when-cross-origin 的含义介绍
281 0
|
6月前
|
UED
HTTP 响应字段 Transfer-Encoding 赋值成 chunked 的作用介绍
HTTP 响应字段 Transfer-Encoding 赋值成 chunked 的作用介绍
54 0
|
6月前
|
存储 移动开发 算法
HTTP 响应字段 Transfer-Encoding 的作用介绍
HTTP 响应字段 Transfer-Encoding 的作用介绍
68 1
|
7月前
|
Web App开发 开发者
因为 SAP UI5 版本升级引起的问题又一例 - 如何分析问题根源试读版
因为 SAP UI5 版本升级引起的问题又一例 - 如何分析问题根源试读版
44 0