简体   繁体   English

GroceryCRUD在启用CodeIgniter CSRF保护时添加,编辑按钮不起作用

[英]GroceryCRUD add, edit buttons not working when enabling CodeIgniter CSRF protection

I am using GroceryCRUD 1.5.0 with CodeIgniter 2.2.0. 我正在使用GroceryCRUD 1.5.0和CodeIgniter 2.2.0。

When enabling CodeIgniter's internal CSRF protection with: 使用以下命令启用CodeIgniter的内部CSRF保护时:

$config['csrf_protection'] = TRUE;

in application/config/config.php , then the GroceryCRUD auto-generated action buttons (edit, view) and links (add) does not work anymore. application/config/config.php ,GroceryCRUD自动生成的操作按钮(编辑,查看)和链接(添加)不再起作用。

It seems that the CSRF token is not passed along in the Ajax calls (confirmed with Firebug). 似乎CSRF令牌不会在Ajax调用中传递(使用Firebug确认)。 It is possible to use this CodeIgniter feature with GroceryCRUD? 可以将此CodeIgniter功能与GroceryCRUD一起使用吗?

I finally managed to solve my problem. 我终于设法解决了我的问题。 Two options are available: 有两种选择:

The easy way : 简单的方法

Set: 组:

$config['grocery_crud_dialog_forms'] = false;

in application/config/grocery_crud.php . application/config/grocery_crud.php

This option works well without CSRF protection enabled (that is, it can be set to true to produce more elegant forms), but fails when set if no code modifications are done in the javascript. 此选项在没有启用CSRF保护的情况下运行良好(也就是说,可以将其设置为true以生成更优雅的表单),但如果在javascript中未进行任何代码修改,则设置失败。

The elegant way : 优雅的方式

If we want to use: 如果我们想使用:

$config['grocery_crud_dialog_forms'] = true;

in application/config/grocery_crud.php to have the cute forms, then: application/config/grocery_crud.php中有可爱的表单,然后:

  1. include the jquery.cookie plugin in pages with forms 在包含表单的页面中包含jquery.cookie插件

  2. add this code to your JS files to auto-magically insert the CSRF token in all ajax POST calls: 将此代码添加到您的JS文件中,以自动神奇地在所有ajax POST调用中插入CSRF令牌:

 $(document).ready(function() { var csrf_token= $.cookie('csrf_cookie_name'); $.ajaxSetup({ data: { 'csrf_test_name' : csrf_token } }); }); 

I hope this will help someone else. 我希望这会帮助别人。

Just in case someone has the same error: For CI 3.0.1 and GroceryCRUD 1.5.1, Cookies are properly sent with AJAX requests, however because the token changes, only the first request will work. 万一有人有相同的错误:对于CI 3.0.1和1.5.1 GroceryCRUD,Cookies 正确的AJAX请求发送,但是由于令牌的变化,只有第一个请求会工作。

To always use the same token, set (in application/config/config.php ): 要始终使用相同的标记,请设置(在application/config/config.php ):

$config['csrf_regenerate'] = FALSE;

Edit: Manual for reference: http://www.codeigniter.com/user_guide/libraries/security.html#cross-site-request-forgery-csrf 编辑:手册供参考: http//www.codeigniter.com/user_guide/libraries/security.html#cross-site-request-forgery-csrf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM