Commit e5722c66 authored by matianhao's avatar matianhao

[纪委接口] <update> 请求秘钥存入redis;接口返回签名错误时重新获取秘钥

parent 4dea01bd
...@@ -45,6 +45,13 @@ ...@@ -45,6 +45,13 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.12</version> <version>1.18.12</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
......
package com.mth.requestsecret.constant;
/**
* @author MaTianHao
* @date 2020/8/13
*/
public class Constants {
// redis存储请求秘钥key值
//-----------------------------------
/**
* 数据局接口
*/
public static final String SJJ_REQUEST_SECRET_PREFIX = "sjj.request.secret";
/**
* 招必得接口
*/
public static final String ZBD_REQUEST_SECRET_PREFIX = "zbd.request.secret";
// 数据局接口返回响应码
//-----------------------------------
/**
* 成功:00
*/
public static final String RESPONSE_CODE_00 = "00";
/**
* sign参数错误:14
*/
public static final String RESPONSE_CODE_14 = "14";
}
...@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2020/7/13 * @date 2020/7/13
*/ */
@RestController @RestController
public class RequestController extends BaseController { public class RequestController {
@Autowired @Autowired
RestTemlateService restTemlateService; RestTemlateService restTemlateService;
......
package com.mth.requestsecret.scheduler; package com.mth.requestsecret.scheduler;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mth.requestsecret.constant.Constants;
import com.mth.requestsecret.util.DSLUtils; import com.mth.requestsecret.util.DSLUtils;
import com.mth.requestsecret.util.MD5Utils; import com.mth.requestsecret.util.MD5Utils;
import com.mth.requestsecret.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -33,6 +35,9 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer { ...@@ -33,6 +35,9 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired
RedisUtils redisUtil;
@Value("${DATA_API_ADDRESS}") @Value("${DATA_API_ADDRESS}")
private String pathUrl; private String pathUrl;
...@@ -75,7 +80,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer { ...@@ -75,7 +80,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
/** /**
* 获取请求秘钥 * 获取请求秘钥
*/ */
private void getRequestSecret() { public void getRequestSecret() {
String url = pathUrl + "/interface/public/service/risen-inte/reTokenByKey.action" + String url = pathUrl + "/interface/public/service/risen-inte/reTokenByKey.action" +
"?appKey={appKey}&sign={sign}&requestTime={requestTime}"; "?appKey={appKey}&sign={sign}&requestTime={requestTime}";
...@@ -100,6 +105,8 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer { ...@@ -100,6 +105,8 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
requestSecretEndTime = Long.parseLong(datas.getString("requestSecretEndTime")); requestSecretEndTime = Long.parseLong(datas.getString("requestSecretEndTime"));
refreshSecret = datas.getString("refreshSecret"); refreshSecret = datas.getString("refreshSecret");
refreshSecretEndTime = Long.parseLong(datas.getString("refreshSecretEndTime")); refreshSecretEndTime = Long.parseLong(datas.getString("refreshSecretEndTime"));
// 保存请求秘钥至redis
redisUtil.set(Constants.SJJ_REQUEST_SECRET_PREFIX, requestSecret);
} }
} }
......
package com.mth.requestsecret.service; package com.mth.requestsecret.service;
import com.alibaba.fastjson.JSONObject;
import com.mth.requestsecret.scheduler.RequestSecretSchedulerTask; import com.mth.requestsecret.scheduler.RequestSecretSchedulerTask;
import com.mth.requestsecret.util.DSLUtils; import com.mth.requestsecret.util.DSLUtils;
import com.mth.requestsecret.util.MD5Utils; import com.mth.requestsecret.util.MD5Utils;
import com.mth.requestsecret.util.RedisUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -13,6 +15,9 @@ import org.springframework.web.client.RestTemplate; ...@@ -13,6 +15,9 @@ import org.springframework.web.client.RestTemplate;
import java.util.Date; import java.util.Date;
import static com.mth.requestsecret.constant.Constants.RESPONSE_CODE_14;
import static com.mth.requestsecret.constant.Constants.SJJ_REQUEST_SECRET_PREFIX;
/** /**
* @author fyl * @author fyl
* @version v1.0 * @version v1.0
...@@ -27,6 +32,12 @@ public class RestTemlateService { ...@@ -27,6 +32,12 @@ public class RestTemlateService {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired
private RedisUtils redisUtil;
@Autowired
private RequestSecretSchedulerTask schedulerTask;
@Value("${DATA_API_ADDRESS}") @Value("${DATA_API_ADDRESS}")
private String pathUrl; private String pathUrl;
...@@ -42,12 +53,15 @@ public class RestTemlateService { ...@@ -42,12 +53,15 @@ public class RestTemlateService {
// 请求时间 // 请求时间
String requestTime = DSLUtils.dateToLong(new Date()) + ""; String requestTime = DSLUtils.dateToLong(new Date()) + "";
// redis中获取秘钥
String requestSecret = redisUtil.get(SJJ_REQUEST_SECRET_PREFIX);
log.info("redis中请求秘钥:{}", requestSecret);
// 签名字符串 // 签名字符串
String signStr = appKey + RequestSecretSchedulerTask.requestSecret + requestTime; String signStr = appKey + requestSecret + requestTime;
// 组装请求参数 // 组装请求参数
paramMap.add("appKey", appKey); paramMap.set("appKey", appKey);
paramMap.add("sign", MD5Utils.encoderByMd5(signStr)); paramMap.set("sign", MD5Utils.encoderByMd5(signStr));
paramMap.add("requestTime", requestTime); paramMap.set("requestTime", requestTime);
// 日志记录 // 日志记录
log.info("纪委api:{}", apiMethod); log.info("纪委api:{}", apiMethod);
...@@ -60,6 +74,15 @@ public class RestTemlateService { ...@@ -60,6 +74,15 @@ public class RestTemlateService {
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(paramMap, headers); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(paramMap, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, request, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, request, String.class);
log.info("api response:{}", responseEntity); log.info("api response:{}", responseEntity);
// 接口返回码
String code = JSONObject.parseObject(responseEntity.getBody()).getString("code");
// 如果返回签名错误,则重新获取秘钥,重新发送请求
if (RESPONSE_CODE_14.equals(code)) {
schedulerTask.getRequestSecret();
return commonSendRequest(paramMap, apiMethod);
}
return responseEntity; return responseEntity;
} }
} }
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment