|
@@ -17,7 +17,7 @@ import com.diagbot.util.EnDecodeUtil;
|
|
|
import com.diagbot.util.GsonUtil;
|
|
|
import com.diagbot.util.RSAEncrypt;
|
|
|
import com.diagbot.util.StringUtil;
|
|
|
-import com.diagbot.vo.HospitalSetVO;
|
|
|
+import com.diagbot.vo.HospitalListVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -35,6 +35,7 @@ import reactor.core.publisher.Mono;
|
|
|
|
|
|
import java.net.URI;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
@@ -53,10 +54,13 @@ import java.util.Map;
|
|
|
public class GlobalGatewayFilter implements GlobalFilter {
|
|
|
|
|
|
private static final String GATE_WAY_PREFIX = "/api";
|
|
|
- private static final String LICENCE = "licence_day";
|
|
|
+ private static final String LICENCE_EXPIRE = "licence_expire";
|
|
|
+ private static final String LICENCE_REMAIN = "licence_remain";
|
|
|
+ private static final String LICENCE_START = "licence_start";
|
|
|
private static Boolean IS_GENERATE = false;
|
|
|
private static Map<String, Long> SERVICE_FILTER = new HashMap<>();
|
|
|
private static Map<String, String> licenceMap = new HashMap<>(); // licence对象
|
|
|
+ private static String START_LIC = "1"; //是否启用licence
|
|
|
private static boolean initLicence = false; // 是否初始化licence
|
|
|
|
|
|
|
|
@@ -157,8 +161,11 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
|
}
|
|
|
|
|
|
// 进行licence校验
|
|
|
- String hospitalCode = request.getHeaders().getFirst("hospitalCode");
|
|
|
- verifyLicence(hospitalCode, serviceName);
|
|
|
+ initLicenceMap();
|
|
|
+ if (START_LIC.equals(licenceMap.get(LICENCE_START))) {
|
|
|
+ String hospitalCode = request.getHeaders().getFirst("hospitalCode");
|
|
|
+ verifyLicence(hospitalCode, serviceName);
|
|
|
+ }
|
|
|
|
|
|
ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
|
|
|
builder.header("Authorization", "Authorization Bearer token");
|
|
@@ -203,31 +210,39 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
|
* @param serviceName 服务
|
|
|
*/
|
|
|
public void verifyLicence(String hospitalCode, String serviceName) {
|
|
|
- // 初始化licence
|
|
|
- if (!initLicence) {
|
|
|
- HospitalSetVO hospitalSetVO = new HospitalSetVO();
|
|
|
- hospitalSetVO.setCode(LICENCE);
|
|
|
- RespDTO<List<SysSetInfoDTO>> respDTO = tranServiceClient.getSysSetInfoDatas(hospitalSetVO);
|
|
|
- if (respDTO != null) {
|
|
|
- for (SysSetInfoDTO setInfoDTO : respDTO.data) {
|
|
|
- String serName = SysTypeEnum.getName(setInfoDTO.getSysType());
|
|
|
- licenceMap.put(setInfoDTO.getHospitalCode() + serName.substring(0, serName.indexOf("-")), setInfoDTO.getValue());
|
|
|
- }
|
|
|
+ // 校验截止日期
|
|
|
+ verifyLicenceExpire(hospitalCode, serviceName);
|
|
|
+ // 校验剩余天数
|
|
|
+ verifyLicenceRemain(hospitalCode, serviceName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void refreshLicenceMap() {
|
|
|
+ tranServiceClient.refreshLicence();
|
|
|
+ for (String key : licenceMap.keySet()) {
|
|
|
+ if (key.indexOf(LICENCE_REMAIN) > -1) {
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- // 校验
|
|
|
- String value = licenceMap.get(hospitalCode + serviceName);
|
|
|
- if (StringUtil.isNotEmpty(value)) {
|
|
|
+ /**
|
|
|
+ * 校验licence到期
|
|
|
+ *
|
|
|
+ * @param hospitalCode 医院编码
|
|
|
+ * @param serviceName 服务名称
|
|
|
+ */
|
|
|
+ public void verifyLicenceExpire(String hospitalCode, String serviceName) {
|
|
|
+ String licenceExpire = licenceMap.get(hospitalCode + LICENCE_EXPIRE + serviceName);
|
|
|
+ if (StringUtil.isNotEmpty(licenceExpire)) {
|
|
|
String licence = "";
|
|
|
try {
|
|
|
- licence = RSAEncrypt.decrypt(value);
|
|
|
+ licence = RSAEncrypt.decrypt(licenceExpire);
|
|
|
} catch (Exception e) { // licence 被篡改
|
|
|
throw new CommonException(CommonErrorCode.LICENCE_ERROR);
|
|
|
}
|
|
|
String nowStr = DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss");
|
|
|
- System.out.println(nowStr);
|
|
|
- System.out.println(licence);
|
|
|
if (licence.compareTo(nowStr) < 0) { // licence 已到期
|
|
|
throw new CommonException(CommonErrorCode.LICENCE_EXPIRE);
|
|
|
}
|
|
@@ -235,7 +250,52 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void refreshLicence() {
|
|
|
+ /**
|
|
|
+ * 校验licence剩余天数
|
|
|
+ *
|
|
|
+ * @param hospitalCode 医院编码
|
|
|
+ * @param serviceName 服务名称
|
|
|
+ */
|
|
|
+ public void verifyLicenceRemain(String hospitalCode, String serviceName) {
|
|
|
+ String licenceRemain = licenceMap.get(hospitalCode + LICENCE_REMAIN + serviceName);
|
|
|
+ if (StringUtil.isNotEmpty(licenceRemain)) {
|
|
|
+ int day = 0;
|
|
|
+ try {
|
|
|
+ day = Integer.parseInt(RSAEncrypt.decrypt(licenceRemain));
|
|
|
+ } catch (Exception e) { // licence 被篡改
|
|
|
+ throw new CommonException(CommonErrorCode.LICENCE_ERROR);
|
|
|
+ }
|
|
|
+ if (day < 0) { // licence 已到期
|
|
|
+ throw new CommonException(CommonErrorCode.LICENCE_EXPIRE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始化licenceMap数据
|
|
|
+ */
|
|
|
+ public void initLicenceMap() {
|
|
|
+ // 初始化licence
|
|
|
+ licenceMap.clear();
|
|
|
+ if (!initLicence) {
|
|
|
+ HospitalListVO hospitalListVO = new HospitalListVO();
|
|
|
+ List<String> codeList = new ArrayList<>();
|
|
|
+ codeList.add(LICENCE_EXPIRE);
|
|
|
+ codeList.add(LICENCE_REMAIN);
|
|
|
+ codeList.add(LICENCE_START);
|
|
|
+ hospitalListVO.setCodeList(codeList);
|
|
|
+ RespDTO<List<SysSetInfoDTO>> respDTO = tranServiceClient.getSysSetInfoDataByList(hospitalListVO);
|
|
|
+ if (respDTO != null) {
|
|
|
+ for (SysSetInfoDTO setInfoDTO : respDTO.data) {
|
|
|
+ String serName = SysTypeEnum.getName(setInfoDTO.getSysType());
|
|
|
+ if (LICENCE_START.equals(setInfoDTO.getCode())) {
|
|
|
+ licenceMap.put(setInfoDTO.getCode(), setInfoDTO.getValue());
|
|
|
+ } else {
|
|
|
+ licenceMap.put(setInfoDTO.getHospitalCode() + setInfoDTO.getCode() + serName.substring(0, serName.indexOf("-")), setInfoDTO.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|