简体   繁体   English

API 网关和应用后端认证

[英]API gateway and app backend authentication

I have application with backend and frontend.我有后端和前端的应用程序。 We are using JWT token for the authentication and Authorization(A2).我们使用 JWT 令牌进行身份验证和授权(A2)。 Now we are planning to use express-gateway as an API gateway (AG) so that backend can be unload from routing and other protection heavy load and shift that burden to AG.现在我们计划将 express-gateway 用作 API 网关(AG),以便后端可以从路由和其他保护重负载中卸载,并将该负担转移到 AG。 Now since we are using AG shall we remove the A2 logic from backend and whatever request comes to backend (every request will be routed from consumer to backend via AG) we treat it as authenticated user and process the request, no need to verify again.现在,由于我们使用的是 AG,我们是否应该从后端删除 A2 逻辑以及任何到达后端的请求(每个请求都将通过 AG 从消费者路由到后端),我们将其视为经过身份验证的用户并处理请求,无需再次验证。 If yes then we will still need JWT token to get the payload to extract the information like email id, role etc. For that should we pass the token from AG to backend.如果是,那么我们仍然需要 JWT 令牌来获取有效负载以提取 email id、角色等信息。为此,我们应该将令牌从 AG 传递到后端。 Also backed might have different kind of things on payload than EG.与 EG 相比,支持的有效载荷上也可能有不同类型的东西。 How to tackle that.如何解决。

To pass authentication information on to a server, you need to use the request-transformer policy to add the information to the request headers going to the server, eg the following fragment adds a header named eg-consumers-firstname:要将身份验证信息传递到服务器,您需要使用 request-transformer 策略将信息添加到发送到服务器的请求标头中,例如,以下片段添加了一个名为 eg-consumers-firstname 的 header:

  - request-transformer:
    - condition:
        name: authenticated
      action:
        headers:
          add:
             jscode: 'req.headers["eg-consumer-firstname"] = consumer.firstname'

The JS variables you can use in jscode sections is not particularly well documented, but you have access to everything in models/users.js.您可以在 jscode 部分中使用的 JS 变量没有特别好的文档记录,但您可以访问 models/users.js 中的所有内容。

In general, you can often adjust the gateway.config.yml such that scopes restrict which apiEndpoints (paths) are available to a given user;通常,您可以经常调整 gateway.config.yml 以使范围限制给定用户可用的 apiEndpoints(路径); this is a better way to prevent unauthorized access then doing the processing on the downstream server side, which should do an independent check in case the API gateway has been compromised.这是防止未经授权的访问的更好方法,然后在下游服务器端进行处理,如果 API 网关受到损害,应该进行独立检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM