(我使用 Hunchentoot 和 Restas,只是想我也会在这里提到它)
我真的不知道如何使用 HTTP 来做这些事情,所以我认为发布我的代码可能是表达我的意图的最简单方法:
(define-route log-in ("/log-in/" :method :post)
(multiple-value-bind (username pwd) (hunchentoot:authorization)
(cond ((and (nth-value 1 (gethash username *users*)) ;; User exists
(string= pwd (gethash username *users*)))) ;; Password is correct
;; Do something to keep track of logged in user
)))
我基本上只是想让用户登录,给他某种方式说“嘿,又是我”,并让我用某种方式说“哦,嘿!这是” em> 你又来了,给你”,然后为用户提供一个网页。 我认为这应该通过 cookie 来完成,并简单地将一些值存储在列表中,以便可以根据 cookie 检查。
我应该如何使用 Hunchentoot+Restas 正确执行此操作?代码和一些解释真的很棒,我在这里迷失了方向。
最佳答案
您可能想要使用(start-session)
,然后添加如下方法:
(defmethod handle-request :before ((acceptor acceptor) (request request))
(unless (your-check-request-matches-login-page) ; skip session check on login page
(if (null *session*)
(redirect "/log-in")
(progn
(your-check-session-validity)
(other-stuff)))))
如果您需要使用登录页面进行身份验证,则上述方法将有效。但是您需要另一种方法来从用户那里获取用户名和密码,因为 (authorization)
将为您提供浏览器在 header 中发送的内容,这用于基本身份验证。
如果您确实想使用基本身份验证,那么浏览器将弹出一个对话框来询问用户凭据,因此您不需要登录页面。您需要以下方法来拦截所有请求并发送适当的 header :
(defmethod handle-request :before ((acceptor acceptor) (request request))
(multiple-value-bind (username pwd) (hunchentoot:authorization)
(if (and (nth-value 1 (gethash username *users*)) ;; User exists
(string= pwd (gethash username *users*))) ;; Password is correct
(progn
;; Do something here
)
(require-authorization "realm"))))
关于common-lisp - 使用 RESTAS 和 Hunchentoot 进行用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611293/