商家可以进入控制台https://console.open.taobao.com/#/index,进入自己所创建的应用并在概览标签下查看自己创建应用的App Key(应用唯一标识,平台通过appkey鉴别应用身份)和App Secret(平台分配的密钥,用来保证应用来源的可靠性,防止被伪造,需要妥善保存)
概念:
session Key是店铺授权给应用的有效凭证。
两种授权方式:
进入SDK下载的标签页,下载对应语言版本的SDK并进行开发(下载前请先检查是否已经申请了相对应的权限包)
注:点击下载后,需要手动执行刷新界面操作才能知晓当前SDK是否已经下
<!-- 应用SDK的jar包 --> <dependency> <groupId>com.taobao.api</groupId> <artifactId>taobao-sdk-java</artifactId> <version>20221208</version> <scope>system</scope> <type>jar</type> <systemPath>${project.basedir}/src/main/resources/lib/taobao-sdk-java-auto_1663138347043-20221208.jar</systemPath> </dependency>
使用SDK包中的checkSign()方法来进行验签,不建议使用自己构造的签名来进行验签。
参照接入接口文档中的成功返回示例进行返回接口响应的结果
参照接入接口文档中的成功返回示例进行返回接口响应的结果
以下为验签核心代码示例:
@RequestMapping(value = "memberRegister", method = RequestMethod.POST)
public String memberRegister(HttpServletRequest httpServletRequest) {
//可以进行提取公共参数
String appKey = httpServletRequest.getParameter("app_key");
String method = httpServletRequest.getParameter("method");
String sign = httpServletRequest.getParameter("sign");
String timestamp = httpServletRequest.getParameter("timestamp");
//获取unix_timestamp信息
String unixTimestamp = httpServletRequest.getParameter("unix_timestamp");
String returnResult = "";
CheckResult checkResult = new CheckResult();
try {
//使用SpiUtils.checkSign()来进行验签
checkResult = SpiUtils.checkSign(httpServletRequest, QimenConstant.APP_SECRET);
System.out.println("验签的结果为:" + checkResult.isSuccess());
if (checkResult.isSuccess()) {
//获取body中的数据
HotelBodyEntity bodyEntity = JsonObjTool.jsonToPojo(checkResult.getRequestBody(), HotelBodyEntity.class);
//获得body中的请求参数信息
String fliggy_user_name = bodyEntity.getFliggy_user_name();
String phone = bodyEntity.getPhone();
String apply_id = bodyEntity.getApply_id();
System.out.println(bodyEntity);
//写具体的业务逻辑,这里例举一个demo
if (!StringUtils.isEmpty(method)) {
HotelResponseEntity entity = new HotelResponseEntity();
entity.setResult_code("SUCCESS");
entity.setResult_message("ok");
HotelResponseEntity.HotelResponseMap map = new HotelResponseEntity.HotelResponseMap();
map.setCard_number("0X1234");
map.setCustomer_id("abcd1234");
map.setEffective_date(1546272000L);
map.setExpire_date(1577808000L);
map.setCard_level("V1");
map.setExtension("{\"xxx\":1}");
map.setPoint(10L);
map.setIs_new(1L);
map.setCustomer_status(1L);
map.setPartner_member_id("0X1234");
map.setCard_status("有效");
entity.setData(map);
return JsonObjTool.objectToJson(entity);
} else {
//模拟逻辑失败返回
return "{\"error_response\":{\"msg\":\"Remote service error\",\"code\":50,\"sub_msg\":\"非法参数\",\"sub_code\":\"isv.invalid-parameter\"}}";
}
} else {
System.out.println("sign验签不通过");
//返回奇门验签失败的错误结果;
return "{\"result_message\":\"Illegal request\",\"result_code\":\"sign-check-failure\"}";
}
} catch (IOException e) {
e.printStackTrace();
}
return returnResult;
}
完整demo示例:?HotelDemo.zip(该示例demo已全部在奇门上通过测试)
若需要获取非公共请求参数,可以从checkResult.getRequestBody()值里面进行获取
//获取body中的数据 HotelBodyEntity bodyEntity = JsonObjTool.jsonToPojo(checkResult.getRequestBody(), HotelBodyEntity.class);
写完代码后发布到公网测试服务器上,对地址进行可用性检测,点击开始服务检测即可
进行地址可用性检测,通过后,可进行下一步,并在正式环境上进行地址检测,通过后服务发布上线。
服务上线后,与群内对接的飞猪技术同学进行回复相对应的接口接入已完成。