gpt4 book ai didi

clojure - 按正确顺序放置 Clojure Ring 中间件

转载 作者:行者123 更新时间:2023-12-02 13:25:12 26 4
gpt4 key购买 nike

我的 Clojure 服务器中间件遇到问题。我的应用程序有以下要求:

  • 某些路线应该可以毫无问题地访问。其他人需要基本身份验证,因此我希望有一个位于所有处理程序函数前面的身份验证函数,并确保请求得到验证。为此,我一直在使用ring-basic-authentication处理程序,特别是how to separate your public and private routes上的说明。 .

  • 但是,我还希望在 Authorization: header 中发送的参数在路由 Controller 中可用。为此,我一直在 compojure.handler 中使用 Compojure 的 site 函数,它将变量放入请求的 :params 字典中(请参阅例如 Missing form parameters in Compojure POST request )

但是我似乎无法让 401 授权参数同时工作。如果我尝试这个:

; this is a stripped down sample case:

(defn authenticated?
"authenticate the request"
[service-name token]
(:valid (model/valid-service-and-token service-name token)))

(defroutes token-routes
(POST "/api/:service-name/phone" request (add-phone request)))

(defroutes public-routes
controller/routes
; match anything in the static dir at resources/public
(route/resources "/"))

(defroutes authviasms-handler
public-routes
(auth/wrap-basic-authentication
controller/token-routes authenticated?))

;handler is compojure.handler
(def application (handler/site authviasms-handler))

(defn start [port]
(ring/run-jetty (var application) {:port (or port 8000) :join? false}))

授权变量可以在authenticated?函数中访问,但不能在路由中访问。

显然,这不是一个非常普遍的示例,但我觉得我真的是在白费力气,只是随机更改中间件顺序并希望事情能够正常进行。我希望能够为我的具体示例提供一些帮助,并了解有关如何包装中间件以使事情正确执行的更多信息。

谢谢,凯文

最佳答案

据我所知,ring.middleware.basic-authentication 不会从请求中的 :params 中读取任何内容,ring.core.request/site 也不会在其中放置任何与身份验证相关的内容。

但在任何环处理程序中,您仍然可以访问 header 。像这样的东西:

(GET "/hello" 
{params :params headers :headers}
(str "your authentication is " (headers "authentication")
" and you provided " params))

类似地,如果您确实愿意,您可以使用它来编写自己的中间件,将与身份验证相关的内容放入参数中。

关于clojure - 按正确顺序放置 Clojure Ring 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com