首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Express-gateway为express microservices应用程序创建新的插件/策略

Express-gateway为express microservices应用程序创建新的插件/策略
EN

Stack Overflow用户
提问于 2020-06-03 19:27:28
回答 1查看 1.3K关注 0票数 1

我正在创建一个express微服务体系结构,并使用快递网关作为API网关。

我可以通过快速网关公开我的服务和端点,其中一个服务(Books)有两个角色(管理,用户),有两个不同的登录起点(管理员使用JWT,用户使用Firebase auth)。

我成功地将管理端点/v1/admin与快递网关提供的JWT一起安全了,现在我想创建一个策略/插件(我不明白其中的区别)来使用我的CheckIfAuthenticatedFirebase中间件来保护我的用户端点/v1/user。

因此,我需要一些帮助,以了解我是否必须创建一个插件或一个政策和步骤来做它。

这是我的gateway.config.yml

代码语言:javascript
复制
http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  bookAdmin:
    path: '/v1/admin*'
  bookUser:
    path: '/v1/user*'
serviceEndpoints:
  book:
    url: 'http://localhost:5000'
policies:
  - cors
  - log
  - proxy
  - jwt
  - request-transformer
pipelines:
  bookAdminPipeline:
    apiEndpoints:
      - bookAdmin
    policies:

      -
        cors:
          action:
            origin: '*'
            methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'   
      -
        jwt:
          action:
            secretOrPublicKey: 'JWTKey'
            checkCredentialExistence: false

      -
        proxy:
          action:
            serviceEndpoint: book
  bookUserPipeline:
    apiEndpoints:
      - bookUser
    policies:

      -
        cors:
          action:
            origin: '*'
            methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'   
      -
        proxy:
          action:
            serviceEndpoint: book

这是我的firebase-middleware.js

代码语言:javascript
复制
var admin = require('../authentication/firebase');

 getAuthToken = (req, res, next) => {
    if (
      req.headers.authorization &&
      req.headers.authorization.split(' ')[0] === 'Bearer'
    ) {
      req.authToken = req.headers.authorization.split(' ')[1];
    } else {
      req.authToken = null;
    }
    next();
  };


checkIfAuthenticated = (req, res, next) => {
   getAuthToken(req, res, async () => {
      try {
        const { authToken } = req;
        const userInfo = await admin
          .auth()
          .verifyIdToken(authToken);
        req.authId = userInfo.uid;
        return next();
      } catch (e) {
        return res
          .status(401)
          .send({ error: 'You are not authorized to make this request' });
      }
    });
  };

  module.exports = checkIfAuthenticated

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-03 22:21:59

您必须创建一个策略并通过插件调用它。

在您的插件目录中创建一个文件夹,假设文件夹名是auth。在该manifest.js

  • create文件夹中创建一个文件夹策略,并创建一个文件 auth.js in policies文件夹。
  1. in manifest.js编写这段代码

module.exports ={module.exports={ version:'1.2.0',init: pluginContext){ let module.exports=require(‘./ policy /auth’) pluginContext.registerPolicy(policy) },策略:‘auth’,//这是让CLI自动添加到gateway.config模式中的“策略”白名单:{ //这是让CLI询问params‘例如插件配置customer-auth’$id:“https://express-gateway.io/schemas/plugins/blacklist.json”}

  1. 您的auth.js文件应该类似于

module.exports ={ name:'auth',schema:{ $id:'http://express-gateway.io/schemas/policies/example-policy.json',type:'object',属性:{ baseUrl:{ type:'string',格式:'url',默认值:‘} },策略:(actionParams) => { const = this;返回(req,res,next) => { //自定义逻辑};};

现在,您只需将清单路径放入system.config.yml中即可。

插件: auth: package:“./plugins/auth/plom.js”

最后一步是在策略部分的gateway.config.yml文件中声明此策略。

策略:-基本- auth - cors -表达式-请求-转换器-密钥-auth-日志- oauth2 -代理-速率-限制-auth

现在,您可以像使用任何其他策略一样轻松地使用它。:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62181135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档