【问题标题】:Express gateway jwt return UnauthorizedExpress 网关 jwt 返回 Unauthorized
【发布时间】:2019-12-05 15:04:33
【问题描述】:

我正在快速网关上尝试 jwt。但是从配置 gateway.config.yml 来看是符合文档的。但是,那总是未经授权返回。 我的 gateway.config.yml:

http:
  port: 8080
apiEndpoints:
  crudAPI:
    host: localhost
    paths:
      - '/users/get-user-data'
      - '/users/delete-user-data'
      - '/users/add-user-data'
      - '/users/get-one-user-data/*'
      - '/users/update-user-data'
      - '/users/update-pass-user-data'
serviceEndpoints:
  crudService:
    url: 'http://localhost:3004'
policies:
  - proxy
  - log
  - jwt
pipelines:
  crud:
    apiEndpoints:
      - crudAPI
    policies:
      - log:
        - action:
            message: "header===> ${req.headers.authorization}"
      - jwt:
        - action:
            secretOrPublicKey: 'secretAuth'
            checkCredentialExistence: false
            # passThrough: true
      - proxy:
        - action:
            serviceEndpoint: crudService

如果 passThrough 设置为 true,它的工作正常。 出问题了?

【问题讨论】:

  • 嘿,您在此处提供的内容不足以提供任何建议/建议。您至少需要展示您是如何向网关发出请求的,以及您如何生成用户和您尝试对其进行身份验证的应用程序!
  • 对不起,这是我对邮递员的要求 passThrough: false link_1 。这是我对邮递员的要求,当 passThrough: true link_2

标签: node.js jwt express-gateway


【解决方案1】:

这在 EG 中运行良好。我在后端 API 上只犯了一个 JWT 错误。感谢您抽出宝贵时间调查此案。我非常感谢与 EG 合作。

验证 JWT 时我的后端 API:

// JSON WEB TOKEN STRATEGY
passport.use(new JwtStrategy({
    // jwtFromRequest: ExtractJwt.fromHeader('authorization'), // WRONG
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),  // CORRECT
    secretOrKey: config.JWT_SECRET
}, async (payload, done) => {
    try {
        // find user specified in token
        const user = await User.findById(payload.sub);

        // handle if user doesnt exist
        if(!user) {
            return done(null, false);
        }

        // return the user
        done(null, user);
    } catch (error) {
        done(error, false);
    }
}));

【讨论】:

  • 太棒了 - 很高兴看到你能够让它发挥作用。
猜你喜欢
  • 2015-03-13
  • 2019-11-07
  • 2021-10-11
  • 2021-08-03
  • 2020-08-28
  • 1970-01-01
  • 2018-01-26
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多