【问题标题】:Clojurescript Websockets with Sente 403 Error带有 Sente 403 错误的 Clojurescript Websockets
【发布时间】:2019-07-24 04:00:02
【问题描述】:

我正在尝试从本地主机上的两个不同端口设置 Web 套接字连接。我正在使用 Sente 和 Immutant。我有以下内容,但在尝试连接时返回 403 禁止

服务器.clj

(defn handler
  "Comment"
  []
  "<h1>Hello World</h1>")

(let [{:keys [ch-recv send-fn connected-uids
              ajax-post-fn ajax-get-or-ws-handshake-fn]}
      (sente/make-channel-socket! (get-sch-adapter) {})]

  (def ring-ajax-post                ajax-post-fn)
  (def ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn)
  (def ch-chsk                       ch-recv) ; ChannelSocket's receive channel
  (def chsk-send!                    send-fn) ; ChannelSocket's send API fn
  (def connected-uids                connected-uids) ; Watchable, read-only atom
  )

(defroutes app
  "The router."
  (GET "/" [] (handler))
  (GET  "/chsk" req (ring-ajax-get-or-ws-handshake req))
  (POST "/chsk" req (ring-ajax-post                req))
  (route/not-found
       "<h1>Page not found</h1>"))

(def my-app
  (-> app
      ;; Add necessary Ring middleware:
      ring.middleware.keyword-params/wrap-keyword-params
      ring.middleware.params/wrap-params))

(def wrapped
  (wrap-cors my-app :access-control-allow-origin [#".*"]
                       :access-control-allow-methods [:get :put :post :delete]))

(defn -main
  "Start the server"
  [& args]
  (immutant/run wrapped {:host "localhost" :port 8080 :path "/"}))

这不会引发任何错误,并且“/”路由会正确显示。

Client.cljs

(let [{:keys [chsk ch-recv send-fn state]}
      (sente/make-channel-socket! "/chsk" ; Note the same path as before
      "sdasds" ; dummy
       {:type :auto ; e/o #{:auto :ajax :ws}
        :host "localhost:8080/"
       }
        )]
  (def chsk       chsk)
  (def ch-chsk    ch-recv) ; ChannelSocket's receive channel
  (def chsk-send! send-fn) ; ChannelSocket's send API fn
  (def chsk-state state)   ; Watchable, read-only atom
  )

这会在尝试连接时引发 403 错误。我不知道它为什么会这样,我已经看了一段时间了,但我觉得很短。

【问题讨论】:

  • 我也认为这是一个 CSRF 问题。您应该在 JS 控制台中看到 403 错误。如果您访问该链接,您应该会看到“Bad CSRF token”消息。

标签: websocket clojure clojurescript immutant sente


【解决方案1】:

我认为这是 CSRF 防伪的问题:

Sente docs:

这很重要。 Sente 有支持,但您需要使用 ring-anti-forgery 等中间件来生成和检查 CSRF 代码。应该覆盖 ring-ajax-post 处理程序(即受保护)。

在 Sente official example 他们展示了如何正确设置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2016-04-21
    • 2021-02-13
    • 2020-03-23
    • 1970-01-01
    相关资源
    最近更新 更多