接入公众号消息
文章内索引
[显示]
验签-明文-解析json
1 2 3 4 5 6 7 8 |
<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> |
1 |
{"Content":"123","CreateTime":"1584081014","ToUserName":"gh_f26861df06bf","FromUserName":"oCGwSs6qq3pndIphSRJ8MBcYpmng","MsgType":"text","MsgId":"22678587759161276"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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
1 2 3 4 |
<xml> <ToUserName><![CDATA[gh_f26861df06bf]]></ToUserName> <Encrypt><![CDATA[6fORtxGyApnwS2+xfCO3mutjMCaVD2PUkqg6n6VnmdUdzE4JgJVXKtLrW30lx8U2hBacRLP0NQxLilFtPj8VBKmk9NQyQuCFmGmGTu0WfGDhsyPIg0v0cxdCs14aYsEGIbndv9pUXgidVNlXIH3JLpLHbrM/Fj8S9qEhrimXQ2V6HHLvZ5xC1wrIdkL0Q7cnmdDcKoKVZw0hpn34MEu2AuDPps55A9G1ES2w9VqWrVVUxdIETtFeqD0Mretz3xpsBdpMdV6G16mhoC134z2Zvb2A2755Iojh0P7s/WfrneKPoQi0jAr84J9FdS7FHjenfF7AruyaBQ5wNkSsrUxVGkiu/eBuZeqa5yTNuvDmlSPYgg29lX1LYbm6EzWpwNASyGmAoJGe69lbzjLa3PKovVDs/1xnr+cVMmCAljALVeQ=]]></Encrypt> </xml> |
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论