如何修复express-gateway上的HTTPS

问题描述 投票:0回答:1

我正在运行微服务API,并且使用express-gateway。该 API 已运行 1 年。我需要实现 HTTPS 连接,但我很难做到。

感谢 openssl,我已经生成了 SSL 证书。我有服务器密钥和客户端密钥。

我已经这样配置了我的网关

gateway.config.yml

http:
 port: 8000

https:
  port: 9443
  hostname: localhost
  tls:
    "default":
        key: ‘path/to/server.key'
        cert: 'path/to/server.crt'

admin:
  port: 9876
  host: localhost
apiEndpoints:
  api:
    host: '*'
    paths: '/ip'
  test:
    host: '*'
    paths: ['/test', '/test/*']
  voice:
    host: '*'
    paths: ['/voices', '/voices/*']
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  testService:
    url: 'http://localhost:6969'
  voiceService:
    url: 'http://localhost:6965'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
      - proxy:
          - action:
              serviceEndpoint: httpbin
              changeOrigin: true
  testPipeline:
    apiEndpoints:
      - test
    policies:
      - proxy:
          - action:
              serviceEndpoint: testService
              changeOrigin: true
  voicePipeline:
    apiEndpoints:
      - voice
    policies:
      - proxy:
          - action:
              serviceEndpoint: voiceService
              changeOrigin: true

这是我的

voice-service.js

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const middleware = require('./middleware/index');
const firebase = require('./config/firebase-config');
const { spawn, execSync } = require('child_process');
const fs = require('fs');

const app = express();
const port = 6965;
var jsonParser = bodyParser.json();
app.use(express.json({limit: '100mb'}));

app.use(cors());

app.use(middleware.decodeToken);

app.get('/', (req, res) => {
    res.send('Welcome to the Voice API');
})

app.get('/voices', async (req, res) => {
    ...
});

app.post('/voices', jsonParser, async (req, res) => {
    ...
});

app.delete('/voices', async (req, res) => {
    ...
});

app.listen(port, () => {
    console.log(`server is running on port ${port}`);
});

我的 API 使用 firebase 持有者令牌,并有一个中间件来检查其有效性

middleware.js
:

const admin = require('../config/firebase-config');

class Middleware {
    async decodeToken(req, res, next) {
        try {
            const token = req.headers.authorization.split(' ')[1];
            const decodeValue = await admin.admin.auth().verifyIdToken(token);
            if (decodeValue)
                return next();
            return res.json({ message: 'Invalid Token' });
        } catch (e) {
            return res.json({ message: 'Invalid Token' });
        }
    }
}
module.exports = new Middleware();

当我尝试测试 https 时,我尝试使用 Postman。我在其中添加了我的客户端 ssl 密钥和有效的不记名令牌。我试图获取“声音/”路线。使用http方法它工作正常,但是使用https它总是结果超时。

有人可以帮助我吗? 我试图尽可能详尽,但如果您需要更多信息,请询问!

https microservices express-gateway
1个回答
0
投票

您需要更新 serviceEndpoints 列表中的 testService 和 voiceService URL

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  testService:
    url: 'http://localhost:6969'
  voiceService:
    url: 'http://localhost:6965'

使用https:

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  testService:
    url: 'https://localhost:6969'
  voiceService:
    url: 'https://localhost:6965'
© www.soinside.com 2019 - 2024. All rights reserved.