Clojure:未针对课堂找到协议方法的实现

发布于 2025-02-04 07:57:39 字数 1731 浏览 1 评论 0原文

我有戒指服务器。我正在使用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-authenticationwrap-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-02-11 07:57:39

myproject.auth $ my_token_backend是函数my-token-backend,您收到的错误说呼叫> -Parse is期望实现协议的对象 - 呼叫结果 您的函数。

所以我认为你想要:

(defn middleware [handler]
  (-> handler
      (wrap-authentication (my-token-backend))
      (wrap-authorization (my-token-backend)))

The class myproject.auth$my_token_backend is the function my-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:

(defn middleware [handler]
  (-> handler
      (wrap-authentication (my-token-backend))
      (wrap-authorization (my-token-backend)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文