Https Clojure:在heroku上包装ssl重定向?

Https Clojure:在heroku上包装ssl重定向?,https,clojure,ring,Https,Clojure,Ring,我刚刚尝试添加此包装器(->路由(wrap ssl redirect)),用于自动将http重定向到https,但是当我部署到heroku时,https://在我的浏览器中不会变为绿色,网站也不会加载 默认的heroku端口不是443,它也应该是wrap ssl redirect函数的默认端口吗 怎么了 谢谢大家! 编辑: 我的代码: (defn prod-app [routes] (-> routes (wrap-keyword-params) (wrap-p

我刚刚尝试添加此包装器
(->路由(wrap ssl redirect))
,用于自动将http重定向到https,但是当我部署到heroku时,
https://
在我的浏览器中不会变为绿色,网站也不会加载

默认的heroku端口不是
443
,它也应该是
wrap ssl redirect
函数的默认端口吗

怎么了

谢谢大家!

编辑:

我的代码:

(defn prod-app [routes]
  (-> routes
      (wrap-keyword-params)
      (wrap-params)
      (wrap-ssl-redirect)))

(defn -main []
  (let [port (Integer/parseInt (get (System/getenv) "PORT" "5000"))]
    (jetty/run-jetty (prod-app domain-specific-routes)
                     {:port port :join? false})))
编辑2:

我刚刚找到了一条可以解决我问题的线索:

想出了这个
需要https
处理程序fn:

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

(defn require-https
  [handler]
  (fn [request]
    (if (and (= (:scheme request) :http) 
             (= (get-in request [:headers "host"]) "secure.mydomain.com"))
      (ring.util.response/redirect (https-url request))
      (handler request))))
但是当我尝试连接到
http://secure.mydomain.com
,我正在浏览器地址栏中销售一个端口
https://secure.mydomain.com:80/
并在Heroku上收到此消息
ERR\u SSL\u PROTOCOL\u ERROR

,您需要他们通过环境变量提供给您。Heroku还将负载平衡器放在应用程序前面。他们将终止SSL连接并通过HTTP与您的应用程序通信,因此您的应用程序将看到的方案始终是HTTP。标准的
wrap-ssl重定向
在这种情况下不起作用,我认为它永远不会成功重定向(它会认为用户正在通过HTTP连接,即使他们确实被重定向到HTTPS)

然而,Heroku将
x-forwarded-proto
添加到每个请求的头中,以解决此问题,因此您可以查看客户端使用的协议。在同一
ring.middleware.ssl
名称空间中,是一个
wrap-forwarded方案
中间件

将请求映射的:方案更改为当前值的中间件 在请求头中。如果应用程序位于 处理SSL传输的反向代理或负载平衡器。 标头默认为x-forwarded-proto。“

如果您首先使用此命令包装路由,然后使用
wrap ssl redirect
,则它应该正确重定向到HTTPS。您还可以添加
ring.middleware.ssl/wrap hsts
以强制执行:


长寿命
lib noir
及其
wrap force ssl
fn

我只需要稍微修改一下以满足我的需要(我有一个SSL证书,用于一个子域,即使我的clojure应用程序处理多个域)

这是我的fn:

(defn wrap-force-ssl
  "Almost like in lib-noir. 
   If the request's scheme is not https [and is for 'secure.'], redirect with https.
   Also checks the X-Forwarded-Proto header."
  [app]
  (fn [req]
    (let [headers (:headers req)
          host    (headers "host")]
      (if (or (= :https (:scheme req))
              (= "https" (headers "x-forwarded-proto"))
              (not= "secure.mydomain.com" host)) ;you might not need this!
        (app req)
        (noir.response/redirect (str "https://" host (:uri req)) :permanent)))))

感谢克里斯·格兰杰和朋友们

您通常需要绑定到Heroku上的端口环境变量。您的日志是否显示任何消息?我在日志中没有看到任何问题。现在,如果我尝试
(->路由(wrap-ssl重定向{:ssl-port(Integer/parseInt(get(System/getenv)“port”“5000”))
,我的日志中有黄旗,状态为
status=301
,浏览器在我的域名旁边显示一个5位数的端口。在我的heroku配置中,我没有
端口
变量。你能用有问题的日志更新你的问题吗,你正在遵循的确切复制步骤,以及你过去在He上的工作吗roku?嗯..添加
包裹转发方案
会让chrome说:
错误太多重定向
。Heroku日志如下:`at=info method=GET path=“/favicon.ico”host=www.mydomain.com请求id=0e85ea4c-044c-4bf4-a16b-FE729ABAD3B fwd=“77.74.193.277”dyno=web.1 connect=0ms service=72ms status=301 bytes=170`也许考虑在GitHub项目上打开一个问题,要求提供关于如何与Heroku一起使用它的文档,他们可能是最好的提问人。不起作用。我开了一张罚单,但遗憾的是Heroku找到了答案。事情发生了……我认为如果包装正确,就可以得到订单重要的是,当请求通过包裹转发的方案时,首先它工作得很好。谢谢!这真的很有用。一个想法,可能要考虑检查<代码>:请求中的查询字符串< /代码>,并将它添加到重定向位置。@ AdnnelsIn你能提供一个例子或建议编辑吗?我一直在思考一些事情。e行:
(noir.response/redirect(if(nil)(:query string req))(格式为“https://%s%s”主机(:uri req))(格式为“https://%s%s”主机(:uri req)(:query string req)):永久)
(defn wrap-force-ssl
  "Almost like in lib-noir. 
   If the request's scheme is not https [and is for 'secure.'], redirect with https.
   Also checks the X-Forwarded-Proto header."
  [app]
  (fn [req]
    (let [headers (:headers req)
          host    (headers "host")]
      (if (or (= :https (:scheme req))
              (= "https" (headers "x-forwarded-proto"))
              (not= "secure.mydomain.com" host)) ;you might not need this!
        (app req)
        (noir.response/redirect (str "https://" host (:uri req)) :permanent)))))