解決fastjson從1.1.41升級到1.2.28后報錯問題詳解
最近因為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中,F(xiàn)astJsonHttpMessageConverter初始化時,設置了MediaType。
public FastJsonHttpMessageConverter(){ super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8)); }
而在1.2.28中,設置的MediaType為‘/',即:
public FastJsonHttpMessageConverter() { super(MediaType.ALL); // */* }
后續(xù)在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>
新版本序列化默認不再對字段進行排序
這個是一個簽名算法的場景:客戶端對參數(shù)進行序列化,然后md5加密成一個簽名;服務端按照相同的算法解析一遍參數(shù),對比簽名值。這里加密依賴json序列化之后的字符串,也就依賴序列化時字段的排序。
這是fastjson做了一個性能優(yōu)化,將排序需求抽象出一個SerializerFeature,供用戶自己配置。如果需要排序場景,在序列化時添加參數(shù)SerializerFeature.MapSortField即可,即:
JSON.toJSONString(obj, SerializerFeature.MapSortField);
官方文檔
1.2.3之后的版本,Map的序列化沒有做排序再輸出,原因是通過TreeMap排序很影響性能。
1.2.27版本中增加SerializerFeature.MapSortField實現(xiàn)同樣的功能。
使用方法如下:
a) 傳入SerializerFeature.MapSortField參數(shù)。 JSON.toJSONString(map, SerializerFeature.MapSortField);
b) 通過代碼修改全局缺省配置。 JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.MapSortField.getMask();
c) 通過JVM啟動參數(shù)配置修改全局配置 -Dfastjson.serializerFeatures.MapSortField=true
d) 通過類路徑下的fastjson.properties來配置 fastjson.serializerFeatures.MapSortField=true
新老版本序列化和反序列化不兼容,會出亂碼。
更多關(guān)于fastjson的相關(guān)文章請點擊下面的相關(guān)鏈接
相關(guān)文章
詳解SpringBoot結(jié)合swagger2快速生成簡單的接口文檔
這篇文章主要介紹了詳解SpringBoot結(jié)合swagger2快速生成簡單的接口文檔,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05java實現(xiàn)隊列queue數(shù)據(jù)結(jié)構(gòu)詳解
大家好,本篇文章主要講的是java實現(xiàn)隊列queue數(shù)據(jù)結(jié)構(gòu)詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02mybatis-4 mybatis與spring結(jié)合使用及原理解析
本文通過圖文并茂的形式給大家介紹了mybatis-4 mybatis與spring結(jié)合使用及原理解析,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-04-04