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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import com.itwork.weixin.cp.config.WxCpConfiguration; import com.itwork.weixin.cp.properties.WxCpProperties; import com.itwork.weixin.cp.util.WXBizMsgCrypt; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
@Slf4j @RestController @RequestMapping("/cp/data/service/{corpId}") public class WxCpDataController {
@Autowired private WxCpProperties properties; @Autowired private WxCpConfiguration configuration;
@GetMapping(produces = "text/plain;charset=utf-8") public String authGet(@RequestParam(name = "msg_signature", required = false) String signature, @RequestParam(name = "timestamp", required = false) String timestamp, @RequestParam(name = "nonce", required = false) String nonce, @RequestParam(name = "echostr", required = false) String echostr) {
log.info("\n数据回调认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]", signature, timestamp, nonce, echostr);
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) { throw new IllegalArgumentException("请求参数非法,请核实!"); }
try { WXBizMsgCrypt crypt = new WXBizMsgCrypt(properties.getToken(), properties.getAesKey(), properties.getCorpId()); String echo = crypt.VerifyURL(signature, timestamp, nonce, echostr); log.info("明文信息:{}", echo); return echo; } catch (Exception e) { e.printStackTrace(); }
return "非法请求"; }
@PostMapping(produces = "application/xml; charset=UTF-8") public String post(@PathVariable("corpId") String corpId, @RequestBody String requestBody, @RequestParam("msg_signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce) { log.info("\n数据回调:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, timestamp, nonce, requestBody);
try {
final WxCpService wxCpService = WxCpConfiguration.getCpServices(corpId); WxCpXmlMessage inMessage = null; if (wxCpService == null) { final WxCpService cpService = configuration.setCpServices(corpId); inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, cpService.getWxCpConfigStorage(), timestamp, nonce, signature); }else { inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(), timestamp, nonce, signature); } this.route(corpId, inMessage); return "success"; } catch (Exception e) { e.printStackTrace(); }
return ""; }
private WxCpXmlOutMessage route(String corpId, WxCpXmlMessage message) { return WxCpConfiguration.getRouters(corpId).route(message); } }
|