clojure:没有找到类的协议方法的实现



我有一个环形服务器。我使用Buddy进行身份验证/授权。通过实现伙伴协议IAuthenticationIAuthorization中的-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-authenticationwrap-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)))

相关内容

  • 没有找到相关文章

最新更新