5

我已经用 ssl 建立了一个 hunchentoot 服务器。我希望将常规的 http 请求重定向到 https。

似乎是和的某种hunchentoot:define-easy-handler组合hunchentoot:redirect要走的路,但我无法弄清楚。

这是我到目前为止所拥有的:

(defvar *https-handler*
  (make-instance 'hunchentoot:easy-ssl-acceptor
                 :name 'ssl
                 :ssl-privatekey-file #P"/path/to/privkey.pem"
                 :ssl-certificate-file #P"/path/to/cert.pem"
                 :port 443))

(hunchentoot:start *https-handler*)
4

3 回答 3

4

是的,您可以添加简单的 http 处理程序并重定向到 ssl 版本:

(defvar *http-handler*
  (make-instance 'hunchentoot:easy-acceptor
                 :name 'http
                 :port 80))

(hunchentoot:define-easy-handler (redir-to-ssl :uri (lambda (uri) t) :acceptor-names '(http)) ()
  (hunchentoot:redirect "/" :protocol :https)) ; where magic happens

...然后也启动它:

(hunchentoot:start *http-handler*)

此版本仅重定向到 index /

于 2019-05-03T01:53:57.997 回答
3

嗯,我是hunchentoot:*dispatch-table*直接用的。独立于我发现的路径重定向它的方法是hunchentoot:redirect除非(hunchentoot:ssl-p)在处理程序内部。我的大多数defuned 处理程序都包装在一个宏中以进行身份​​验证。所以,我只需要修改那个宏,然后M-x slime-who-macroexpands-> C-c C-k

(unless (hunchentoot:ssl-p)
  (hunchentoot:redirect (hunchentoot:request-uri*)
                       :protocol :https))
于 2020-04-23T19:28:05.233 回答
0

如果您需要将每个 HTTP 请求不加选择地重定向到 HTTPS,easy-acceptor则无需使用。我建议定义一个专门的acceptor

(defclass http-to-https-acceptor (hunchentoot:acceptor) ())
(defmethod hunchentoot:acceptor-dispatch-request ((acceptor http-to-https-acceptor) request)
  (hunchentoot:redirect (hunchentoot:request-uri request)
                        :protocol :https))

然后在某个时候:

(hunchentoot:start (make-instance 'http-to-https-acceptor :port 80))
于 2022-01-28T18:03:55.283 回答