gpt4 book ai didi

clojure - Compojure:在路由处理中访问基本身份验证变量

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

我正在使用带有基本身份验证中间件的 Compojure 编写 Web API。基本身份验证部分类似于:

(defn authenticated? [id pass]
(and (= id "blah")
(= pass "blah"))

id 和 pass 使用 id:pass@website 技术传入。我的问题是,我想访问这个 id 并进一步传递到使用各种 GET、PUT、POST 等标题处理路由的地方。但我不知道该怎么做,也没有在谷歌上搜索任何运气。

; i'd like to access id and pass here
(defroutes routes
(GET "/" [] {:status 200 :headers {} :body "hi!"})
...)

我认为上述解决方案是以某种方式“添加”id 并传递给一些可以在处理路由的地方访问的变量集,但我不知道如何添加或访问它们。

希望有人能给我指出正确的方向 - 谢谢。

最佳答案

假设您正在谈论 https://github.com/remvee/ring-basic-authentication , authenticated? fn可以返回一个真值,该值将添加到 :basic-authentication 中的请求中。像(未经测试):

(defn authenticated? [id pass]
(and (= id "gal")
(= pass "foo")
{:user id :passwd pass}))

(defroutes routes
(GET "/" {the-user :basic-authentication} {:status 200 :headers {} :body (str "hi! Mr. " (:user the-user) " your password is " (:passwd the-user))})
...)

关于clojure - Compojure:在路由处理中访问基本身份验证变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120374/

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