Search in sources :

Example 1 with WxCpCryptUtil

use of me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil in project weixin-java-tools by chanjarster.

the class WxCpXmlMessage method fromEncryptedXml.

/**
   * 从加密字符串转换
   *
   * @param encryptedXml
   * @param wxCpConfigStorage
   * @param timestamp
   * @param nonce
   * @param msgSignature
   * @return
   */
public static WxCpXmlMessage fromEncryptedXml(String encryptedXml, WxCpConfigStorage wxCpConfigStorage, String timestamp, String nonce, String msgSignature) {
    WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
    String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
    return fromXml(plainText);
}
Also used : WxCpCryptUtil(me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil)

Example 2 with WxCpCryptUtil

use of me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil in project weixin-java-tools by chanjarster.

the class WxCpXmlOutMessage method toEncryptedXml.

/**
   * 转换成加密的xml格式
   * @return
   */
public String toEncryptedXml(WxCpConfigStorage wxCpConfigStorage) {
    String plainXml = toXml();
    WxCpCryptUtil pc = new WxCpCryptUtil(wxCpConfigStorage);
    return pc.encrypt(plainXml);
}
Also used : WxCpCryptUtil(me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil)

Example 3 with WxCpCryptUtil

use of me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil in project weixin-java-tools by chanjarster.

the class WxCpEndpointServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    String msgSignature = request.getParameter("msg_signature");
    String nonce = request.getParameter("nonce");
    String timestamp = request.getParameter("timestamp");
    String echostr = request.getParameter("echostr");
    if (StringUtils.isNotBlank(echostr)) {
        if (!wxCpService.checkSignature(msgSignature, timestamp, nonce, echostr)) {
            // 消息签名不正确,说明不是公众平台发过来的消息
            response.getWriter().println("非法请求");
            return;
        }
        WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
        String plainText = cryptUtil.decrypt(echostr);
        // 说明是一个仅仅用来验证的请求,回显echostr
        response.getWriter().println(plainText);
        return;
    }
    WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
    WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
    if (outMessage != null) {
        response.getWriter().write(outMessage.toEncryptedXml(wxCpConfigStorage));
    }
    return;
}
Also used : WxCpCryptUtil(me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil) WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) WxCpXmlOutMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutMessage)

Aggregations

WxCpCryptUtil (me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil)3 WxCpXmlMessage (me.chanjar.weixin.cp.bean.WxCpXmlMessage)1 WxCpXmlOutMessage (me.chanjar.weixin.cp.bean.WxCpXmlOutMessage)1