新手问题 怎么单独让某个路由不校验 CSRF token

lifeifei · January 08, 2020 · Last by lifeifei replied at January 08, 2020 · 2238 hits

我们都是知道 rails 默认开启 csrf token,但是我想某个路由下不去校验,仅仅是作为 api 使用,这个去哪里配置的

/tasks/ 检验 csrf token

/api/tasks 不检验 csrf token

skip_before_action :verify_authenticity_token, only: [你的 action]

skip_before_action :verify_authenticity_token, if: :conditional_actions?

def conditional_actions? # 定义一下就可以了 end

估计你想要所有的 api 请求跳过 csrf 验证,建议抽一个 api 的 base 类,在其中直接skip_before_action :verify_authenticity_token,所有 api 相关 controller 继承这个类

好的,多谢大家,问题解决了!

You need to Sign in before reply, if you don't have an account, please Sign up first.