Common lisp 使用RESTAS和Hunchentoot进行用户身份验证
(我用Hunchentoot和Restas,只是想在这里也提一下) 我真的不知道如何使用HTTP来做这些事情,所以我想发布我的代码可能是显示我意图的最简单方式:Common lisp 使用RESTAS和Hunchentoot进行用户身份验证,common-lisp,hunchentoot,Common Lisp,Hunchentoot,(我用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*)
(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
)))
我基本上只想让一个用户登录,给他某种方式说“嘿,又是我了”,让我说“哦,嘿!又是你了,给你了”,然后为用户提供一个网页。
我认为这应该通过cookie来完成,同时简单地将一些值存储在一个可以对照cookie进行检查的列表中
我该如何在Hunchentoot+Restas上正确地做到这一点?代码和一些解释将非常棒,我在这里很迷茫。您可能需要使用
(启动会话)
,然后添加如下方法:
(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)))))
如果您需要使用登录页面进行身份验证,上述方法将起作用。但是,您需要另一种方式从用户处获取用户和密码,因为(授权)
将提供浏览器在标题中发送的内容,即基本身份验证
如果您确实想使用基本身份验证,那么浏览器将弹出一个对话框询问用户凭据,因此您不需要登录页面。您需要以下方法来截获所有请求并发送适当的标头:
(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"))))
另外,
hunchentoot:authorization
用于基本授权。您需要调用(需要授权“某些领域”)
,以便Hunchentoot将正确的标题发送到浏览器。然后,浏览器向用户询问用户和密码,所有请求都会在clear上附加user+base64编码的密码。如果您在所有通信中使用HTTPS是可以的,但是在其他方面是不安全的。