我如何使用最新版本的环 /综合设备使用RING Anti Anti Anti-Forgery / CSRF令牌



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

这是我从最小示例复制的最小示例代码,以证明使用最新的戒指和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>

我做错了吗?

我借的代码旨在回答这个问题。

问题是ring-defaults(在CompoJure> = 1.2中替换compojure.handler名称空间)自动在通常的使用方式中使用RING 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)))

因此,正在产生两个反诉讼代币,并且GET请求提供了无效的。删除wrap-anti-forgery线解决了问题。

最新更新