Clojure / Noir:强制HTTPS,如果请求是http://到https://则重定向 [英] Clojure / Noir: Force HTTPS, redirect if the request was http:// to https://

查看:176
本文介绍了Clojure / Noir:强制HTTPS,如果请求是http://到https://则重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上强制使用SSL。如果只有http

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

我写了下面的代码,但它不是一个环形样式的中间件将网站重定向到相同的URL与https除了检查请求方案并打印它应该重定向到的URL之外,真正做任何事情。

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?

我使用clojure 1.2和Noir。

I'm using clojure 1.2 with Noir.

注意:如何使用多个嵌套的str将多个字符串组合成一个字符串? / p>

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

推荐答案

您可以使用 ring.util.response / redirect



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)
    ...))

对于旁注, str 是可变的:

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

这篇关于Clojure / Noir:强制HTTPS,如果请求是http://到https://则重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