Browse Source

微信第三方平台

dev_shibei_match
zxc 5 years ago
parent
commit
713d3e408e
  1. 57
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java
  2. 49
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java
  3. 1
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AuthRedirectController.java
  4. 66
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java

57
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java

@ -6,63 +6,40 @@ package com.epmet.constant;
*/
public interface ModuleConstant {
String TICKET_KEY = "ComponentVerifyTicket";
String FUNC_INFO = "func_info";
//获得授权事件的票据 如下
String UTF8 = "UTF-8";
String MSG_SIGNATURE = "msg_signature";
String TIMESTAMP = "timestamp";
String NONCE = "nonce";
String INFO_TYPE = "InfoType";
String TICKET_UNDERLINE_KEY = "component_verify_ticket";
String TICKET_KEY = "ComponentVerifyTicket";
String UNAUTHORIZED = "unauthorized";
String NULL_CHAR = "";
String SUCCESS = "success";
/**
* 第三方平台 AppId
*/
// 获取 component_access_token 如下
String COMPONENT_APPID = "component_appid";
String COMPONENT_APPSECRET = "component_appsecret";
/**
* 第三方平台 access_token
*/
String COMPONENT_ACCESS_TOKEN = "component_access_token";
String FUNC_INFO = "func_info";
/**
* 有效期
*/
String EXPIRES_IN = "expires_in";
//获取预授权码 如下
String PRE_AUTH_CODE = "pre_auth_code";
//使用授权码获取授权信息 如下
String AUTHORIZATION_CODE = "authorization_code";
String AUTHORIZER_APPID = "authorizer_appid";
String AUTHORIZATION_INFO = "authorization_info";
//获取/刷新接口调用令牌 如下
String AUTHORIZER_APPID = "authorizer_appid";
String AUTHORIZER_REFRESH_TOKEN = "authorizer_refresh_token";
String AUTHORIZER_ACCESS_TOKEN = "authorizer_access_token";
String AUTH_TICKET = "====================授权票据【ComponentVerifyTicket】:【%s】====================";
String ERROR_TICKET = "微信开放平台,第三方平台获取【验证票据】失败";
String END_TICKET = "==============================结束授权事件接收URL=================================";
String START_RECEIVE = "==============================开始授权事件接收URL=================================";
String URL_TICKET = "授权事件接收URL,验证票据";
String SUCCESS_TICKET = "第三方平台授权事件接收URL,验证票据成功";
String ERROR_URL_TICKET = "第三方平台授权事件接收URL,验证票据异常";
String SUCCESS_ACCESS_TOKEN = "====================结束执行定时任务获取令牌【component_access_token】====================";
String FAILURE_ACCESS_TOKEN = "微信开放平台,第三方平台获取【令牌】失败";
//授权回调URL 如下
String AUTH_CODE = "auth_code";
}

49
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java

@ -0,0 +1,49 @@
package com.epmet.constant;
/**
* @Author zxc
* @CreateTime 2020/7/10 9:27
*/
public interface ThirdRunTimeInfoConstant {
String AUTH_TICKET = "====================授权票据【ComponentVerifyTicket】:【%s】====================";
String ERROR_TICKET = "微信开放平台,第三方平台获取【验证票据】失败";
String END_TICKET = "==============================结束授权事件接收URL=================================";
String START_RECEIVE = "==============================开始授权事件接收URL=================================";
String URL_TICKET = "授权事件接收URL,验证票据";
String SUCCESS_TICKET = "第三方平台授权事件接收URL,验证票据成功";
String ERROR_URL_TICKET = "第三方平台授权事件接收URL,验证票据异常";
String SUCCESS_ACCESS_TOKEN = "====================结束执行定时任务获取令牌【component_access_token】====================";
String FAILURE_ACCESS_TOKEN = "微信开放平台,第三方平台获取【令牌】失败";
String ENCRYPT = "Encrypt:%s";
String MSG = "msg:%s";
String START_GET_COMPONENT_ACCESS_TOKEN = "====================开始执行定时任务获取令牌【component_access_token】====================";
String START_DELETE_COMPONENT_ACCESS_TOKEN = "====================开始逻辑删除【component_access_token】====================";
String START_GET_PRE_AUTH_CODE = "====================开始执行定时任务获取预授权码【pre_auth_code】====================";
String POST_RESULT = "====================返回post结果:%s";
String FAILURE_GET_PRE_AUTH_CODE = "微信开放平台,第三方平台获取【预授权码】失败";
String END_GET_PRE_AUTH_CODE = "====================结束获取预授权码【pre_auth_code】====================";
String START_GET_AUTH_INFO = "=====================开始获取【authorization_info】=====================";
String START_INSERT_FUNC_INFO = "================================开始插入【func_info】====================================";
String END_GET_AUTH_INFO = "=====================结束获取【authorization_info】=====================";
}

