2016-10-20 4 views
0

Как я должен использовать csrf в моем приложении grails, я делаю это, инкапсулируя свою логику действий под withForm{...}.invalidToken{....}, также добавляю атрибут как : g:formRemote useToken="true" под g:formRemote тег в gsp.CSRF в grails, withForm {}. InvalidToken {} не работает нормально

Проблема заключается в том, что я всегда попадаю в блок invalidToken{...} на submit и, следовательно, моя форма не сохраняется.

Как я должен заставить его работать правильно?

Пример:

def action = { 

     withForm{ 

      ...... 
    }.invalidToken{ 

      println "Invalid Token code"  

    } 


} 

GSP например:

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

Пожалуйста, добавьте [mcve] – xenteros

ответ

0

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, устарели.

Надеюсь, это поможет.

 Смежные вопросы

  • Нет связанных вопросов^_^