最近因為fastjson安全漏洞,升級jar包時,踩了一些坑。
創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)服務(wù)商,為中小企業(yè)提供網(wǎng)站制作、做網(wǎng)站服務(wù),網(wǎng)站設(shè)計,網(wǎng)站托管、服務(wù)器托管等一站式綜合服務(wù)型公司,專業(yè)打造企業(yè)形象網(wǎng)站,讓您在眾多競爭對手中脫穎而出創(chuàng)新互聯(lián)建站。
背景:
使用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初始化時,設(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過程中,又要判斷Content-Type不能含有通配符,這應(yī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>
這個是一個簽名算法的場景:客戶端對參數(shù)進行序列化,然后md5加密成一個簽名;服務(wù)端按照相同的算法解析一遍參數(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)鏈接
本文題目:解決fastjson從1.1.41升級到1.2.28后報錯問題詳解
URL地址:http://muchs.cn/article26/jchhcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、云服務(wù)器、商城網(wǎng)站、Google、App設(shè)計、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)