Clojure / Noir: Force HTTPS, redirect if the request was http:// to https://

我怕爱的太早我们不能终老 提交于 2020-01-02 02:19:28

问题


I'm trying to force SSL on my site. I want to have a ring style middle-ware to redirect the site to the same URL with https if it is only http

I wrote the following code but it doesn't really do anything besides check the request scheme and print the URL it should be redirecting to.

(defn https-url [request-url]
  (str (str (str (str "https://" (:server-name request-url) ":") (:server-port request-url))) (:uri request-url)))

(defn require-https
  [handler]
  (fn [request]
    (let [page-request (handler request)]
      (if (= (:scheme page-request) :http)
        (println (https-url page-request))))))

(server/add-middleware require-https)

How would I implement this into a real app?

I'm using clojure 1.2 with Noir.

Side note: How do I combine multiple strings into one string with out using multiple nested str's?


回答1:


You can use ring.util.response/redirect:

(fn handler [request]
  (if need-to-redirect?
    ;; NB. target-url should be a string
    (ring.util.response/redirect target-url)
    ...))

As for the side note, str is variadic:

(str "foo" 'bar "baz")
; => "foobarbaz"


来源:https://stackoverflow.com/questions/9303054/clojure-noir-force-https-redirect-if-the-request-was-http-to-https

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!