如何使用 clojure-liberator 实现用户身份验证



我并不真正了解 https://github.com/clojure-liberator/liberator 以及它提供给开发人员的决策点列表。如何使用库的顶部/旁边/顶部实现基本的身份验证/身份验证服务?

惯用的方法是实现:authorized?决策点。但是,目前不支持处理基本身份验证或摘要式身份验证。一种实用的方法是使用 ring-basic-authentication 进行身份验证,并仅处理资源中的授权。以下示例使用环基本身份验证并将令牌设置为用户的角色。然后由解放者检查此角色authorized?

(defresource admin-only
  :handle-ok "secrect"
  :handle-unauthorized "for admins only"
  :authorized? (fn [{{token :token} :request}]
                 (= "admin" token)))
;; token returned encodes role
(defn authenticated? [name pass] 
  (cond (and (= name "scott") 
             (= pass "tiger")) "admin")
        (and (= name "jack")
             (= pass "jill"))  "user)))
(def app (wrap-basic-authentication admin-only authenticated?))

来自自述文件"

资源与 Ring 兼容,可以包装在 Ring 中间件中。评估时,资源返回一个函数,该函数接受 Ring 请求并返回 Ring 响应。

因此,您可以将其包装在环基本身份验证中

(use 'ring.middleware.basic-authentication)
(defn authenticated? [name pass] (and (= name "foo") (= pass "bar")))
(def app (-> routes .. (wrap-basic-authentication authenticated?))

相关内容

最新更新