【问题标题】:How can I use ring anti-forgery / CSRF token with latest version ring/compojure?如何在最新版本的 ring/compojure 中使用环防伪/CSRF 令牌?
【发布时间】:2015-07-22 05:55:10
【问题描述】:

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

这是我从 the minimal example here 复制的 minimal example code,以证明使用最新的 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

【问题讨论】:

标签: clojure ring compojure csrf-protection


【解决方案1】:

问题在于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 行解决了这个问题。

【讨论】:

  • 如果您对结果满意,可以将自己的问题标记为已回答。
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 2015-03-12
  • 2017-09-29
  • 2013-07-18
  • 1970-01-01
  • 2011-04-21
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多