知识库

Neo4j 浏览器如何与 Neo4j 服务器交互?

从 Neo4j 3.2 开始,Neo4j 浏览器只支持与 Neo4j 服务器的 Bolt 连接。这需要网络允许浏览器和 Neo4j 服务器上指定的 Bolt 端口之间的套接字通信。要查看您的网络是否允许使用 websocket,可以使用 http://www.websocket.org/echo.html

如果您的网络不允许使用 websocket,那么在尝试通过 Neo4j 浏览器在 http://localhost:7474 进行身份验证时,将记录以下错误

ServiceUnavailable: WebSocket connection failure. Due to security constraints in your web browser, the reason for the failure is not
available to this Neo4j Driver. Please use your browsers development console to determine the root cause of the failure. Common reasons
include the database being unavailable, using the wrong connection URL or temporary network problems. If you have enabled encryption,
ensure your browser is configured to trust the certificate Neo4j is configured to use. WebSocket `readyState` is: 3

此外,要允许远程浏览器连接,您的 $NEO4J_HOME/conf/neo4j.conf 需要配置为

# To have Bolt accept non-local connections, uncomment this line:
dbms.connector.bolt.address=0.0.0.0:7687

以下是 Neo4j 浏览器和 Neo4j 服务器之间的通信过程

  • 浏览器在启动时会进行一次 HTTP 调用

    GET / HTTP/1.1
    Host: <server>:7474
    Content-Type: application/json
  • 此请求只是为了询问 Neo4j Bolt URL 是什么(可以在服务器上使用 dbms.connectors.default_advertised_address 进行配置)。在此请求之后,浏览器尝试通过 Bolt 连接到服务器,而无需凭据。这样做有两个原因

    • 当响应返回时,我们知道是否启用了身份验证

    • 如果连接成功,则更新应用程序状态以反映不需要凭据即可连接

  • 如果第一次连接尝试失败,浏览器会检查 Web 浏览器的本地存储中是否有任何登录凭据。如果有,浏览器会尝试使用这些凭据进行连接

    • 如果成功,一切正常。

    • 如果失败,放弃并提示用户输入连接凭据

  • 因此,作为图表,这将类似于

  • Seq 0

    Client === GET ==> Neo4j (HTTP 7474) Ask for Bolt connection URL
    Client <== Resp === Neo4j (HTTP 7474)
  • Seq 1

    Client === Bolt ==> Neo4j (WS 7687) without auth credentials
  • Alternate 1.1

    Client <== Resp (success) === Neo4j (WS 7687)
    (Success, stop)
  • Alternate 1.2

    Client <== Resp (no success) === Neo4j (WS 7687)
    (No success, goto Seq 2)
  • Seq 2

    Client === Bolt ==> Neo4j (WS 7687) with auth credentials
  • Alternate 2.1

    Client <== Resp (success) === Neo4j (WS 7687)
    (Success, stop)
  • Alternate 2.2

    Client <== Resp (no success) === Neo4j (WS 7687)
    (No success, prompt user for credentials)

请注意,当您有无效凭据或无法通过 Bolt 连接时,您最终会到达提示您输入凭据的同一页面,因此,如果您无法连接,请务必检查这两个可能的原因。