使用Neo4Jconfiguration与Spring启动连接Neo4J时未经授权的异常

时间:2015-04-02 07:32:16

标签: java spring spring-boot spring-data-neo4j

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

Whitelabel错误页面     此应用程序没有/ 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?

这是我的bean连接Neo4J实例。

@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();
}

如何在此处设置用户名/密码?

我很感激如果得到我的问题的解决方案。感谢。

3 个答案:

答案 0 :(得分:3)

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

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

需要在获取会话工厂之前将凭据添加到sys属性。对于良好的实践,我将凭据带回到配置文件中并将其加载到系统属性中。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

使用迄今为止的最新版本

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

允许您使用以下任何方法连接到Neo4j

1.打开会话时传递连接凭据

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

2.在URL中嵌入连接凭据

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

3.设置系统属性

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