Commit 08d37a0a authored by matianhao's avatar matianhao

Initial commit

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mth</groupId>
<artifactId>jiwei-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jiwei-api</name>
<description>纪委智慧监督平台api调用</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.mth.requestsecret;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RequestSecretApplication {
public static void main(String[] args) {
SpringApplication.run(RequestSecretApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
package com.mth.requestsecret.controller;
import com.mth.requestsecret.scheduler.RequestSecretSchedulerTask;
import com.mth.requestsecret.util.DSLUtils;
import com.mth.requestsecret.util.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author MaTianHao
* @date 2020/7/13
*/
@Slf4j
public class BaseController {
@Autowired
private RestTemplate restTemplate;
@Value("${DATA_API_ADDRESS}")
private String pathUrl;
@Value("${app.key}")
private String appKey;
/**
* 发送请求公共方法
*
* @param paramMap api参数
* @param apiMethod api签名
* @return
*/
protected ResponseEntity<String> commonSendRequest(Map<String, Object> paramMap, final String apiMethod) {
// 请求url拼接api签名和公共参数
StringBuilder url = new StringBuilder()
.append(pathUrl)
.append("/interface/public/service/risen-api/")
.append(apiMethod)
.append("?appKey={appKey}&sign={sign}&requestTime={requestTime}");
// 拼接api参数
for (String key : paramMap.keySet()) {
url.append("&").append(key).append("={").append(key).append("}");
}
// 请求时间
String requestTime = DSLUtils.dateToLong(new Date()) + "";
// 签名字符串
String signStr = appKey + RequestSecretSchedulerTask.requestSecret + requestTime;
// 组装请求参数
Map<String, Object> map = new HashMap<String, Object>(12) {{
put("appKey", appKey);
put("sign", MD5Utils.encoderByMd5(signStr));
put("requestTime", requestTime);
}};
map.putAll(paramMap);
// 日志记录
log.info("纪委api:{}", apiMethod);
log.info("api url:{}", url);
log.info("api params:{}", map);
// 发送请求
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url.toString(), String.class, map);
log.info("api response:{}", responseEntity);
return responseEntity;
}
}
package com.mth.requestsecret.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author MaTianHao
* @date 2020/7/13
*/
@RestController
public class RequestController extends BaseController {
/**
* 五.浙江省电力业务开放数据查询
*
* @param strJson 业务数据
* @param dsgeSign 签名数据
* @param channelCode 渠道编码
* @return
*/
@GetMapping("zjsdlywkfsjcx")
public ResponseEntity<String> zjsdlywkfsjcx(String strJson, String dsgeSign, String channelCode) {
String apiMethod = "zjsdlywkfsjcx.action";
Map<String, Object> paramMap = new HashMap<String, Object>(3) {{
put("strJson", strJson);
put("dsgeSign", dsgeSign);
put("channelCode", channelCode);
}};
return commonSendRequest(paramMap, apiMethod);
}
/**
* 六.浙江省教育部高校学籍姓名及证件号码查询
*
* @param userName 姓名
* @param idCard 证件号码
* @return
*/
@GetMapping("/zjsjybgxxjxmjzjhmcx")
public ResponseEntity<String> zjsjybgxxjxmjzjhmcx(@RequestParam String userName, @RequestParam String idCard) {
// api签名
String apiMethod = "zjsjybgxxjxmjzjhmcx.action";
// api参数
Map<String, Object> paramMap = new HashMap<String, Object>(2) {{
put("xm", userName);
put("zjhm", idCard);
}};
return commonSendRequest(paramMap, apiMethod);
}
/**
* 七.浙江省出入境证件查询
*
* @param idCard 证件号码
* @return
*/
@GetMapping("/zjscrjzjcx")
public ResponseEntity<String> zjscrjzjcx(@RequestParam String idCard) {
// api签名
String apiMethod = "zjscrjzjcx.action";
// api参数
Map<String, Object> paramMap = new HashMap<String, Object>(2) {{
put("cardId", idCard);
}};
return commonSendRequest(paramMap, apiMethod);
}
/**
* 八.浙江省公安厅居民身份证(新)查询
*
* @param userName 姓名
* @param idCard 证件号码
* @return
*/
@GetMapping("/zjsgatjmsfzxcx")
public ResponseEntity<String> zjsgatjmsfzxcx(@RequestParam String userName, @RequestParam String idCard) {
// api签名
String apiMethod = "zjsgatjmsfzxcx.action";
// api参数
Map<String, Object> paramMap = new HashMap<String, Object>(2) {{
put("czrkxm", userName);
put("czrkgmsfhm", idCard);
}};
return commonSendRequest(paramMap, apiMethod);
}
/**
* 九.浙江省公安厅居民身份证查询(电子证照))
*
* @param userName 姓名
* @param idCard 证件号码
* @return
*/
@GetMapping("/zjsgatjmsfzcxdzzz")
public ResponseEntity<String> zjsgatjmsfzcxdzzz(@RequestParam String userName, @RequestParam String idCard) {
// api签名
String apiMethod = "zjsgatjmsfzcxdzzz.action";
// api参数
Map<String, Object> paramMap = new HashMap<String, Object>(2) {{
put("User_Name", userName);
put("User_ID", idCard);
}};
return commonSendRequest(paramMap, apiMethod);
}
}
package com.mth.requestsecret.scheduler;
import com.alibaba.fastjson.JSONObject;
import com.mth.requestsecret.util.DSLUtils;
import com.mth.requestsecret.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author MaTianHao
* @date 2020/7/10
*/
@Component
@Configuration
@EnableScheduling
public class RequestSecretSchedulerTask implements SchedulingConfigurer {
@Autowired
private RestTemplate restTemplate;
@Value("${DATA_API_ADDRESS}")
private String pathUrl;
@Value("${app.key}")
private String appKey;
@Value("${app.secret}")
private String appSecret;
/**
* 请求秘钥
*/
public static String requestSecret = null;
/**
* 请求秘钥过期时间
*/
private static Long requestSecretEndTime = null;
/**
* 刷新秘钥
*/
public static String refreshSecret = null;
/**
* 刷新秘钥过期时间
*/
private static Long refreshSecretEndTime = null;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(
() -> System.out.println("定时任务获取requestSecret:" + LocalDateTime.now().toString()),
triggerContext -> {
getRequestSecret();
// 提前一分钟刷新
Instant instant = Instant.ofEpochMilli(requestSecretEndTime).minus(1, ChronoUnit.MINUTES);
return Date.from(instant);
}
);
}
/**
* 获取请求秘钥
*/
private void getRequestSecret() {
String url = pathUrl + "/interface/public/service/risen-inte/reTokenByKey.action" +
"?appKey={appKey}&sign={sign}&requestTime={requestTime}";
// 请求时间
String requestTime = DSLUtils.dateToLong(new Date()) + "";
// 签名字符串
String signStr = appKey + appSecret + requestTime;
// 组装请求参数
Map<String, Object> map = new HashMap<String, Object>(3) {{
put("appKey", appKey);
put("sign", MD5Utils.encoderByMd5(signStr));
put("requestTime", requestTime);
}};
// 获取请求秘钥和刷新秘钥
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, map);
// 解析返回结果
JSONObject datas = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("datas");
if (!Objects.isNull(datas)) {
requestSecret = datas.getString("requestSecret");
requestSecretEndTime = Long.parseLong(datas.getString("requestSecretEndTime"));
refreshSecret = datas.getString("refreshSecret");
refreshSecretEndTime = Long.parseLong(datas.getString("refreshSecretEndTime"));
}
}
}
package com.mth.requestsecret.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @ClassName: DSLUtils
* @Description:
* @author: yuhl
* @date: Jul 10, 2017 6:00:45 PM
*/
public class DSLUtils {
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
public static String longToString(long currentTime, String formatType)
throws Exception {
Date date = longToDate(currentTime, formatType);
String strTime = dateToString(date, formatType);
return strTime;
}
public static Date stringToDate(String strTime, String formatType)
throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
public static Date longToDate(long currentTime, String formatType)
throws Exception {
Date dateOld = new Date(currentTime);
String sDateTime = dateToString(dateOld, formatType);
Date date = stringToDate(sDateTime, formatType);
return date;
}
public static long stringToLong(String strTime, String formatType)
throws Exception {
Date date = stringToDate(strTime, formatType);
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date);
return currentTime;
}
}
public static long dateToLong(Date date) {
return date.getTime();
}
}
package com.mth.requestsecret.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String encoderByMd5(String plainText) {
String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return re_md5;
}
}
server:
port: 8080
app:
key: s5tnb9mtt56utxtabmfqarsfdjeegvzw
secret: bieyrg7nkqlkrm124xefeyhx7fh77sy9
DATA_API_ADDRESS: http://127.0.0.1:8101
\ No newline at end of file
package com.mth.requestsecret;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RequestSecretApplicationTests {
@Test
void contextLoads() {
}
}
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