【问题标题】:CSRF in grails, withForm{}.invalidToken{} not working finegrails 中的 CSRF,withForm{}.invalidToken{} 无法正常工作
【发布时间】:2016-10-24 00:27:32
【问题描述】:

因为我必须在我的 grails 应用程序中使用 csrf,所以我通过在withForm{...}.invalidToken{....} 下封装我的操作逻辑来做到这一点,我还在 gsp 中的g:formRemote 标记下添加一个属性为:g:formRemote useToken="true"

问题是,我总是在提交时进入 invalidToken{...} 块,因此我的表单没有得到保存。

我应该如何让它正常工作?

例子:

def action = {

       withForm{

             ......
  }.invalidToken{

          println "Invalid Token code"    

    }


}

gsp 前:

<g:formRemote useToken="true" ...>
     ...
     ...
 </g:formRemote>

【问题讨论】:

标签: security grails csrf


【解决方案1】:

useToken 不支持 formRemote。请关注https://github.com/grails/grails-core/blob/2.5.x/grails-plugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/JavascriptTagLib.groovy#L349

formRemote 支持的属性是:

     * @attr name REQUIRED The form name
     * @attr url REQUIRED The url to submit to as either a map (containing values for the controller, action, id, and params) or a URL string
     * @attr action The action to execute as a fallback, defaults to the url if non specified
     * @attr update Either a map containing the elements to update for 'success' or 'failure' states, or a string with the element to update in which cause failure events would be ignored
     * @attr before The javascript function to call before the remote function call
     * @attr after The javascript function to call after the remote function call
     * @attr asynchronous Whether to do the call asynchronously or not (defaults to true)
     * @attr method The method to use the execute the call (defaults to "post")

话虽如此。您可以在控制器中继续使用withForm,并使用 jQuery 或其他 JS 库实现表单提交并发送令牌。请看一下这个问题:Grails - Is there a recommended way of dealing with CSRF attacks in AJAX forms?

另外,我建议您不要使用 formRemote 等标签。我假设您至少使用 Grails 2.x。如果您将来想迁移到 Grails 3.x,则不推荐使用与 Ajax 相关的标签。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-05
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2014-08-24
    相关资源
    最近更新 更多