接入公众号消息

验签-明文-解析json

<xml>
<ToUserName><![CDATA[gh_f26861df06bf]]></ToUserName>
<FromUserName><![CDATA[oCGwSs6qq3pndIphSRJ8MBcYpmng]]></FromUserName>
<CreateTime>1584081014</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[123]]></Content>
<MsgId>22678587759161276</MsgId>
</xml>
{"Content":"123","CreateTime":"1584081014","ToUserName":"gh_f26861df06bf","FromUserName":"oCGwSs6qq3pndIphSRJ8MBcYpmng","MsgType":"text","MsgId":"22678587759161276"}
package com.akucun.member.aggregation.facade.controller.wechat; import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays; /** * Crated by Hanson on 2020/3/9 22:21 */
@RestController
@RequestMapping(value = "/wechat/public/plain")
@Slf4j
public class WeChatPublicPlaintextController { @Value("${wechat.public.token:OfiMH56z64WcyzKZQ3}") private String token; @GetMapping public String validateToken(String signature,String timestamp,String nonce,String echostr){ if(checkSignature(signature, timestamp, nonce)){ log.info("wechat validate token success echostr:{}",echostr); return echostr; } log.warn("wechat validate token failed sign:{},timestamp:{},nonce:{}",signature,timestamp,nonce); return null; } XmlMapper xmlMapper = new XmlMapper(); @PostMapping public String handleMessage(@RequestBody String content){ log.info("rec msg:{}",content); JSONObject param = null; try { param = xmlMapper.readValue(content, JSONObject.class); log.info("rec json:{}",param.toJSONString()); } catch (IOException e) { log.error("wechat call back parse xml error, content:{}",content,e); } return "success"; } private boolean checkSignature(String signature,String timestamp,String nonce){ String[] arr = {token,timestamp,nonce}; Arrays.sort(arr); StringBuffer sb = new StringBuffer(); for(String s : arr){ sb.append(s); } String temp = null; try { temp = getSha1(sb.toString()); } catch (NoSuchAlgorithmException e) { log.warn("decrypt failed",e); } return temp.equals(signature); } private String getSha1(String input) throws NoSuchAlgorithmException { MessageDigest mDigest = MessageDigest.getInstance("SHA1"); byte[] result = mDigest.digest(input.getBytes()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < result.length; i++) { sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); }
}

验签-密文-解析-json

<xml> <ToUserName><![CDATA[gh_f26861df06bf]]></ToUserName> <Encrypt><![CDATA[6fORtxGyApnwS2+xfCO3mutjMCaVD2PUkqg6n6VnmdUdzE4JgJVXKtLrW30lx8U2hBacRLP0NQxLilFtPj8VBKmk9NQyQuCFmGmGTu0WfGDhsyPIg0v0cxdCs14aYsEGIbndv9pUXgidVNlXIH3JLpLHbrM/Fj8S9qEhrimXQ2V6HHLvZ5xC1wrIdkL0Q7cnmdDcKoKVZw0hpn34MEu2AuDPps55A9G1ES2w9VqWrVVUxdIETtFeqD0Mretz3xpsBdpMdV6G16mhoC134z2Zvb2A2755Iojh0P7s/WfrneKPoQi0jAr84J9FdS7FHjenfF7AruyaBQ5wNkSsrUxVGkiu/eBuZeqa5yTNuvDmlSPYgg29lX1LYbm6EzWpwNASyGmAoJGe69lbzjLa3PKovVDs/1xnr+cVMmCAljALVeQ=]]></Encrypt>
</xml>

 


©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注