已启用CSRF的CodeIgniter抛出403(禁止)

问题描述 投票:0回答:1

所以我计划在我的网站中实施CSRF并设置我认为正确设置的需求,但仍然对ajax提交表单存在疑问。

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_token_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = FALSE;
$config['csrf_exclude_uris'] = array();

这就是我在配置中所设置的,我在其中设置$config['csrf_regenerate'] = FALSE;以防止在每次提交时重新生成(基于此讨论Ajax CSRF 403 forbidden codeigniter。]

还有我的表单,我使用form_open()form_close()而不是html表单标签。

Ajax提交表单:

$('#formData').on('submit', function(e){
    e.preventDefault();
    var formData = new FormData($(this)[0]);

    $.ajax({
        type: 'post',
        cache : false,
        processData: false,
        dataType: 'json',
        url: 'example.com.sg/login/verify',
        data: formData,
        success: function (res) {
            if(res.status == true){
                window.location.href = res.redirect;
            }
        },
        error: function(err){
            console.log(err.responseText)
        }
    });
})

PHP函数

public function verify(){
    $this->form_validation
        ->set_rules('email', 'Email', 'trim|required|valid_email')
        ->set_rules('password', 'Password', 'trim|required');

    if($this->form_validation->run() == true){
        // do email and password verification
        echo json_encode([ 'status' => true, 'redirect' => base_url('dashboard') ]);
    } else {
        $this->returnFormError($this->form_validation->error_array());
    }
}

如果我将ajax类型更改为get,则可以,但不能使用post。如果从控制台查看代码,则我的表单在隐藏的输入中包含此csrf令牌。

php codeigniter ajaxform csrf-token
1个回答
0
投票

//在config.php中$ config ['encryption_key'] ='某个密钥';首先更新此>

© www.soinside.com 2019 - 2024. All rights reserved.