确保在Clojure中绑定var
在我的中间件中,我正在检查确保在Clojure中绑定var,clojure,nullpointerexception,ring,Clojure,Nullpointerexception,Ring,在我的中间件中,我正在检查:session的值,以查看用户是否已登录 如果设置了:session的值,则效果非常好。尽管如此,我不确定检查:session是否绑定的最佳方法是什么 (defn logged-in-verify [ring-handler] (fn new-ring-handler [request] ;;verify that the scrypt hash of email and timestamp matches. (let [session
:session
的值,以查看用户是否已登录
如果设置了:session
的值,则效果非常好。尽管如此,我不确定检查:session
是否绑定的最佳方法是什么
(defn logged-in-verify
[ring-handler]
(fn new-ring-handler
[request]
;;verify that the scrypt hash of email and timestamp matches.
(let [session (:session request)
email (:ph-auth-email session)
token (:ph-auth-token session)
timestamp (:ph-auth-timestamp session)]
(if (scryptgen/check (str email timestamp) token)
(do
;; return response from wrapped handler
(ring-handler request))
;; return error response
{:status 400, :body "Please sign in."}))))
因为我没有检查会话是否绑定,所以使用这个中间件的东西如果未设置,就会返回一个NullPointerException
。最好的方法是什么?使用或类似方法检查您是否确实有一个会话:
(defn logged-in-verify
[ring-handler]
(fn new-ring-handler
[request]
;;verify that the scrypt hash of email and timestamp matches.
(if-let [session (:session request)]
(let [email (:ph-auth-email session)
token (:ph-auth-token session)
timestamp (:ph-auth-timestamp session)]
(if (scryptgen/check (str email timestamp) token)
;; return response from wrapped handler
(ring-handler request))
;; return error response
{:status 400, :body "Please sign in."}))
;; do something when there is no session yet
(generate-new-session-and-redirect))))
Ring请求只是映射,因此您可以使用“contains”(包含)来查看它是否包含特定的键,或者使用“get”(获取)来获取与键关联的值,或者使用默认值(如果该键不在映射中)。我仍然会得到空指针异常。。。也许我错过了什么。。。我会处理它。啊,事实证明:会话总是设置好的,尽管第一个是{}。添加一个额外的if-let似乎是一个不错的选择。感谢您可以使用
进入
。考虑到您的问题,您可能希望有类似于(如果let*[email(进入请求[:session:ph auth-email]令牌(进入请求[:session:ph auth-token])的东西,
,但如果let*不存在,则需要。Cf。