Clojure:未针对课堂找到协议方法的实现
我有戒指服务器。我正在使用Buddy进行身份验证 /授权。我通过实现-parse
,-authenticate
和-handle-handle-inauthorized
代码>和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-oterorization
middleware中使用我的后端:
(defn middleware [handler]
(-> handler
(wrap-authentication my-token-backend)
(wrap-authorization my-token-backend)
...并用类似的中间件来调用我的应用程序: (DEF APP(中间件主路由))
。
当我进入浏览器中的索引页面时,我会收到以下错误: java.lang.illegalgumentException:未实现方法:: - 协议的parse:#'buddy.auth.protocols/iAuthentication,为类:myproject.auth.auth.auth $ my_token_backend_backend
。
当我调用(反映my-token-backend)
中时,我注意到方法的名称-parse
,-authenticate
和-handle-inaushorized
已转换为下划线。这是为什么我会遇到错误,还是来自其他地方的错误?
编辑:经过肖恩的评论,我将中间件更改为如下:
(defn middleware [handler]
(-> handler
(wrap-authentication (my-token-backend))
(wrap-authorization (my-token-backend))))
I have a ring server. I am using Buddy for authentication / authorization. I implemented my own token backend by implementing -parse
, -authenticate
, and -handle-unauthorized
of the buddy protocols IAuthentication
and IAuthorization
. Here it is:
(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))))))
I then use my backend in wrap-authentication
and wrap-authorization
middleware:
(defn middleware [handler]
(-> handler
(wrap-authentication my-token-backend)
(wrap-authorization my-token-backend)
...and call my app with that middleware like so:(def app (middleware main-routes))
.
When I go to my index page in my browser, I get the following error:java.lang.IllegalArgumentException: No implementation of method: :-parse of protocol: #'buddy.auth.protocols/IAuthentication found for class: myproject.auth$my_token_backend
.
When I call (reflect my-token-backend)
in the REPL, I noticed the dashes in the names of the methods -parse
, -authenticate
, and -handle-unauthorized
have been converted to underscores. Is this why I'm getting that error, or is the error coming from somewhere else?
Edit: After Sean's comment, I've changed my middleware to look like the following:
(defn middleware [handler]
(-> handler
(wrap-authentication (my-token-backend))
(wrap-authorization (my-token-backend))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类
myproject.auth $ my_token_backend
是函数my-token-backend
,您收到的错误说呼叫> -Parse
is期望实现协议的对象 - 呼叫的结果 您的函数。所以我认为你想要:
The class
myproject.auth$my_token_backend
is the functionmy-token-backend
and the error you are getting says that the call to-parse
is expecting an object that implements the protocols -- which would be the result of calling your function.So I think you want: