Authored by wangshusheng

Merge branch 'dev_广点通第二版加密体系'

... ... @@ -7,8 +7,10 @@ import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.union.request.*;
import com.yoho.service.model.union.response.*;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.interceptor.RemoteIPInterceptor;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
... ... @@ -49,6 +51,10 @@ public interface IUnionService {
return clickUnionRequestBO;
}
default public Pair<Integer, String> visitCallbackUrl(String url) {
return HttpUtils.httpGet(url);
}
/**
* 联盟点击
... ...
package com.yoho.unions.server.service.impl;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.yoho.core.common.utils.MD5;
import com.yoho.service.model.union.request.ActivateUnionRequestBO;
import com.yoho.service.model.union.request.ClickUnionRequestBO;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.dal.IUnionConfigDAO;
import com.yoho.unions.dal.model.UnionConfig;
import com.yoho.unions.interceptor.RemoteIPInterceptor;
import com.yoho.unions.server.service.IUnionService;
/**
* 广点通回调:新加密方法
*
* @author yoho
*/
@Service("UnionServiceImpl_7")
public class GDT3ServiceImpl extends UnionServiceImpl implements IUnionService {
static Logger log = LoggerFactory.getLogger(GDT3ServiceImpl.class);
@Resource
IUnionConfigDAO unionConfigDAO;
@Override
public String getCallbackUrl(ClickUnionRequestBO clickBO, ActivateUnionRequestBO activateUnionRequestBO) {
// 直接调用原有的广点通获取URL的方法
ActivateUnionRequestBO requestBO = new ActivateUnionRequestBO();
requestBO.setAppid(clickBO.getAppid());
requestBO.setClient_type(clickBO.getClient_type());
requestBO.setIdfa(clickBO.getIdfa());
requestBO.setImei(clickBO.getImei());
requestBO.setCommonUse(clickBO.getCommonUse());
// 广点通后台生成的点击id
requestBO.setUdid(clickBO.getClickId());
String clientIp = RemoteIPInterceptor.getRemoteIP();
if (StringUtils.isNotEmpty(clientIp)) {
String[] ips = clientIp.split(",");
if (ips.length > 0) {
requestBO.setClientIp(ips[0]);
}
}
return processUrl(requestBO);
}
@Override
public ClickUnionRequestBO clickHttpRequestTOBO(HttpServletRequest request,ClickUnionRequestBO bo){
bo = new ClickUnionRequestBO();
String muid = request.getParameter("muid");
String app_type = request.getParameter("app_type");
bo.setClient_type(app_type);
if(app_type.equals(ClientTypeEnum.ANDROID.getName())){
bo.setImei(muid);
bo.setInterfaceType("addMonitor_android");
}else {
bo.setIdfa(muid);
bo.setInterfaceType("addMonitor_ios");
}
bo.setAppid(request.getParameter("appid"));
bo.setClickId(request.getParameter("click_id"));
bo.setUnion_type(request.getParameter("union_type"));
bo.setClickTime(Integer.valueOf(request.getParameter("click_time")));
bo.setTd(request.getParameter("td"));
//广告主在广点通的账户id
bo.setCommonUse(String.valueOf(request.getParameter("advertiser_id")));
return bo;
}
@SuppressWarnings("unused")
public String processUrl(ActivateUnionRequestBO requestBO) {
log.info("enter GDTServiceImpl.processUrl param{} is ", requestBO);
// 有货在广点通的账号id
String uid = getUid(requestBO, "3938107");
String clientType = getClientType(requestBO, ClientTypeEnum.IOS.getName());
// UnionConfig unionConfig = new UnionConfig();
// unionConfig.setEncryptKey("BAAAAAAAAAAAPBc7");
// unionConfig.setSignKey("d683620ba71ff13a");
UnionConfig unionConfig = unionConfigDAO.selectByUidAndClientType(Integer.valueOf(uid),clientType);
String encryptKey = getEncryptKey(unionConfig, null);
String signKey = getSignKey(unionConfig, null);
String url = getUrl(unionConfig, "https://t.gdt.qq.com/conv/app/");
String muid = getMuid(requestBO);
log.info("activateUnion muid is {} , clientType is {}, signKey is {}, url is {}", muid, clientType, signKey, url);
int conv_time = DateUtil.getCurrentTimeSecond();
String client_ip = requestBO.getClientIp();
String clickId = requestBO.getUdid();
String appId = requestBO.getAppid();
StringBuilder sixParamBuilder = new StringBuilder();
// app_type=ANDROID
sixParamBuilder.append("app_type=").append(clientTypeConver(clientType));
// &click_id=007210548a030059ccdfd1d4
sixParamBuilder.append("&click_id=").append(nullToEmpty(clickId));
// &client_ip=10.11.12.13
sixParamBuilder.append("&client_ip=").append(nullToEmpty(client_ip));
// &conv_time=1422263664
sixParamBuilder.append("&conv_time=").append(conv_time);
// &muid=0f074dc8e1f0547310e729032ac0730b
sixParamBuilder.append("&muid=").append(nullToEmpty(muid));
// &sign_key=08ebe39d34c421b8
sixParamBuilder.append("&sign_key=").append(signKey);
log.info("sixParamBuilder is {} ", sixParamBuilder.toString());
String encstr = MD5.md5(sixParamBuilder.toString());
String encver = "1.0";
String convType = "MOBILEAPP_ACTIVITE";
// https://t.gdt.qq.com/conv/app/{appid}/conv
StringBuilder urlBuilder = new StringBuilder(url);
// + {appid}/conv
urlBuilder.append(appId).append("/conv");
log.info("urlBuilder is {} ", urlBuilder.toString());
// post body
StringBuilder postBodyBuilder = new StringBuilder();
// click_id=
postBodyBuilder.append("click_id=").append(clickId);
// &appid=112233
postBodyBuilder.append("&appid=").append(appId);
// &muid=0f074dc8e1f0547310e729032ac0730b
postBodyBuilder.append("&muid=").append(muid);
// &conv_time=1422263664
postBodyBuilder.append("&conv_time=").append(conv_time);
// &client_ip=
postBodyBuilder.append("&client_ip=").append(client_ip);
// &encstr=8d3fb6477b01827dc2f635a5b45a7fdd
postBodyBuilder.append("&encstr=").append(encstr);
// &encver=1.0
postBodyBuilder.append("&encver=").append(encver);
// &advertiser_id=20345
postBodyBuilder.append("&advertiser_id=").append(uid);
// &app_type=IOS
postBodyBuilder.append("&app_type=").append(clientTypeConver(clientType));
// &conv_type=MOBILEAPP_ACTIVITE
postBodyBuilder.append("&conv_type=").append(convType);
log.info("postBodyBuilder is {} ", postBodyBuilder.toString());
String finalUrl = urlBuilder.append("?").append(postBodyBuilder.toString()).toString();
log.info("finalUrl is {} ", finalUrl);
return finalUrl;
}
public static void main(String[] args) {
// test code
ActivateUnionRequestBO bo = new ActivateUnionRequestBO();
bo.setClient_type("ios");
bo.setIdfa("29562890-1743-4005-BB7B-E942FD5E59F2");
bo.setAppid("490655927");
bo.setUdid("dxf7ewqkaaadlvncpnqa");
bo.setAppkey("yohobuy");
bo.setCommonUse("4376278");
GDT3ServiceImpl impl = new GDT3ServiceImpl();
String url = impl.processUrl(bo);
System.out.println(impl.visitCallbackUrl(url));
}
@Override
public Pair<Integer, String> visitCallbackUrl(String url) {
log.info("visitCallbackUrl url is : {}", url);
int index = url.indexOf('?');
if (index < 0) {
return Pair.of(500, "url错误:没有问号和参数");
}
String param = url.substring(index + 1);
String[] paramPairs = param.split("&");
Map<String, Object> map = new HashMap<>(paramPairs.length);
for (String pair : paramPairs) {
String[] p = pair.split("=");
if (p.length == 2) {
map.put(p[0], p[1]);
}
}
try {
return HttpUtils.httpPost(url.substring(0, index), map);
} catch (Exception e) {
log.error("回调第三方错误,url为:" + url, e);
return Pair.of(500, "调用第三方回调url异常");
}
}
private String getUid(ActivateUnionRequestBO requestBO, String defaultValue) {
if(StringUtils.isNotEmpty(requestBO.getCommonUse())){
return requestBO.getCommonUse();
} else {
return defaultValue;
}
}
private String getClientType(ActivateUnionRequestBO requestBO, String defaultValue) {
if(StringUtils.isNotEmpty(requestBO.getClient_type())){
return requestBO.getClient_type().toLowerCase();
} else {
return defaultValue;
}
}
private String getEncryptKey(UnionConfig unionConfig, String defaultValue) {
if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getEncryptKey())){
return unionConfig.getEncryptKey();
} else {
return defaultValue;
}
}
private String getSignKey(UnionConfig unionConfig, String defaultValue) {
if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getSignKey())){
return unionConfig.getSignKey();
} else {
return defaultValue;
}
}
private String getUrl(UnionConfig unionConfig, String defaultValue) {
if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getUrl())){
return unionConfig.getUrl();
} else {
return defaultValue;
}
}
private String getMuid(ActivateUnionRequestBO requestBO) {
String muid;
if (ClientTypeEnum.IOS.getName().equalsIgnoreCase(requestBO.getClient_type())) {
// IDFA 码(需转大写),进行 md5 以后得到的 32位全小写 md5 表现字符串。
muid = requestBO.getIdfa().toUpperCase();
} else {
// IMEI 号(需转小写),进行 md5 以后得到的 32位全小写 md5 表现字符串。
muid = requestBO.getImei().toLowerCase();
}
// 对设备id进行MD5加密,获取32位小写加密串
return MD5.md5(muid);
}
private String clientTypeConver(String clientType) {
if (StringUtils.isEmpty(clientType)) {
return "";
}
if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType)) {
return clientType.toUpperCase();
} else {
return "IOS";
}
}
private String nullToEmpty(String str) {
if (str == null) {
return "";
} else {
return str;
}
}
}
... ...
... ... @@ -366,6 +366,18 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
}
}
}
if (StringUtils.isEmpty(value) && "100000000003503".equals(request.getChannelId())) {
ClickUnionRequestBO clickInfo = new ClickUnionRequestBO();
clickInfo.setClient_type(request.getClient_type());
clickInfo.setIdfa(request.getIdfa());
clickInfo.setImei(request.getImei());
clickInfo.setAppid(request.getAppid());
clickInfo.setCommonUse(request.getCommonUse());
clickInfo.setClickId(request.getUdid());
clickInfo.setUnion_type(request.getChannelId());
value = JSON.toJSONString(clickInfo);
}
if (StringUtils.isEmpty(value)) {
if(StringUtils.isEmpty(request.getClientIp())){
... ... @@ -591,8 +603,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
union_type = String.valueOf(unionTypeMatch.getUnionTypeMatch());
}
String bean =buffer.append("UnionServiceImpl").append("_").append(union_type).toString();
IUnionService uniteService = null;
if(SpringContextUtil.containsBean(bean)){
IUnionService uniteService = SpringContextUtil.getBean(bean, IUnionService.class);
uniteService = SpringContextUtil.getBean(bean, IUnionService.class);
url = uniteService.getCallbackUrl(click,request);
}else{
url = unionService.getCallbackUrl(click,request);
... ... @@ -612,7 +625,12 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
}
activeUnion.info("activateUnion in call url={}", url);
//改成httpclient方式调用
Pair<Integer, String> pair = HttpUtils.httpGet(url);
Pair<Integer, String> pair;
if (uniteService != null) {
pair = uniteService.visitCallbackUrl(url);
} else {
pair = visitCallbackUrl(url);
}
try{
UnionCallbackLogs unionCallbackLogs = new UnionCallbackLogs();
... ...