解决fastjson从1.1.41升级到1.2.28后报错问题详解
作者:漫夭 发布时间:2021-12-30 21:55:35
最近因为fastjson安全漏洞,升级jar包时,踩了一些坑。
新版本FastJsonHttpMessageConverter初始化,默认设置MediaType为*/*
背景:
使用Spring RestTemplate,配置如下:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="ky.clientHttpRequestFactory"/>
<property name="errorHandler">
<bean class="org.springframework.web.client.DefaultResponseErrorHandler"/>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="cn.com.autodx.common.jsonView.ViewAwareJsonMessageConverter">
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
<value>text/javascript;charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
其中ViewAwareJsonMessageConverter继承自FastJsonHttpMessageConverter。
fastjson从1.1.41升级到1.2.28之后,请求报错:
json java.lang.IllegalArgumentException: 'Content-Type' cannot contain wildcard type '*'
原因是在1.1.41中,FastJsonHttpMessageConverter初始化时,设置了MediaType。
public FastJsonHttpMessageConverter(){
super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8));
}
而在1.2.28中,设置的MediaType为‘/',即:
public FastJsonHttpMessageConverter() {
super(MediaType.ALL); // */*
}
后续在org.springframework.http.converter.AbstractHttpMessageConverter.write过程中,又要判断Content-Type不能含有通配符,这应该是一种保护机制,并强制用户自己配置MediaType。代码如下:
@Override
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
final HttpHeaders headers = outputMessage.getHeaders();
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(t);
}
if (contentTypeToUse != null) {
//设置Content-Type,不允许含有通配符
headers.setContentType(contentTypeToUse);
}
}
......
if (outputMessage instanceof StreamingHttpOutputMessage) {
......
}else {
//自定义MessageConverter的write操作
writeInternal(t, outputMessage);
outputMessage.getBody().flush();
}
}
public void setContentType(MediaType mediaType) {
Assert.isTrue(!mediaType.isWildcardType(), "'Content-Type' cannot contain wildcard type '*'");
Assert.isTrue(!mediaType.isWildcardSubtype(), "'Content-Type' cannot contain wildcard subtype '*'");
set(CONTENT_TYPE, mediaType.toString());
}
所以,需要为ViewAwareJsonMessageConverter设置supportedMediaTypes:
<bean class="cn.com.autodx.common.jsonView.ViewAwareJsonMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>application/*+json;charset=UTF-8</value>
</list>
</property>
</bean>
新版本序列化默认不再对字段进行排序
这个是一个签名算法的场景:客户端对参数进行序列化,然后md5加密成一个签名;服务端按照相同的算法解析一遍参数,对比签名值。这里加密依赖json序列化之后的字符串,也就依赖序列化时字段的排序。
这是fastjson做了一个性能优化,将排序需求抽象出一个SerializerFeature,供用户自己配置。如果需要排序场景,在序列化时添加参数SerializerFeature.MapSortField即可,即:
JSON.toJSONString(obj, SerializerFeature.MapSortField);
官方文档
1.2.3之后的版本,Map的序列化没有做排序再输出,原因是通过TreeMap排序很影响性能。
1.2.27版本中增加SerializerFeature.MapSortField实现同样的功能。
使用方法如下:
a) 传入SerializerFeature.MapSortField参数。 JSON.toJSONString(map, SerializerFeature.MapSortField);
b) 通过代码修改全局缺省配置。 JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.MapSortField.getMask();
c) 通过JVM启动参数配置修改全局配置 -Dfastjson.serializerFeatures.MapSortField=true
d) 通过类路径下的fastjson.properties来配置 fastjson.serializerFeatures.MapSortField=true
新老版本序列化和反序列化不兼容,会出乱码。
来源:https://www.cnblogs.com/shoren/p/fastjson.html
猜你喜欢
- 本文以实例形式详细讲述了Java的反射机制,是Java程序设计中重要的技巧。分享给大家供大家参考。具体分析如下:首先,Reflection是
- 1.分支结构的概念当需要进行条件判断并做出选择时,使用分支结构2.if分支结构格式:if(条件表达式){语句块;}package com.l
- Spring Cloud Feign简介 Spring Cloud Feign也是一个基础工具类,它整合了Spring Cloud Ribb
- 本文实例为大家分享了Java实现简易俄罗斯方块的具体代码,供大家参考,具体内容如下一、将对象抽象为类首先考虑俄罗斯方块游戏中含有哪些具体的对
- 最近在看《.NET游戏编程入门经典 C#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪
- 对某个类型中的方法进行拦截,然后加入固定的业务逻辑,这是AOP面向切面编程可以做的事,在springboot里实现aop的方法也有很多, s
- 本文实例为大家分享了Spring MVC接口防数据篡改和重复提交的具体代码,供大家参考,具体内容如下一、自定义一个注解,此注解可以使用在方法
- 本文实例为大家分享了java启动线程的方法,供大家参考,具体内容如下1.继承Threadpublic class java_thread e
- Maven Repository仓库的具体使用不知道大家是不是这样,反正我访问官网的时候不是非常慢就是崩溃,所以我就将我用过的Maven依赖
- 1,Java中操作方法:import java.io.*; public class FileInputStreamTest &
- 本文实例为大家分享了使用C#写一个时钟,供大家参考,具体内容如下时钟是这样的一共使用四个控件即可:WinFrom窗体应用程序代码:using
- 最近解决了一个令我头疼好久的问题,就是三星手机拍照图片旋转的问题,项目中有上传图片的功能,那么涉及到拍照,从相册中选择图片,别的手机都ok没
- 背景项目中我们经常会用搜索功能,普通的搜索我们可以用一个SQL的like也能实现匹配,但是搜索的核心需求是全文匹配,对于全文匹配,数据库的索
- typora-copy-images-to: ./一键清除maven仓库中下载失败的jar包maven是一款非常优秀的项目管理工具,特别是其
- 这篇效果和上一篇:https://www.jb51.net/article/100638.htm的效果是一样的,但是不再在OnTouchEv
- 一、AOP概述AOP,即面向切面编程,简单来说就是将代码中重复的部分抽取出来,在需要执行的时候使用 * 的技术,在不修改源码的基础上对方法
- 引言上文Android:实现一个自定义有限制区域的图例(角度自识别)涂鸦工具类(中)中我们已经实现了在复杂的异形区域中涂鸦,最后生成图片保存
- 我前面几篇博客中提到过.net中的事件与Windows事件的区别,本文讨论的是前者,也就是我们代码中经常用到的Event。Event很常见,
- 递归算法设计的基本思想是:对于一个复杂的问题,把原问题分解为若干个相对简单类同的子问题,继续下去直到子问题简单到能够直接求解,也就是说到了递
- 将字母全部转换为大写或小写,在C#编程中是一个非常常见的功能。在开发过程中,经常需要验证用户登录,用户在输入用户名时可能不区分大小写,如果我