我有一个环形服务器。我正在使用 Buddy 进行身份验证/授权。我通过实现-parse
,-authenticate
和-handle-unauthorized
buddy 协议IAuthentication
和IAuthorization
. 这里是:
(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)
...并使用该中间件调用我的应用程序,如下所示(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
.
当我调用(reflect my-token-backend)
REPL 时,我注意到方法名称中的破折号-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)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句