【问题标题】:How to encode the JWT decode token payload on client-side in angular 8?如何在 Angular 8 中对客户端的 JWT 解码令牌有效负载进行编码?
【发布时间】:2020-02-08 15:20:44
【问题描述】:

我正在使用 angular2-jwt 来解析 http.get 之后的代码。 我需要在发送 http 帖子时对正文进行编码。

let body = {
 "sub": "1234567890",
 "name": "John Doe",
 "iat": 1516239022
};

this.http.post(url, body, { headers: headers }).subscribe(res => {
       resolve(res);
     }, (err) => {
       resolve(err);
});

【问题讨论】:

  • 你为什么要这么做?登录后,服务器应在标头中向您发送编码的 jwt 令牌。

标签: angular typescript rxjs jwt angular8


【解决方案1】:

您无法对其进行编码,您可以使用密钥对 JWT 进行编码,这是您永远不应该向客户端公开的内容。

要解码 JWT,您必须让原始编码的 JWT 已经浮动,将其发送回服务器而不是解码的对象。

【讨论】:

    【解决方案2】:

    你想在这里使用resolve 做什么?您的 res 将包含您收到的数据

    this.http.post(url, body, { headers: headers }).subscribe(res => {
           console.log(res)//here res is your body/data
         }, (err) => {
           resolve(err);
    });
    

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 2018-06-13
      • 2018-08-06
      • 2019-06-19
      • 2016-05-29
      • 2019-10-29
      • 2021-06-01
      • 2015-09-17
      • 2017-12-04
      相关资源
      最近更新 更多