我在Spring messageConverter中看到了以下评论
/**
* Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
* <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
* This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
* string, the prefix would need to be ignored.
*/字符串前缀是如何防止JSON劫持的?
发布于 2015-08-09 07:13:59
精心设计的示例:假设Google有一个URL,如mail.google.com/ JSON = inbox,它以JSON格式返回收件箱的前50条消息。由于相同的来源策略,其他域上的邪恶网站无法通过AJAX请求获取这些数据,但它们可以通过标记包含URL。当设置了对象(数组或哈希)属性时,重写全局数组构造函数或访问器方法可以调用URL,允许它们读取JSON内容。
{} &&阻止了这一点: mail.google.com上的AJAX请求将完全访问文本内容,并且可以删除它。但是,标记插入不需要任何处理就盲目地执行JavaScript。由于{}是一个falsey值,实际的响应永远不会被解析。
其他框架为响应添加了不同的内容,比如while(1); (来自Google的例子),它会给黑客造成无限的循环,但我们可以在自己的站点上删除它。
发布于 2015-08-09 07:20:05
前缀的添加将使字符串无效。
我认为您可能需要检查这个堆栈溢出问题并对其进行注释:“)}”、“n”和"{} &“之间的区别,以避免json劫持](https://stackoverflow.com/questions/21999572/difference-between-n-and-in-avoiding-json-hijacking)
https://stackoverflow.com/questions/31901528
复制相似问题