将req传递给jwt验证程序函数时丢失令牌。使用.all()函数

时间:2018-12-11 16:21:33

标签: node.js express jwt

var category = require('../controllers/category/category.controller')
var _verifyToken = require('../../config/verify_jwt_token')
var prefix = '/category'
module.exports = function (app) {
  app.route(prefix + '/categories').all(_verifyToken.verifyToken).get(category.getAllCategories)
}

x-access-token标题可在 getAllcategories 中使用。 get(category.getAllCategories)      控制器,但是当我将请求传递给 .all(_verifyToken.verifyToken)函数时。我丢了令牌。

 app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.setHeader(
      'Access-Control-Allow-Methods',
      'GET, POST, OPTIONS, PUT, PATCH, DELETE'
    )
    res.setHeader(
      'Access-Control-Allow-Headers',
      'X-Requested-With, content-type, x-access-token, Authorization'
    )
    res.setHeader('Access-Control-Allow-Credentials', false)
    next()
  })

只有在Angular 6中才发生这种情况。对于postman和angularJS 1,它可以正常工作

角度令牌拦截器

constructor( private _authService: AuthService) {}
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>  {
    // let authService:
    console.log('Here is the token interceptor');
    // const token = this._authService.getToken();
    // console.log(token);
    const tokenizedReq = req.clone({
      setHeaders: {
        'Content-Type': 'application/json"',
        'x-access-token': `${this._authService.getToken()}`
      }
    });
    return next.handle(tokenizedReq);
  }

角度请求

_getAllCategoriesUrl = environment.APIEndPoint + '/category/categories';
  getCategories() {
    return this.http.get<any>(this._getAllCategoriesUrl);
  }

0 个答案:

没有答案