Search in sources :

Example 1 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlMessage in project weixin-java-mp-multi-demo by binarywang.

the class AbstractWxPortalController method post.

@ResponseBody
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("signature") String signature, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.getWxService().checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.getWxService().getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)

Example 2 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlMessage in project fw-cloud-framework by liuweijw.

the class WechatController method post.

@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.wxService.getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlMessage in project weixin-java-mp-multi-demo by binarywang.

the class AbstractWxPortalController method post.

@ResponseBody
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("signature") String signature, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.getWxService().checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.getWxService().getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)

Example 4 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlMessage in project weixin-java-mp-demo-springboot by binarywang.

the class WechatController method post.

@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.wxService.getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)

Example 5 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlMessage in project summer by foxsugar.

the class WechatAction method wechatCore.

@RequestMapping(value = "/core")
public void wechatCore(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    String signature = request.getParameter("signature");
    String nonce = request.getParameter("nonce");
    String timestamp = request.getParameter("timestamp");
    String oto = configStorage.getToken();
    if (!this.wxMpService.checkSignature(timestamp, nonce, signature)) {
        // 消息签名不正确,说明不是公众平台发过来的消息
        response.getWriter().println("非法请求");
        return;
    }
    String echoStr = request.getParameter("echostr");
    if (StringUtils.isNotBlank(echoStr)) {
        // 说明是一个仅仅用来验证的请求,回显echostr
        String echoStrOut = String.copyValueOf(echoStr.toCharArray());
        response.getWriter().println(echoStrOut);
        return;
    }
    String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type");
    if ("raw".equals(encryptType)) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            response.getWriter().write("");
        } else {
            response.getWriter().write(outMessage.toXml());
        }
        return;
    }
    if ("aes".equals(encryptType)) {
        // 是aes加密的消息
        String msgSignature = request.getParameter("msg_signature");
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), this.configStorage, timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            response.getWriter().write("");
        } else {
            response.getWriter().write(outMessage.toEncryptedXml(this.configStorage));
        }
        return;
    }
    response.getWriter().println("不可识别的加密类型");
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WxMpXmlMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlMessage)6 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1