Pet*_*and 4 http clojure request cors preflight
我在 Clojure 中有一个使用 Compojure 的简单服务器(这是环形模式的一些风格)。开发中一切都工作正常,现在我在产品中,我无法让 CORS 为我的一生工作 - 我有一个wrap-preflight似乎工作正常的功能,但我不断在终端中收到 CORS 错误,而且发布或获取对我的评论系统工作的请求。我完全迷失了,非常沮丧,我四处询问,似乎没有人知道。
这是主要core.clj代码 - 如果有人有任何想法请告诉我。您可以在 thedailyblech.com 上实时查看错误(不是广告,但也许有助于调试)。
谢谢你!
(ns clojure-play.core
(:use org.httpkit.server
[compojure.core :refer :all]
[compojure.route :as route]
[clojure.data.json :as json]
[clojure.tools.logging :only [info]]
[clojure-play.routes :as routes]
[ring.middleware.json :only [wrap-json-body]]
[ring.middleware.cors :refer [wrap-cors]])
(:require [monger.core :as mg]
[monger.collection :as mc]
[clojure.edn :as edn]
[clojure.java.io :as io]
[compojure.handler :as handler])
(:import [org.bson.types ObjectId]
[com.mongodb DB WriteConcern])
(:gen-class))
(println "in the beginning was the command line...")
(defonce channels (atom #{}))
(defn connect! [channel]
(info "channel open")
(swap! channels conj channel))
(defn notify-clients [msg]
(doseq [channel @channels]
(send! channel msg)))
(defn disconnect! [channel status]
(info "channel closed:" status)
(swap! channels #(remove #{channel} %)))
(defn ws-handler [request]
(with-channel request channel
(connect! channel)
(on-close channel (partial disconnect! channel))
(on-receive channel #(notify-clients %))))
(defn my-routes [db]
(routes
(GET "/foo" [] "Hello Foo")
(GET "/bar" [] "Hello Bar")
(GET "/json_example/:name" [] routes/json_example)
(GET "/json_example" [] routes/json_example)
(POST "/email" [] routes/post_email)
(POST "/write_comment" [] (fn [req] (routes/write_comment req db)))
(POST "/update_comment" [] (fn [req] (routes/update_comment req db)))
(GET "/read_comments/:path" [path] (fn [req] (routes/read_comments req db path)))
(GET "/read_comments/:path1/:path2" [path1 path2] (fn [req] (routes/read_comments req db (str path1 "/" path2))))
(GET "/ws" [] ws-handler)))
(defn connectDB []
(defonce connection
(let
[uri "mongodb://somemlabthingy"
{:keys [conn db]} (mg/connect-via-uri uri)]
{:conn conn
:db db}))
{:db (:db connection)
:conn (:conn connection)})
(def cors-headers
"Generic CORS headers"
{"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Headers" "*"
"Access-Control-Allow-Methods" "GET POST OPTIONS DELETE PUT"})
(defn preflight?
"Returns true if the request is a preflight request"
[request]
(= (request :request-method) :options))
(defn -main
"this is main"
[& args]
(println "hello there main")
(def db (get (connectDB) :db))
(println (read-string (slurp (io/resource "environment/config.edn"))))
(defn wrap-preflight [handler]
(fn [request]
(do
(println "inside wrap-preflight")
(println "value of request")
(println request)
(println "value of handler")
(println handler)
(if (preflight? request)
{:status 200
:headers cors-headers
:body "preflight complete"}
(handler request)))))
(run-server
(wrap-preflight
(wrap-cors
(wrap-json-body
(my-routes db)
{:keywords? true :bigdecimals? true})
:access-control-allow-origin [#"http://www.thedailyblech.com"]
:access-control-allow-methods [:get :put :post :delete :options]
:access-control-allow-headers ["Origin" "X-Requested-With"
"Content-Type" "Accept"]))
{:port 4000}))
Run Code Online (Sandbox Code Playgroud)
CORS 中间件自动处理预检内容——您不需要单独的中间件,也不需要生成自己的标头等。
你让它包装了routes正确的内容——所以 CORS 检查首先发生,然后是路由。您应该删除自定义预检中间件,此时它应该可以工作。
我们在工作中使用wrap-cors,唯一遇到的麻烦是允许足够的标头(一些标头是由生产基础设施插入的,例如负载均衡器)。我们最终得到了这个:
:access-control-allow-headers #{"accept"
"accept-encoding"
"accept-language"
"authorization"
"content-type"
"origin"}
Run Code Online (Sandbox Code Playgroud)
就其价值而言,我们拥有以下方法:
:access-control-allow-methods [:delete :get
:patch :post :put]
Run Code Online (Sandbox Code Playgroud)
(你不需要:options在那里)
| 归档时间: |
|
| 查看次数: |
1600 次 |
| 最近记录: |