微信第三方平台【一】获取验证票据 component_verify_ticket,授权结果接收

在第三方平台创建审核通过后,微信服务器会向其 ”授权事件接收URL” 每隔 10 分钟以 POST 的方式推送 component_verify_ticke,

推送到程序后,需要根据微信官方提供的加解密代码,进行解密。拿到ticket之后,mysql保存一下【我自己是redis一份,mysql一份】

接收验证票据和授权结果的URL为,申请第三方平台的时候,填写的“授权事件接收URL

上代码:

public String componentVerifyTicket(HttpServletRequest request, HttpServletResponse response) {
    log.info(ThirdRunTimeInfoConstant.START_RECEIVE);
    try {
        request.setCharacterEncoding(“utf-8”);
        response.setCharacterEncoding(“utf-8”);
        // 微信加密签名
        String msgSignature = request.getParameter(“msg_signature”);
        // 时间戳
        String timeStamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        String encryptType = request.getParameter("encrypt_type");
        String signature = request.getParameter("signature");
        log.info(String.format(ThirdRunTimeInfoConstant.VERIFY_TICKET,msgSignature,timeStamp,nonce,encryptType,signature));
        // 从请求中读取整个post数据
        InputStream inputStream;
        String postData = null;
        inputStream = request.getInputStream();
        postData= IOUtils.toString(inputStream,ModuleConstant.UTF8);
        WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(token,aesKey,componentAppId);
        String msg = wxBizMsgCrypt.decryptMsg(msgSignature, timeStamp, nonce, postData);
        log.info(String.format(ThirdRunTimeInfoConstant.MSG,msg));
        // 将xml转为map
        Map result = WXXmlToMapUtil.xmlToMap(msg);
        String infotype = result.get("InfoType");
        log.info(String.format(ThirdRunTimeInfoConstant.INFO_TYPE,infotype));
        switch (infotype){
            case "component_verify_ticket":   //接收票据 【component_verify_ticket】
                String ComponentVerifyTicket = result.get(ModuleConstant.TICKET_KEY);
                log.info(String.format(COMPONENT_VERIFY_TICKET,ComponentVerifyTicket));
                // 缓存 ticket
                redisThird.setComponentVerifyTicket(ComponentVerifyTicket);
                // 存数据库
                ComponentVerifyTicketFormDTO ticketFormDTO = new ComponentVerifyTicketFormDTO();
                ticketFormDTO.setComponentAppId(componentAppId);
                ticketFormDTO.setComponentVerifyTicket(ComponentVerifyTicket);
                //先逻辑删
                ticketDao.deleteOldComponentVerifyTicket();
                ticketDao.insertComponentVerifyTicket(ticketFormDTO);
                log.info(ModuleConstant.TICKET_UNDERLINE_KEY+":"+ComponentVerifyTicket);
                break;
            case "authorized": //授权成功
                this.disposeAuthResult(result);
                log.info(AUTH_STATUS);
                break;
            case "unauthorized"://用户取消授权
                //todo 取消授权
                String authorizerAppId = result.get(ModuleConstant.AUTHORIZER_APP_ID_HUMP);
                customerMpDao.updateAuthorizationFlag(authorizerAppId);
                this.disposeAuthResult(result);
                log.info(REMOVE_AUTH);
                break;
            case "updateauthorized"://授权变更
                this.disposeAuthResult(result);
                log.info(UPDATE_AUTH);
                break;
        }
    } catch (Exception e) {
        log.error(e.getMessage());
        return ModuleConstant.SUCCESS;
    }
    return ModuleConstant.SUCCESS;
}

不论代码是否出错,最终一定要反回  “success”,是个死值。

收权结果,接收到后,根据项目情况写自己的逻辑

你可能感兴趣的:(微信第三方平台,java,小程序)