模拟和用户切换
模拟和用户切换是 Neo4j 数据库和驱动程序的功能,允许在与初始连接不同的上下文中执行查询。
模拟
模拟仍然以原始配置的用户身份对数据库进行身份验证,但以模拟用户的身份运行查询。模拟用户时,查询在模拟用户的完整安全上下文中运行,而不是在经过身份验证的用户上下文中运行(主数据库、权限等)。
以下是如何根据请求模拟不同用户的示例。此处,要模拟的用户来自 HTTP 标头 User
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL } from "@neo4j/graphql";
import neo4j from "neo4j-driver";
const typeDefs = `#graphql
type Movie {
title: String!
}
`;
const driver = neo4j.driver(
"neo4j://localhost:7687",
neo4j.auth.basic("username", "password")
);
const neo4jGraphql = new Neo4jGraphQL({
typeDefs,
driver,
});
const schema = await neo4jGraphql.getSchema();
const server = new ApolloServer({
schema,
});
const { url } = await startStandaloneServer(server, {
// Your async context function should async and return an object
context: async ({ req }) => ({
sessionConfig: {
impersonatedUser: req.headers.user,
},
}),
});
console.log(`🚀 Server ready at: ${url}`);
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL, Neo4jGraphQLContext } from "@neo4j/graphql";
import neo4j from "neo4j-driver";
const typeDefs = `#graphql
type Movie {
title: String!
}
`;
const driver = neo4j.driver(
"neo4j://localhost:7687",
neo4j.auth.basic("username", "password")
);
const neo4jGraphql = new Neo4jGraphQL({
typeDefs,
driver,
});
const schema = await neo4jGraphql.getSchema();
const server = new ApolloServer<Neo4jGraphQLContext>({
schema,
});
const { url } = await startStandaloneServer(server, {
// Your async context function should async and return an object
context: async ({ req }) => ({
sessionConfig: {
impersonatedUser: req.headers.user,
},
}),
});
console.log(`🚀 Server ready at: ${url}`);
用户切换
用户切换会完全切换使用数据库进行身份验证的用户,用于给定会话,而不会产生实例化全新驱动程序实例的性能成本。
下面是根据请求配置用户切换的示例。请注意,用户名和密码在 HTTP 标头 User
和 Password
中提供,但不建议在生产环境中使用此方法
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL } from "@neo4j/graphql";
import neo4j from "neo4j-driver";
const typeDefs = `#graphql
type Movie {
title: String!
}
`;
const driver = neo4j.driver(
"neo4j://localhost:7687",
neo4j.auth.basic("username", "password")
);
const neo4jGraphql = new Neo4jGraphQL({
typeDefs,
driver,
});
const schema = await neo4jGraphql.getSchema();
const server = new ApolloServer({
schema,
});
const { url } = await startStandaloneServer(server, {
// Your async context function should async and return an object
context: async ({ req }) => ({
sessionConfig: {
auth: neo4j.auth.basic(req.headers.user, req.headers.password),
},
}),
});
console.log(`🚀 Server ready at: ${url}`);
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL, Neo4jGraphQLContext } from "@neo4j/graphql";
import neo4j from "neo4j-driver";
const typeDefs = `#graphql
type Movie {
title: String!
}
`;
const driver = neo4j.driver(
"neo4j://localhost:7687",
neo4j.auth.basic("username", "password")
);
const neo4jGraphql = new Neo4jGraphQL({
typeDefs,
driver,
});
const schema = await neo4jGraphql.getSchema();
const server = new ApolloServer<Neo4jGraphQLContext>({
schema,
});
const { url } = await startStandaloneServer(server, {
// Your async context function should async and return an object
context: async ({ req }) => ({
sessionConfig: {
auth: neo4j.auth.basic(req.headers.user, req.headers.password),
},
}),
});
console.log(`🚀 Server ready at: ${url}`);