我有一个环形服务器。我使用Buddy进行身份验证/授权。通过实现伙伴协议IAuthentication
和IAuthorization
中的-parse
、-authenticate
和-handle-unauthorized
,我实现了自己的令牌后端。
(ns myproject.auth
(:require [buddy.auth.protocols :as proto]))
...
(defn my-token-backend
([] (my-token-backend nil))
([{:keys [unauthorized-handler]}]
(reify
proto/IAuthentication
(-parse [_ request]
(token-or-nil))
(-authenticate [_ request token]
(get-user-from-token token))
proto/IAuthorization
(-handle-unauthorized [_ request metadata]
(if unauthorized-handler
(unauthorized-handler request metadata)
(handle-unauthorized-default request))))))
然后在wrap-authentication
和wrap-authorization
中间件中使用我的后端:
(defn middleware [handler]
(-> handler
(wrap-authentication my-token-backend)
(wrap-authorization my-token-backend)
…然后用中间件像这样调用我的app(def app (middleware main-routes))
.
当我去我的索引页在我的浏览器,我得到以下错误:java.lang.IllegalArgumentException: No implementation of method: :-parse of protocol: #'buddy.auth.protocols/IAuthentication found for class: myproject.auth$my_token_backend
.
当我在REPL中调用(reflect my-token-backend)
时,我注意到-parse
,-authenticate
和-handle-unauthorized
方法名称中的破折号已转换为下划线。这是我得到错误的原因吗,还是错误来自其他地方?
编辑:在Sean的评论之后,我改变了我的中间件,看起来像这样:
(defn middleware [handler]
(-> handler
(wrap-authentication (my-token-backend))
(wrap-authorization (my-token-backend))))
类myproject.auth$my_token_backend
是函数my-token-backend
,您得到的错误表示对-parse
的调用期望实现协议的对象-这将是调用函数的结果。
所以我认为你想:
(defn middleware [handler]
(-> handler
(wrap-authentication (my-token-backend))
(wrap-authorization (my-token-backend)))