最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

通过Angular 4拦截器

IT培训 admin 2浏览 0评论

通过Angular 4拦截器

我正在使用登录构建应用程序。

用户可以登录并将令牌返回到前端。

当我将令牌发送回服务器进行验证时,问题就出现了。

我正在使用HttpInterceptor将Authorization标头发送到服务器。

当我登录控制台时:

  console.log(cloned.headers.get("Authorization"))

但是,当登录快速中间件时,标题显示正常

console.log(req.headers.authorization);

我得到undefined

以下是来自网络选项卡的请求标头,如您所见,没有授权

拦截器:

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const id_token = localStorage.getItem("id_token");

    if (id_token) {
      const cloned = req.clone({
        headers: req.headers.set("Authorization", "Bearer " + id_token)
      });
      // console.log(cloned)
      console.log(cloned.headers.get("Authorization"))
      return next.handle(cloned);
    }
    else {
      return next.handle(req);
    }
  }
}

表达:

router.use('/edit', function(req, res, next) {
  console.log(req.headers.authorization);
  const checkIfAuthenticated = expressJwt({
    secret: RSA_PUBLIC_KEY
  });

  if (checkIfAuthenticated) {
    return res.status(401).json({
      title: 'Not Authorised',
      error: 'You are not authorised'
    });
  }
})
回答如下:

默认情况下隐藏Authorization-header。在你的后端你需要公开标题:

Access-Control-Expose-Headers: Authorization

请参阅此主题作为参考:

Angular 2: Get Authorization header

@Update:对不起,误解了你的问题。请重新检查你的第二个console.log()。你正在记录:

console.log(req.headers.authorization);

不应该这样

console.log(req.headers.get("Authorization"));

代替?

通过Angular 4拦截器

我正在使用登录构建应用程序。

用户可以登录并将令牌返回到前端。

当我将令牌发送回服务器进行验证时,问题就出现了。

我正在使用HttpInterceptor将Authorization标头发送到服务器。

当我登录控制台时:

  console.log(cloned.headers.get("Authorization"))

但是,当登录快速中间件时,标题显示正常

console.log(req.headers.authorization);

我得到undefined

以下是来自网络选项卡的请求标头,如您所见,没有授权

拦截器:

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const id_token = localStorage.getItem("id_token");

    if (id_token) {
      const cloned = req.clone({
        headers: req.headers.set("Authorization", "Bearer " + id_token)
      });
      // console.log(cloned)
      console.log(cloned.headers.get("Authorization"))
      return next.handle(cloned);
    }
    else {
      return next.handle(req);
    }
  }
}

表达:

router.use('/edit', function(req, res, next) {
  console.log(req.headers.authorization);
  const checkIfAuthenticated = expressJwt({
    secret: RSA_PUBLIC_KEY
  });

  if (checkIfAuthenticated) {
    return res.status(401).json({
      title: 'Not Authorised',
      error: 'You are not authorised'
    });
  }
})
回答如下:

默认情况下隐藏Authorization-header。在你的后端你需要公开标题:

Access-Control-Expose-Headers: Authorization

请参阅此主题作为参考:

Angular 2: Get Authorization header

@Update:对不起,误解了你的问题。请重新检查你的第二个console.log()。你正在记录:

console.log(req.headers.authorization);

不应该这样

console.log(req.headers.get("Authorization"));

代替?

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论