[alibaba/fastjson]最近几个新版本会把括号序列化成unicode字符

2025-10-29 552 views
1

hi, 如题

回答

7

提供下版本和测试用例吧.我测试没发现问题

4

不好意思,当时比较赶,没写详细,现补充一下: 所用版本: fastjson: 1.2.56 spring mvc: 4.3.22

spring mvc配置:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

使用:

@RestController(value = "testController")
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Map<String, Object> test() {
        Map<String, Object> map = new HashMap<>();
        map.put("a", "()");
        return map;
    }
}

访问打印出来的是:

{"a":"\u0028\u0029"}

而用fastjson-1.2.29打印出来的是:

{"a":"()"}

3

我这边也出现了,版本也是1.2.56

8

升级到1.2.58也是

3

从1.2.36版本,“在spring-mvc场景,缺省打开增强Feature.BrowserSecure选项,防御xss安全攻击”,开发出现

9

从1.2.36版本,“在spring-mvc场景,缺省打开增强Feature.BrowserSecure选项,防御xss安全攻击”,开发出现 @linminqin 你是对的。 打开了SerializerFeature.BrowserSecure选项,会将()<>等字符转换为16进制表示。


public void test_4() throws Exception {
String text = JSON.toJSONString("(", SerializerFeature.BrowserSecure);
System.out.println(text);
assertEquals("\"\\u0028\"", text);
}
public void test_5() throws Exception {
    String text = JSON.toJSONString(")", SerializerFeature.BrowserSecure);
    assertEquals("\"\\u0029\"", text);
}
@jakeszhong 如果想显示原符号,可以禁用此选项。如下是通过编程方式配置SpringMVC来取消SerializerFeature.BrowserSecure功能的demo,亲测可行。
```java
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //自定义配置...
        FastJsonConfig config = new FastJsonConfig();
        SerializerFeature.config(SerializerFeature.of(config.getSerializerFeatures()), SerializerFeature.BrowserSecure, false);
        config.setSerializerFeatures(SerializerFeature.values());
        converter.setFastJsonConfig(config);
        converters.add(0, converter);
    }
3

需要加入这个功能 SerializerFeature.BrowserSecure 才可以,不然会影响序列化

8

请问在MVC的配置文件中如何进行配置呢