【问题标题】:How to get complete details from request coming from gateway to services using Apollo Federation如何使用 Apollo Federation 从网关到服务的请求中获取完整的详细信息
【发布时间】:2021-09-28 13:36:59
【问题描述】:

我正在尝试在 Apollo Federation 中实现 Auth0,我能够在其单个服务 (https://auth0.com/blog/developing-a-secure-api-with-nestjs-adding-authorization/#Set-Up-API-Authorization) 中实现它,但如果我尝试通过网关访问 API,则标头/有效负载不会传递给服务,因此它总是未经授权的。

如果 API 是通过单个服务访问的,则有效负载会被接收并从标头中正确解码并且工作正常,但如果通过网关,它不会级联到需要它的服务。

目前使用的是代码优先实现。我也试过镜像服务中使用的模块,但还是不行。

单个服务中的示例负载

{
  iss: 'issuer url here',
  sub: 'google-oauth2',
  aud: ['audience link'],
  iat: ,
  exp: ,
  azp: '',
  scope: '',
  permissions: [ 'sample:permission' ] 
}

在网关中导入

imports: [
ConfigModule.forRoot(),
GraphQLGatewayModule.forRoot({
            server: {
                cors: true,
            },
            gateway: {
                serviceHealthCheck: true,
                serviceList: [
                    {
                        name: 'service',
                        url: `${process.env.SERVICE_URL}/graphql`,
                    },
                ],
            },
        }),
]

【问题讨论】:

    标签: graphql nestjs auth0 apollo-federation


    【解决方案1】:

    您可以使用 buildService 选项自定义内部请求中使用的标头:

    server.ts

        const gateway = new ApolloGateway({
            buildService: ({ url }) => new RequestHandler({ url }),
            serviceList,
        })
    

    RequestHandler 类扩展 RemoteGraphQLDataSource 的地方:

    import { RemoteGraphQLDataSource } from '@apollo/gateway'
    import type { GraphQLRequest } from 'apollo-server-core'
    import type express from 'express'
    
    export class RequestHandler extends RemoteGraphQLDataSource {
        willSendRequest({ context, request }: { context: { req: express.Request }, request: GraphQLRequest }) {
            request.http?.headers.set('somethingFromOriginalRequestOrSomethingCustom', context.req.headers['something'])
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2015-10-01
      • 2021-11-29
      • 1970-01-01
      • 2018-10-04
      • 2012-02-29
      相关资源
      最近更新 更多