使用带有 Spring 引导的 Neo4J 配置连接 Neo4J 时发生未经授权的异常

Unauthorized exception while connecting Neo4J using Neo4Jconfiguration with Spring boot

您好,我正在尝试编写一个微服务,我已经创建了一个简单的 spring 启动应用程序,它使用 Neo4JConfiguration 进行扩展。我可以 运行 成功并且服务器使用目标端口启动但是通过点击 api 内部错误在浏览器中抛出

白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

There was an unexpected error (type=Internal Server Error, status=500).
    org.springframework.web.util.NestedServletException: Request processing failed; 
    nested exception is org.neo4j.ogm.session.result.ResultProcessingException: 
        Failed to execute request: 
        {"statements":[{"statement":"MATCH p=(n)-[*0..1]-(m) WHERE id(n) = { id } RETURN collect(distinct p)",
            "parameters":{"id":1},"resultDataContents":["graph"]}]}

而我的 mvn 控制台错误清楚地表明会话尚未获得授权。错误:

12:12:19.114 [qtp212459676-16] INFO  org.neo4j.ogm.session.Neo4jSession - There is no existing transaction, creating a transient one
12:12:19.272 [qtp212459676-16] INFO  o.n.o.session.request.DefaultRequest - POST http://localhost:7474/db/data/transaction/commit, request: {"statements":[{"statement":"MATCH p=(n)-[*0..1]-(m) WHERE id(n) = { id } RETURN collect(distinct p)","parameters":{"id":1},"resultDataContents":["graph"]}]}
**caught response exception: Unauthorized**
12:12:19.606 [qtp212459676-16] INFO  o.s.d.n.config.Neo4jConfiguration - Intercepted exception
12:12:19.642 [qtp212459676-16] WARN  o.e.jetty.servlet.ServletHandler - 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.neo4j.ogm.session.result.ResultProcessingException: Failed to execute request: {"statements":[{"statement":"MATCH p=(n)-[*0..1]-(m) WHERE id(n) = { id } RETURN collect(distinct p)","parameters":{"id":1},"resultDataContents":["graph"]}]}

谁能告诉我如何使用 springboot 授权 neo4j 吗?

这是我与 Neo4J 实例连接的 bean。

@Override
@Bean
public Neo4jServer neo4jServer() {
   // log.info("Initialising server connection");    
    return new RemoteServer("http://localhost:7474");
    //return new InProcessServer();
}

@Override
@Bean
public SessionFactory getSessionFactory() {
    log.info("Initialising Session Factory");
    return new SessionFactory("nuclei.domain");
}

@Override
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
    log.info("Initialising session-scoped Session Bean");
    return super.getSession();
}

如何在这里设置用户名/密码?

如果能得到我问题的标准解决方案,我将不胜感激。谢谢

您可以将它们指定为系统属性。参见org.neo4j.ogm.authentication.CredentialsService;例如-Dusername=neo4j -Dpassword=your_password

我现在已经成功连接到数据库了。以下是我添加的系统属性部分。

@Override
@Bean
public SessionFactory getSessionFactory() {
    log.info("Initialising Session Factory");
    System.setProperty("username", "neo4j");
    System.setProperty("password", "root");
    return new SessionFactory("nuclei.domain");
}

需要在获取会话之前将凭据添加到 sys 属性 factory.For 好的做法我将凭据带回我的配置文件并将其加载到系统属性。

使用最新版本

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm</artifactId>
    <version>1.1.4</version>
</dependency>

使您能够使用以下任何方法连接到 Neo4j

1.Passing 打开会话时的连接凭据

SessionFactory sessionFactory = new SessionFactory("org.neo4j.example.domain");
Session session = sessionFactory.openSession("http://localhost:7474", username, password);

2.Embedding URL

中的连接凭据
SessionFactory sessionFactory = new SessionFactory("org.neo4j.example.domain");
Session session = sessionFactory.openSession("http://username:password@localhost:7474");

3.Setting 系统属性

System.setProperty("username", user);
System.setProperty("password", pass);
SessionFactory sessionFactory = new SessionFactory("org.neo4j.example.domain");
Session session = sessionFactory.openSession("http://localhost:7474");