1

我正在快速网关上尝试 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 它的工作正常。出问题了?

4

1 回答 1

0

这在 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);
    }
}));
于 2019-12-05T06:38:05.640 回答