知识库

Neo4j Browser 如何与 Neo4j Server 交互?

从 Neo4j 3.2 开始,Neo4j Browser 仅支持与 Neo4j Server 的 Bolt 连接。这要求网络允许在浏览器和 Neo4j Server 上指定的 Bolt 端口之间进行套接字通信。要检查您的网络是否允许 WebSocket,可以使用 http://www.websocket.org/echo.html

如果您的网络不允许 WebSocket,当尝试通过 http://localhost:7474 上的 Neo4j Browser 进行身份验证时,将记录以下错误

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 Browser 和 Neo4j Server 之间的通信过程

  • Browser 在启动时会进行一次 HTTP 调用

    GET / HTTP/1.1
    Host: <server>:7474
    Content-Type: application/json
  • 此请求仅用于询问 Neo4j Bolt URL 是什么(可在服务器上通过 dbms.connectors.default_advertised_address 配置)。在该请求之后,Browser 会尝试通过 Bolt 连接到服务器,无需凭据。这有两个原因:

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

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

  • 如果首次连接尝试失败,Browser 会检查网络浏览器的本地存储中是否存储有任何登录凭据。如果有,Browser 会尝试使用它们进行连接

    • 如果成功,一切正常。

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

  • 因此,用图表表示,它看起来像这样:

  • 序列 0

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

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

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

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

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

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

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

请注意,无论您是凭据无效还是无法通过 Bolt 连接,最终都会进入同一页面提示输入凭据,因此如果您无法连接,请务必检查这两种可能的原因。

© . All rights reserved.