是的,你可以,你有两个选择:
1- 像这样修改“application/config.php”文件的简单方法:
$config['csrf_protection'] = FALSE;
if (isset($_SERVER["REQUEST_URI"]))
if(stripos($_SERVER["REQUEST_URI"],'/login') !== FALSE
|| stripos($_SERVER["REQUEST_URI"],'/register') !== FALSE
)
$config['csrf_protection'] = TRUE;
2- 通过覆盖安全类并创建一个挂钩类,如下所示:
2-1 应用程序/核心/My_Security.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Security extends CI_Security
{
public function regenerate_csrf_hash()
{
// CSRF config
foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key) {
if (false !== ($val = config_item($key))) {
$this->{'_' . $key} = $val;
}
}
// Append application specific cookie prefix
if (config_item('cookie_prefix')) {
$this->_csrf_cookie_name = config_item('cookie_prefix') . $this->_csrf_cookie_name;
}
// Set the CSRF hash
$this->_csrf_set_hash();
$this->csrf_set_cookie();
}
}
2-2 application/hooks/EnableOptions.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class EnableOptions {
private $ci;
public function __construct(){
$this->ci = &get_instance();
}
public function enableCSRF()
{
if ($this->ci->config->item('csrf_protection') === false) {
$uri = $this->ci->config->item('enable_csrf_for_uris_only');
$segment = $this->ci->uri->segment('1'); // 1 : for controller name
if (in_array($segment, $uri)) {
$this->ci->config->set_item('csrf_protection', true);
$this->ci->security->regenerate_csrf_hash();
}
}
}
}
2-3 应用程序/config/hooks.php:
$hook['post_controller_constructor'][] = array(
'class' => 'EnableOptions',
'function' => 'enableCSRF',
'filename' => 'EnableOptions.php',
'filepath' => 'hooks',
'params' => array()
);
2-4 应用程序/config/config.php :
通过将“enable_hooks”修改为true来启用钩子,
$config['enable_hooks'] = TRUE;
并为 URI 添加一个额外的参数,
$config['enable_csrf_for_uris_only'] = array('login', 'register');
就是这样。