处理xml消息中后缀为$n的节点在签名时可能错误的问题

This commit is contained in:
jinyu 2015-06-13 21:34:33 +08:00
parent 2275ddd97a
commit 5e1ff55326
12 changed files with 237 additions and 88 deletions

View File

@ -7,9 +7,9 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
/**
* 签名工具类
@ -23,15 +23,19 @@ import com.foxinmy.weixin4j.model.Consts;
public class MapUtil {
/**
* 连接字符串
* @param object 对象
* @param encoder 是否编码
* @param lowerCase 是否转换小写
* @param extra 附加对象
*
* @param object
* 对象
* @param encoder
* 是否编码
* @param lowerCase
* 是否转换小写
* @param extra
* 附加对象
* @return
*/
public static String toJoinString(Object object, boolean encoder,
boolean lowerCase, Map<String, String> extra) {
String text = JSON.toJSONString(object);
Map<String, String> map = new TreeMap<String, String>(
new Comparator<String>() {
@Override
@ -39,10 +43,15 @@ public class MapUtil {
return o1.compareTo(o2);
}
});
map.putAll(JSON.parseObject(text,
new TypeReference<Map<String, String>>() {
}));
JSONObject obj = null;
if (object instanceof String) {
obj = JSONObject.parseObject(object.toString());
} else {
obj = ListsuffixResultSerializer.serializeToJSON(object);
}
for (String key : obj.keySet()) {
map.put(key, obj.getString(key));
}
if (extra != null && !extra.isEmpty()) {
map.putAll(extra);
}
@ -51,9 +60,13 @@ public class MapUtil {
/**
* 连接字符串
* @param map 对象
* @param encoder 是否编码
* @param lowerCase 是否转换小写
*
* @param map
* 对象
* @param encoder
* 是否编码
* @param lowerCase
* 是否转换小写
* @return
*/
public static String toJoinString(Map<String, String> map, boolean encoder,

View File

@ -201,11 +201,9 @@ public class ReflectionUtil {
return field.getType().getSimpleName();
}
@SuppressWarnings("unused")
private static Field getAccessibleField(final Object object,
public static Field getAccessibleField(final Object object,
final String fieldName) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
for (Class<?> superClass = object.getClass(); superClass != Object.class;) {
try {
Field field = superClass.getDeclaredField(fieldName);
field.setAccessible(true);

View File

@ -0,0 +1,12 @@
package com.foxinmy.weixin4j.xml;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ListsuffixResult {
}

View File

@ -0,0 +1,142 @@
package com.foxinmy.weixin4j.xml;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.util.ReflectionUtil;
/**
* 后缀为_$n xml节点序列化
*
* @className ListsuffixResultSerializer
* @author jy
* @date 2015年3月24日
* @since JDK 1.7
* @see
*/
public class ListsuffixResultSerializer {
private static final String LISTSUFFIX_RESULT_NAME = "$listsuffix_result$";
/**
* 序列化为json
*
* @param object
* @return json
*/
public static JSONObject serializeToJSON(Object object) {
final JSONObject listsuffix = new JSONObject();
String preJson = JSON.toJSONString(object, new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object value) {
if (name.equalsIgnoreCase(LISTSUFFIX_RESULT_NAME)) {
listsuffix.put(LISTSUFFIX_RESULT_NAME, JSON.toJSON(value));
return false;
}
Field field = ReflectionUtil.getAccessibleField(source, name);
if (field != null
&& field.getAnnotation(ListsuffixResult.class) != null) {
listsuffix.put(LISTSUFFIX_RESULT_NAME, JSON.toJSON(value));
return false;
}
return true;
}
});
JSONObject result = JSON.parseObject(preJson);
try {
Map<String, String> listMap = listsuffixConvertMap(listsuffix
.getJSONArray(LISTSUFFIX_RESULT_NAME));
result.putAll(listMap);
} catch (JSONException e) {
;//
}
return result;
}
/**
* list对象转换为map的$n形式
*
* @param listsuffix
* @return
*/
public static Map<String, String> listsuffixConvertMap(List<?> listsuffix) {
Map<String, String> listMap = new HashMap<String, String>();
if (listsuffix != null && !listsuffix.isEmpty()) {
for (int i = 0; i < listsuffix.size(); i++) {
listMap.putAll(JSON.parseObject(JSON.toJSONString(
listsuffix.get(i), new ListsuffixEndNameFilter(i)),
new TypeReference<Map<String, String>>() {
}));
}
}
return listMap;
}
private static class ListsuffixEndNameFilter implements NameFilter {
private final int index;
public ListsuffixEndNameFilter(int index) {
this.index = index;
}
@Override
public String process(Object object, String name, Object value) {
return String.format("%s_%d", name, index);
}
}
/**
* 序列化为xml
*
* @param object
* @return xml
*/
public static String serializeToXML(Object object) {
JSONObject obj = serializeToJSON(object);
StringWriter sw = new StringWriter();
XMLStreamWriter xw = null;
try {
xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
xw.writeStartDocument(Consts.UTF_8.name(), "1.0");
xw.writeStartElement("xml");
for (String key : obj.keySet()) {
xw.writeStartElement(key);
xw.writeCData(obj.getString(key));
xw.writeEndElement();
}
xw.writeEndElement();
xw.writeEndDocument();
} catch (XMLStreamException e) {
e.printStackTrace();
} finally {
if (xw != null) {
try {
xw.close();
} catch (XMLStreamException e) {
;
}
}
try {
sw.close();
} catch (IOException e) {
;
}
}
return sw.getBuffer().toString();
}
}

View File

@ -31,15 +31,15 @@ import com.foxinmy.weixin4j.xml.ListWrapper;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
* 后缀为_$n xml节点转换
* 后缀为_$n xml节点反序列化
*
* @className ListsuffixResultConverter
* @className ListsuffixResultDeserializer
* @author jy
* @date 2015年3月24日
* @since JDK 1.7
* @see
*/
public class ListsuffixResultConverter {
public class ListsuffixResultDeserializer {
private final static Pattern SUFFIX_PATTERN = Pattern.compile("(_\\d)$");
@ -53,8 +53,8 @@ public class ListsuffixResultConverter {
* @param clazz
* @return
*/
public static <T> T containCouponConvert(String content, Class<T> clazz) {
return convert(content, clazz, "couponList");
public static <T> T containCouponDeserialize(String content, Class<T> clazz) {
return deserialize(content, clazz, "couponList");
}
/**
@ -64,8 +64,8 @@ public class ListsuffixResultConverter {
* @param clazz
* @return
*/
public static <T> T containRefundConvert(String content, Class<T> clazz) {
return convert(content, clazz, "refundList");
public static <T> T containRefundDeserialize(String content, Class<T> clazz) {
return deserialize(content, clazz, "refundList");
}
/**
@ -75,7 +75,7 @@ public class ListsuffixResultConverter {
* @param clazz
* @return
*/
public static <T> T containRefundDetailConvert(String content,
public static <T> T containRefundDetailDeserialize(String content,
Class<T> clazz) {
T t = XmlStream.fromXML(content, clazz);
Class<?> wrapperClazz = ReflectionUtil.getFieldGenericType(t,
@ -205,7 +205,7 @@ public class ListsuffixResultConverter {
return t;
}
public static <T> T convert(String content, Class<T> clazz,
public static <T> T deserialize(String content, Class<T> clazz,
String listPropertyName) {
T t = XmlStream.fromXML(content, clazz);
Class<?> wrapperClazz = ReflectionUtil.getFieldGenericType(t,

View File

@ -1,33 +0,0 @@
package com.foxinmy.weixin4j.mp.payment.conver;
/**
* 后缀为_$n xml节点序列化
*
* @className ListsuffixResultSerializer
* @author jy
* @date 2015年3月24日
* @since JDK 1.7
* @see
*/
public class ListsuffixResultSerializer {
/**
* 序列化为json
*
* @param object
* @return json
*/
public String serializeToJSON(Object object) {
return null;
}
/**
* 序列化为xml
*
* @param object
* @return xml
*/
public String serializeToXML(Object object) {
return null;
}
}

View File

@ -9,7 +9,6 @@ import javax.xml.bind.annotation.XmlRootElement;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.mp.type.SignType;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 调用V2.x接口返回的公用字段
@ -28,13 +27,13 @@ public class ApiResult implements Serializable {
/**
* 是查询结果状态码,0 表明成功,其他表明错误;
*/
@JSONField(name = "ret_code")
@JSONField(name = "retcode")
@XmlElement(name = "retcode")
private int retCode;
private Integer retCode;
/**
* 是查询结果出错信息;
*/
@JSONField(name = "ret_msg")
@JSONField(name = "retmsg")
@XmlElement(name = "retmsg")
private String retMsg;
/**
@ -64,11 +63,11 @@ public class ApiResult implements Serializable {
@XmlElement(name = "sign_type")
private SignType signType;
protected ApiResult(){
protected ApiResult() {
// jaxb required
}
public int getRetCode() {
public Integer getRetCode() {
return retCode;
}
@ -77,7 +76,7 @@ public class ApiResult implements Serializable {
}
public String getRetMsg() {
return StringUtil.isNotBlank(retMsg) ? retMsg : null;
return this.retMsg;
}
public void setRetMsg(String retMsg) {

View File

@ -9,6 +9,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.xml.ListsuffixResult;
/**
* V2退款记录
@ -42,18 +43,19 @@ public class RefundRecord extends ApiResult {
*/
@XmlElement(name = "refund_count")
@JSONField(name = "refund_count")
private int count;
private int refundCount;
/**
* 退款详情
*/
@ListsuffixResult
@XmlTransient
@JSONField(serialize = false, deserialize = false)
@JSONField(deserialize = false)
private List<RefundDetail> refundList;
protected RefundRecord() {
// jaxb required
}
public String getTransactionId() {
return transactionId;
}
@ -62,8 +64,8 @@ public class RefundRecord extends ApiResult {
return outTradeNo;
}
public int getCount() {
return count;
public int getRefundCount() {
return refundCount;
}
public List<RefundDetail> getRefundList() {
@ -77,7 +79,7 @@ public class RefundRecord extends ApiResult {
@Override
public String toString() {
return "RefundRecord [transactionId=" + transactionId + ", outTradeNo="
+ outTradeNo + ", count=" + count + ", refundList="
+ outTradeNo + ", refundCount=" + refundCount + ", refundList="
+ refundList + ", " + super.toString() + "]";
}
}

View File

@ -16,6 +16,7 @@ import com.foxinmy.weixin4j.mp.type.TradeState;
import com.foxinmy.weixin4j.mp.type.TradeType;
import com.foxinmy.weixin4j.util.DateUtil;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.ListsuffixResult;
/**
* V3订单信息
@ -86,8 +87,9 @@ public class Order extends ApiResult {
/**
* 代金券信息 验证签名有点麻烦
*/
@ListsuffixResult
@XmlTransient
@JSONField(serialize = false, deserialize = false)
@JSONField(deserialize = false)
private List<CouponInfo> couponList;
/**
* 现金支付金额

View File

@ -14,6 +14,7 @@ import com.foxinmy.weixin4j.mp.type.CurrencyType;
import com.foxinmy.weixin4j.mp.type.RefundChannel;
import com.foxinmy.weixin4j.mp.type.RefundStatus;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.ListsuffixResult;
/**
* V3退款详细
@ -129,14 +130,15 @@ public class RefundDetail extends ApiResult {
*
* @see com.foxinmy.weixin4j.mp.payment.coupon.CouponInfo
*/
@ListsuffixResult
@XmlTransient
@JSONField(serialize = false, deserialize = false)
@JSONField(deserialize = false)
private List<CouponInfo> couponList;
protected RefundDetail() {
// jaxb required
}
public String getOutRefundNo() {
return outRefundNo;
}

View File

@ -10,6 +10,7 @@ import javax.xml.bind.annotation.XmlTransient;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.mp.type.CurrencyType;
import com.foxinmy.weixin4j.xml.ListsuffixResult;
/**
* V3退款记录
@ -82,20 +83,21 @@ public class RefundRecord extends ApiResult {
*/
@XmlElement(name = "refund_count")
@JSONField(name = "refund_count")
private int count;
private int refundCount;
/**
* 退款详情
*
* @see com.foxinmy.weixin4j.mp.payment.v3.RefundDetail
*/
@ListsuffixResult
@XmlTransient
@JSONField(serialize = false, deserialize = false)
@JSONField(deserialize = false)
private List<RefundDetail> refundList;
protected RefundRecord() {
// jaxb required
}
public String getTransactionId() {
return transactionId;
}
@ -154,8 +156,8 @@ public class RefundRecord extends ApiResult {
return totalFee;
}
public int getCount() {
return count;
public int getRefundCount() {
return refundCount;
}
public List<RefundDetail> getRefundList() {
@ -187,7 +189,7 @@ public class RefundRecord extends ApiResult {
+ ", feeType=" + feeType + ", cashFee=" + getFormatCashFee()
+ ", cashFeeType=" + cashFeeType + ", refundFee="
+ getFormatRefundFee() + ", couponRefundFee="
+ getFormatCouponRefundFee() + ", count=" + count
+ getFormatCouponRefundFee() + ", refundCount=" + refundCount
+ ", refundList=" + refundList + ", " + super.toString() + "]";
}
}

View File

@ -6,9 +6,11 @@ import java.util.HashMap;
import java.util.Map;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.payment.PayUtil;
import com.foxinmy.weixin4j.mp.payment.conver.ListsuffixResultDeserializer;
import com.foxinmy.weixin4j.mp.payment.v2.RefundRecord;
import com.foxinmy.weixin4j.mp.payment.v3.Order;
import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
import com.foxinmy.weixin4j.xml.XmlStream;
public class XmlstreamTest {
@ -60,11 +62,11 @@ public class XmlstreamTest {
} catch (Exception e) {
}
System.err.println(ListsuffixResultDeserializer.containCouponDeserialize(
sb.toString(), Order.class));
System.err.println(ListsuffixResultDeserializer
.containCouponDeserialize(sb.toString(), Order.class));
}
public static void xml2refundRecordV2() throws Exception {
public static RefundRecord xml2refundRecordV2() throws Exception {
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(
@ -77,8 +79,8 @@ public class XmlstreamTest {
} catch (Exception e) {
}
System.err.println(ListsuffixResultDeserializer.containRefundDeserialize(
sb.toString(), RefundRecord.class));
return ListsuffixResultDeserializer.containRefundDeserialize(
sb.toString(), RefundRecord.class);
}
public static void xml2refundRecordV3() throws Exception {
@ -106,5 +108,13 @@ public class XmlstreamTest {
// xml2refundRecordV2();
// xml2refundRecordV3();
// object2xmlWithoutRootElement();
RefundRecord refundRecord = xml2refundRecordV2();
System.err.println(refundRecord);
String sign = refundRecord.getSign();
refundRecord.setSign(null);
String validSign = PayUtil.paysignMd5(refundRecord,
"GATFzDwbQdbbci3QEQxX2rUBvwTrsMiZ");
System.err.println("sign=" + sign + ",validSign=" + validSign);
System.err.println(ListsuffixResultSerializer.serializeToXML(refundRecord));
}
}