java处理微信第三方平台获取ComponentVerifyTicket

所需import

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.interfaces.component.entity.ComponentVerifyTicket;
import org.jeecg.modules.interfaces.component.service.ComponentVerifyTicketService;
import org.jeecg.modules.interfaces.scrmauthrecord.service.ScrmAuthRecordService;
import org.jeecg.modules.interfaces.scrmofficialaccounts.entity.ScrmOfficialAccounts;
import org.jeecg.modules.interfaces.scrmofficialaccounts.service.ScrmOfficialAccountsService;
import org.jeecg.qywx.wxapi.mp.aes.WXBizMsgCrypt;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.Date;
import java.util.Map;

参数说明:
nonce/timestamp/signature/msg_signature/encrypt_type都是微信主动推送的,只需要接受即可

encodingAesKey/appid/token 是申请平台的时候设置的参数,找申请人员要即可

主要代码

	@ApiOperation(value = "微信验证票据回调接口")
    @RequestMapping(value = "/notify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        log.info("开始获取微信推送的数据------");
        Map<String, String[]> map = request.getParameterMap();
        String nonce = map.get("nonce")[0];
        String timestamp = map.get("timestamp")[0];
        String signature = map.get("signature")[0];
        String msg_signature = map.get("msg_signature")[0];
        String encrypt_type = map.get("encrypt_type")[0];
        log.info("微信回调event中获取微信回调的信息-----------------data:{}",request.getQueryString());
        if (StringUtils.isBlank(msg_signature)) {
            log.info("没有获取到微信发送过来的消息----------");
        }else{
            try{
                StringBuffer sb = new StringBuffer();
                BufferedReader in = request.getReader();
                String line;
                while((line=in.readLine())!=null){
                    sb.append(line);
                }
                //动态获取数据库配置
                ScrmOfficialAccounts scrmOfficialAccounts = new ScrmOfficialAccounts();
                scrmOfficialAccounts = scrmOfficialAccountsService.getOne(new LambdaQueryWrapper<>(scrmOfficialAccounts));
                String xml = sb.toString();
                log.info("获取解密前的postdata的xml:{}",xml);
                WXBizMsgCrypt componentAccount = new WXBizMsgCrypt(scrmOfficialAccounts.getToken(), scrmOfficialAccounts.getEncodingAesKey(), scrmOfficialAccounts.getAppid());
                  xml = componentAccount.decryptMsg(msg_signature, timestamp, nonce, xml);
                log.info("获取微信推送的数据解密后明文xml:{}",xml);

                Document document = DocumentHelper.parseText(xml);
                Element rootElement = document.getRootElement();
                String infoType = rootElement.elementText("InfoType");
                log.info("数据类型为--------------------infoType:{}",infoType);
                if(infoType != null){
                    if("component_verify_ticket".equals(infoType)){
                        String componentVerifyTicket = rootElement.elementText("ComponentVerifyTicket");
                        log.info("获取componentVerifyTicket:{}",componentVerifyTicket);
                        String expiresInStr = rootElement.elementText("expiresIn");
                        long expireIn = 0;
                        if(expiresInStr!=null){
                            expireIn = Long.parseLong(expiresInStr);
                        }
                        ComponentVerifyTicket ticket = new ComponentVerifyTicket();
                        ticket.setSomeVerifyTicket(componentVerifyTicket);
                        componentVerifyTicketService.remove(new LambdaQueryWrapper<>());
                        ticket.setAppid(scrmOfficialAccounts.getAppid());
                        ticket.setCreateTime(String.valueOf(new Date().getTime()));
                        ticket.setInfoType(infoType);
                        componentVerifyTicketService.save(ticket);
                    }
                }else{
                    log.info("微信关于账号绑定,解绑,更新账号绑定信息,获取ticket信息出现错误------------");
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

重点

此处需要修改jdk
https://blog.csdn.net/myfmyfmyfmyf/article/details/80049860
具体方法参照该例子,如果要上服务器端也需修改服务器端jdk配置

你可能感兴趣的:(java)