使用Java Neo4j Rest Api进行身份验证以访问服务器

Tom*_*Tom 2 java rest neo4j

我开始使用Neo4j Rest Api但是当我尝试连接服务器时出现以下错误:

{
  "errors" : [ {
    "code" : "Neo.ClientError.Security.AuthorizationFailed",
    "message" : "No authorization header supplied."
  } ]
}
Run Code Online (Sandbox Code Playgroud)

这个错误似乎是正常的,因为我在使用请求时没有对服务器进行身份验证.在Neo4j 2.2.2手册(http://neo4j.com/docs/stable/rest-api-security.html)中我读过我收到此错误的原因是:

请求应包含Authorization标头,其值为Basic,其中"payload"是base64编码的"username:password"字符串. 示例请求GET http:// localhost:7474/user/neo4j 接受:application/json; charset = UTF-8授权:基本bmVvNGo6c2VjcmV0

但我无法摆脱这个错误,因为我不知道如何在我的请求中包含标题!以下是我的代码中的请求示例:

WebResource resource = Client.create().resource( nodeEntryPointUri );   
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
                .type( MediaType.APPLICATION_JSON )
                .entity( "{\"data\" : \"nex\"}" )
                .post( ClientResponse.class );      
response.close();
Run Code Online (Sandbox Code Playgroud)

因此,如果有人可以帮我根据我的要求对服务器进行身份验证,那可能会非常有帮助!

谢谢

Ste*_*ter 5

在Jersey 1.x中,您可以为客户端实例提供处理身份验证的过滤器:

Client client = Client.create()
client.addFilter(new HTTPBasicAuthFilter("neo4j","<mypwd>")); // <-- that's it!
WebResource resource = client.resource(
    "http://localhost:7474/db/data/transaction/commit"
);

ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
   .type( MediaType.APPLICATION_JSON )
   .post( ClientResponse.class, someStringContainingJsonPayload);
Run Code Online (Sandbox Code Playgroud)