java - JSON前缀有什么用?

标签 java json spring spring-mvc

我知道这是一个愚蠢的问题,但我没有通过 Google 找到预期的答案。 Spring MVC 4.3的一个新特性是支持配置JSON前缀

<mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="jsonPrefix" value=")]}',\n" />
    </bean>
</mvc:message-converters>

因此 JSON 输入/输出看起来像:

)]}',\n"{\"userName\":\"ABC\",\"emailId\":\"ABC@gmail.com\"}"

我知道这是出于安全目的。但具体是关于什么的?

最佳答案

如果你看另一种添加的方法setPrefixJson(boolean) in the Spring JavaDoc你会明白背后的原因:

Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default is false.

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 should be stripped before parsing the string as JSON.

关于java - JSON前缀有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41082479/

相关文章:

c# - 在c#中删除Json数组中的Json对象

json - 使用 Typescript 进行持久化

java - 在 Oauth2 token 请求 URL 中隐藏密码

java - Jaxb 解码返回空值

java - Hibernate @SQLDelete sql 不添加模式

java - 是否实现扩展类

java - Jakson 多态枚举案例

java - 如何将256个元素的数组检索到mongodb?

Java EE JSON-P 流 API 解析数据对象

java - 在linux中使用springboot上传镜像