1
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AuthRedirectController.java

@ -5,7 +5,6 @@ import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.Result;
import com.epmet.service.ComponentVerifyTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

66
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java

@ -9,6 +9,7 @@ import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.constant.ModuleConstant;
import com.epmet.constant.ThirdApiConstant;
import com.epmet.constant.ThirdRunTimeInfoConstant;
import com.epmet.dao.*;
import com.epmet.dto.form.*;
import com.epmet.dto.result.AuthorizationInfoResultDTO;
@ -18,11 +19,9 @@ import com.epmet.service.ComponentVerifyTicketService;
import com.epmet.util.WXBizMsgCrypt;
import com.epmet.util.WXXmlToMapUtil;
import com.epmet.util.XmlUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -35,6 +34,7 @@ import java.lang.reflect.Field;
import java.util.*;
import static com.epmet.constant.ThirdPlatformConstant.*;
import static com.epmet.constant.ThirdRunTimeInfoConstant.*;
/**
* @Author zxc
@ -71,24 +71,24 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
@Transactional(rollbackFor = Exception.class)
@Override
public String componentVerifyTicket(HttpServletRequest request, HttpServletResponse response) {
log.info(ModuleConstant.START_RECEIVE);
log.info(ThirdRunTimeInfoConstant.START_RECEIVE);
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding(ModuleConstant.UTF8);
response.setCharacterEncoding(ModuleConstant.UTF8);
// 微信加密签名
String msgSignature = request.getParameter("msg_signature");
String msgSignature = request.getParameter(ModuleConstant.MSG_SIGNATURE);
// 时间戳
String timeStamp = request.getParameter("timestamp");
String timeStamp = request.getParameter(ModuleConstant.TIMESTAMP);
// 随机数
String nonce = request.getParameter("nonce");
String nonce = request.getParameter(ModuleConstant.NONCE);
// 从请求中读取整个post数据
InputStream inputStream;
String postData = null;
inputStream = request.getInputStream();
postData= IOUtils.toString(inputStream, "UTF-8");
postData= IOUtils.toString(inputStream,ModuleConstant.UTF8);
//从XML中获取<Encrypt></Encrypt>标签内的密文文本
String encrypt = XmlUtil.toXml(postData);
log.info("Encrypt:"+encrypt);
log.info(String.format(ThirdRunTimeInfoConstant.ENCRYPT,encrypt));
//格式化密文文本,否则没有<ToUserName>标签,会解密失败,参考官方的加解密代码JAVA版本
String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
String fromXML = String.format(format, encrypt);
@ -102,14 +102,14 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
// 解密消息
msg = wxcpt.decryptMsg(msgSignature, timeStamp, nonce, fromXML);
}
log.info("msg:"+msg);
log.info(String.format(ThirdRunTimeInfoConstant.MSG,msg));
// 将xml转为map
Map<String, String> result = WXXmlToMapUtil.xmlToMap(msg);
String infotype = result.get("InfoType"); //获取infotype,注:微信开放平台文档中标明固定为:"component_verify_ticket",但参考其他代码,还包含authorized???
String infotype = result.get(ModuleConstant.INFO_TYPE); //获取infotype,注:微信开放平台文档中标明固定为:"component_verify_ticket",但参考其他代码,还包含authorized???
log.info(infotype);
switch (infotype){
case "component_verify_ticket": //验证票据
String ComponentVerifyTicket = result.get("ComponentVerifyTicket");
case ModuleConstant.TICKET_UNDERLINE_KEY: //验证票据
String ComponentVerifyTicket = result.get(ModuleConstant.TICKET_KEY);
// 缓存 ticket
redisThird.setComponentVerifyTicket(ComponentVerifyTicket);
// 存数据库
@ -117,17 +117,17 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
ticketFormDTO.setAppid(PLATFORM_APP_ID);
ticketFormDTO.setComponentVerifyTicket(ComponentVerifyTicket);
ticketDao.insertComponentVerifyTicket(ticketFormDTO);
log.info("component_verify_ticket:"+ComponentVerifyTicket);
log.info(ModuleConstant.TICKET_UNDERLINE_KEY+":"+ComponentVerifyTicket);
break;
case "unauthorized"://用户取消授权
return "";
case ModuleConstant.UNAUTHORIZED://用户取消授权
return ModuleConstant.NULL_CHAR;
}
} catch (Exception e) {
log.error(e.getMessage());
return "";
return ModuleConstant.NULL_CHAR;
}
log.info(ModuleConstant.END_TICKET);
return "success";
log.info(ThirdRunTimeInfoConstant.END_TICKET);
return ModuleConstant.SUCCESS;
}
/**
@ -138,7 +138,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
@Transactional(rollbackFor = Exception.class)
@Override
public void getComponentAccessToken() {
log.info("====================开始执行定时任务获取令牌【component_access_token】====================");
log.info(ThirdRunTimeInfoConstant.START_GET_COMPONENT_ACCESS_TOKEN);
Map<String, String> reMap;
try {
// 核心定时器
@ -157,17 +157,18 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
formDTO.setComponentAccessToken(componentAccessToken);
formDTO.setExpiresIn(Integer.valueOf(expiresIn));
//先逻辑删,在插入
log.info(ThirdRunTimeInfoConstant.START_DELETE_COMPONENT_ACCESS_TOKEN);
componentAccessTokenDao.updateOldComponentAccessToken();
componentAccessTokenDao.insertComponentAccessToken(formDTO);
//存缓存
redisThird.setComponentAccessToken(componentAccessToken);
} else {
throw new RenException(ModuleConstant.FAILURE_ACCESS_TOKEN);
throw new RenException(ThirdRunTimeInfoConstant.FAILURE_ACCESS_TOKEN);
}
} catch (Exception e) {
e.printStackTrace();
}
log.info(ModuleConstant.SUCCESS_ACCESS_TOKEN);
log.info(ThirdRunTimeInfoConstant.SUCCESS_ACCESS_TOKEN);
}
/**
@ -178,13 +179,13 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
@Transactional(rollbackFor = Exception.class)
@Override
public void preAuthCode() {
log.info("====================开始执行定时任务获取预授权码【pre_auth_code】====================");
log.info(START_GET_PRE_AUTH_CODE);
try {
String accessToken = redisThird.getComponentAccessToken(ModuleConstant.COMPONENT_ACCESS_TOKEN);
JSONObject jsonObject = new JSONObject();
jsonObject.put(ModuleConstant.COMPONENT_APPID, PLATFORM_APP_ID);
String post = HttpClientManager.getInstance().sendPostByJSON(ThirdApiConstant.API_CREATE_PREAUTHCODE_URL + accessToken, JSON.toJSONString(jsonObject)).getData();
log.info("====================返回post结果:" + post);
log.info(String.format(POST_RESULT,post));
HashMap<String, String> hashMap = JSON.parseObject(post, HashMap.class);
String preAuthCode = hashMap.get(ModuleConstant.PRE_AUTH_CODE);
String expiresIn = hashMap.get(ModuleConstant.EXPIRES_IN);
@ -200,12 +201,12 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
//预授权码 放入缓存
redisThird.setPreAuthCode(preAuthCode,ex);
} else {
throw new RuntimeException("微信开放平台,第三方平台获取【预授权码】失败");
throw new RuntimeException(FAILURE_GET_PRE_AUTH_CODE);
}
} catch (Exception e) {
e.printStackTrace();
}
log.info("====================结束执行定时任务获取预授权码【pre_auth_code】====================");
log.info(END_GET_PRE_AUTH_CODE);
}
/**
@ -214,6 +215,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
*/
@Transactional(rollbackFor = Exception.class)
public AuthorizationInfoResultDTO authInfoByAuthCode(String authCode,String authAppId,String customerId) {
log.info(START_GET_AUTH_INFO);
AuthorizationInfoResultDTO authorizationInfoResultDTO = new AuthorizationInfoResultDTO();
// 获取令牌【component_access_token】
String accessToken = redisThird.getComponentAccessToken(ModuleConstant.COMPONENT_ACCESS_TOKEN);
@ -247,6 +249,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
refreshAuthorizerAccessTokenDao.updateOldAuthorizerAccessToken(customerId);
refreshAuthorizerAccessTokenDao.insertAuthorizerAccessToken(refreshAccessToken);
// 2. 权限列表
log.info(START_INSERT_FUNC_INFO);
List<FuncInfoFormDTO> funcInfos = new ArrayList<>();
List<Map> func_info = authorizationInfoResultDTO.getFunc_info();
for (Map map1 : func_info) {
@ -268,6 +271,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
redisThird.setAuthInfo(authorizationInfoResultDTO);
//authorizer_refresh_token 放入缓存
redisThird.setAuthorizerRefreshToken(refreshAccessToken);
log.info(END_GET_AUTH_INFO);
return authorizationInfoResultDTO;
}
@ -323,13 +327,13 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe
String userId = tokenDto.getUserId();
String customerId = paCustomerAgencyDao.getCustomerIdByUserId(userId);
try {
request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding(ModuleConstant.UTF8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setCharacterEncoding("UTF-8");
String authCode = request.getParameter("auth_code");
String expiresIn = request.getParameter("expires_in");
response.setCharacterEncoding(ModuleConstant.UTF8);
String authCode = request.getParameter(ModuleConstant.AUTH_CODE);
String expiresIn = request.getParameter(ModuleConstant.EXPIRES_IN);
//authCode存数据库
AuthCodeFormDTO formDTO = new AuthCodeFormDTO();
formDTO.setAuthCode(authCode);

Loading…
Cancel
Save