gpt4 book ai didi

clojure - 如何在最新版本的 ring/compojure 中使用 ring anti-forgery/CSRF token ?

转载 作者:行者123 更新时间:2023-12-04 12:30:50 26 4
gpt4 key购买 nike

我复制了一些在 compojure 1.1.18 和其他旧库中工作的旧代码,但使用最新版本我无法让它工作。

这是我的 minimal example code复制自 the minimal example here为了证明使用最新的 ring 和 compojure 库,当我发送 http POST 时出现错误,即使设置了标题。
lein ring server开始它,然后做
curl -X GET --cookie-jar cookies "http://localhost:3000/"结果是这样的:

{"csrf-token":"7JnNbzx8BNG/kAeH4bz1jDdGc7zPC4TddDyiyPGX3jmpVilhyXJ7AOjfJgeQllGthFeVS/rgG4GpkUaF"}

但是当我这样做时
curl -X POST -v --cookie cookies -F "email=someone@gmail.com" --header "X-CSRF-Token: 7JnNbzx8BNG/kAeH4bz1jDdGc7zPC4TddDyiyPGX3jmpVilhyXJ7AOjfJgeQllGthFeVS/rgG4GpkUaF" http://localhost:3000/send

我收到 <h1>Invalid anti-forgery token</h1>
难道我做错了什么?

我借的代码 was intended to answer this question .

最佳答案

问题是 ring-defaults (替换了 compojure >= 1.2 中的 compojure.handler 命名空间)自动使用环 anti-forgery在通常的使用模式中:

(defroutes app-routes
(GET "/" [] (generate-string {:csrf-token
*anti-forgery-token*}))
(POST "/send" [email] "ok")
(resources "/")
(not-found "Not Found"))

(def app
(-> app-routes
(wrap-defaults site-defaults)))

因此正在生成两个防伪 token 和 GET请求提供了无效的。删除 wrap-anti-forgery线解决了问题。

关于clojure - 如何在最新版本的 ring/compojure 中使用 ring anti-forgery/CSRF token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30172569/

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