Json 编码点火器CSRF响应格式

Json 编码点火器CSRF响应格式,json,codeigniter,csrf,Json,Codeigniter,Csrf,我对所有ajax调用都使用预定义的响应格式 如果请求成功,则服务器将响应: {"status":true,"data":{"name":"person","age":2}} 请注意,数据不是必需的 如果请求失败,那么我将获得 {"status":false,"reason":"You are not authorised."} 因此每个响应都有一个status字段,如果status是FALSE,那么就会有reason字段 问题是,现在我在Codeigniter中启用CSRF保护,如果令牌过期

我对所有ajax调用都使用预定义的响应格式

如果请求
成功
,则服务器将响应:

{"status":true,"data":{"name":"person","age":2}}
请注意,
数据
不是必需的

如果请求失败,那么我将获得

{"status":false,"reason":"You are not authorised."}
因此每个响应都有一个
status
字段,如果status是
FALSE
,那么就会有
reason
字段

问题是,现在我在Codeigniter中启用
CSRF
保护,如果令牌过期/失败,系统输出

The action you have requested is not allowed.
这是HTML内容

是否可以扩展安全类,以便如果请求是通过Ajax进行的,那么它将保留json_编码的格式,否则使用html格式。(我不想修改核心)


谢谢。

此错误消息来自
CI\u Exceptions::show\u error
方法(位于system/core中)。您可以通过通常的方式重写此方法,以便捕获此错误并返回所需的任何内容

您可以通过覆盖
CI\u Security::csrf\u show\u error
方法来摆脱该方法中的调用,这样它就不会简单地调用

show_error('The action you have requested is not allowed.');
这可能更可靠

或者,您可以在
CI_异常
类中攻击此漏洞。由于此错误没有特定的错误代码,因此您必须匹配可能在两次更新之间中断的消息()。生成的类可能如下所示:

class MY_Exceptions extends CI_Exceptions {
    public function show_error($heading, $message, $template = 'error_general', $status_code = 500) {
        if ($message == 'The action you have requested is not allowed.') {
            // handle this error your way
        } else {
            // send every other error to the original handler
            parent::show_error($heading, $message, $template, $status_code);
        }
    }
}
也许会有帮助