解決fastjson從1.1.41升級(jí)到1.2.28后報(bào)錯(cuò)問(wèn)題詳解
最近因?yàn)閒astjson安全漏洞,升級(jí)jar包時(shí),踩了一些坑。
新版本FastJsonHttpMessageConverter初始化,默認(rèn)設(shè)置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升級(jí)到1.2.28之后,請(qǐng)求報(bào)錯(cuò):
json java.lang.IllegalArgumentException: 'Content-Type' cannot contain wildcard type '*'
原因是在1.1.41中,F(xiàn)astJsonHttpMessageConverter初始化時(shí),設(shè)置了MediaType。
public FastJsonHttpMessageConverter(){
super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8));
}
而在1.2.28中,設(shè)置的MediaType為‘/',即:
public FastJsonHttpMessageConverter() {
super(MediaType.ALL); // */*
}
后續(xù)在org.springframework.http.converter.AbstractHttpMessageConverter.write過(guò)程中,又要判斷Content-Type不能含有通配符,這應(yīng)該是一種保護(hù)機(jī)制,并強(qiáng)制用戶自己配置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) {
//設(shè)置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設(shè)置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>
新版本序列化默認(rèn)不再對(duì)字段進(jìn)行排序
這個(gè)是一個(gè)簽名算法的場(chǎng)景:客戶端對(duì)參數(shù)進(jìn)行序列化,然后md5加密成一個(gè)簽名;服務(wù)端按照相同的算法解析一遍參數(shù),對(duì)比簽名值。這里加密依賴json序列化之后的字符串,也就依賴序列化時(shí)字段的排序。
這是fastjson做了一個(gè)性能優(yōu)化,將排序需求抽象出一個(gè)SerializerFeature,供用戶自己配置。如果需要排序場(chǎng)景,在序列化時(shí)添加參數(shù)SerializerFeature.MapSortField即可,即:
JSON.toJSONString(obj, SerializerFeature.MapSortField);
官方文檔
1.2.3之后的版本,Map的序列化沒(méi)有做排序再輸出,原因是通過(guò)TreeMap排序很影響性能。
1.2.27版本中增加SerializerFeature.MapSortField實(shí)現(xiàn)同樣的功能。
使用方法如下:
a) 傳入SerializerFeature.MapSortField參數(shù)。 JSON.toJSONString(map, SerializerFeature.MapSortField);
b) 通過(guò)代碼修改全局缺省配置。 JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.MapSortField.getMask();
c) 通過(guò)JVM啟動(dòng)參數(shù)配置修改全局配置 -Dfastjson.serializerFeatures.MapSortField=true
d) 通過(guò)類路徑下的fastjson.properties來(lái)配置 fastjson.serializerFeatures.MapSortField=true
新老版本序列化和反序列化不兼容,會(huì)出亂碼。
更多關(guān)于fastjson的相關(guān)文章請(qǐng)點(diǎn)擊下面的相關(guān)鏈接
相關(guān)文章
Java使用NIO優(yōu)化IO實(shí)現(xiàn)文件上傳下載功能
IO 是基于流來(lái)讀取的,而NIO則是基于塊讀取,面向流 的 I/O 系統(tǒng)一次一個(gè)字節(jié)地處理數(shù)據(jù),這篇文章主要介紹了Java使用NIO優(yōu)化IO實(shí)現(xiàn)文件上傳下載功能,需要的朋友可以參考下2022-07-07
Java Apache Shiro安全框架快速開發(fā)詳解流程
Apache Shiro是一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會(huì)話管理。使用Shiro的易于理解的API,您可以快速、輕松地獲得任何應(yīng)用程序,從最小的移動(dòng)應(yīng)用程序到最大的網(wǎng)絡(luò)和企業(yè)應(yīng)用程序2021-10-10
SpringBoot部署和前端連接問(wèn)題解決的完整指南(net::ERR_CONNECTION_REFUSED)
在開發(fā)和部署 Spring Boot 應(yīng)用時(shí),可能會(huì)遇到各種問(wèn)題,例如 JAR 文件無(wú)法運(yùn)行、前端無(wú)法連接后端服務(wù)等,本文將詳細(xì)總結(jié)這些問(wèn)題的解決方法,幫助你順利部署和運(yùn)行 Spring Boot 應(yīng)用,需要的朋友可以參考下2025-01-01
Java零基礎(chǔ)也看得懂的單例模式與final及抽象類和接口詳解
本文主要講了單例模式中的餓漢式和懶漢式的區(qū)別,final的使用,抽象類的介紹以及接口的具體內(nèi)容,感興趣的朋友來(lái)看看吧2022-05-05
Shell重啟SpringBoot項(xiàng)目腳本的示例代碼(含服務(wù)守護(hù))
本文介紹了如何使用?Bash?腳本來(lái)管理和守護(hù)運(yùn)行服務(wù),將展示一個(gè)示例腳本,該腳本可以停止、啟動(dòng)和守護(hù)運(yùn)行一個(gè)服務(wù),并提供了相應(yīng)的解釋和用法說(shuō)明,文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
spring?boot整合mongo查詢converter異常排查記錄
這篇文章主要為大家介紹了spring?boot整合mongo查詢時(shí)拋出converter異常的排查解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03

