我复制了一些在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
线解决了问题。