如何在Nestjs中禁用Public Graphql Introspection

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

我想让我的 Graphql API 更安全,我正在寻找一种方法来禁用 Nestjs 中的 Graphql Introspection for Public 或一种排除某些私有 API 的方法,但在 Nestjs 文档中找不到任何参考,

我有 AuthGuards 设置,但它们无法达到阻止架构访问的目的。

security graphql nestjs production
2个回答
0
投票

如果您使用 Apollo,答案就在文档中

const server = new ApolloServer({ typeDefs, resolvers, introspection: process.env.NODE_ENV !== 'production' });
这会禁用生产中的自省。

如果您想始终禁用它(不推荐,因为这会使开发变得更加困难):

introspection: false
    

0
投票
在 app.module.ts 的导入数组中执行此操作

GraphQLModule.forRoot<ApolloDriverConfig>({ driver: ApolloDriver, autoSchemaFile: join(process.cwd(), 'src/schema.gql'), sortSchema: true, playground: false, introspection: process.env.NODE_ENV !== 'production' context: ({ req, res }) => ({ req, res }), subscriptions: { 'graphql-ws': true, }, plugins: [ process.env.NODE_ENV !== 'production' ? ApolloServerPluginLandingPageLocalDefault() : ApolloServerPluginLandingPageProductionDefault(), ], }),
这将确保apollo沙箱仅在开发环境中可见

© www.soinside.com 2019 - 2024. All rights reserved.