|
@@ -4,13 +4,17 @@ package com.diagbot.filter;
|
|
|
|
|
|
import com.diagbot.biz.log.entity.SysLog;
|
|
import com.diagbot.biz.log.entity.SysLog;
|
|
import com.diagbot.dto.RespDTO;
|
|
import com.diagbot.dto.RespDTO;
|
|
|
|
+import com.diagbot.dto.ServiceFilterDTO;
|
|
|
|
+import com.diagbot.entity.HasPermissionDTO;
|
|
import com.diagbot.entity.ServiceFilter;
|
|
import com.diagbot.entity.ServiceFilter;
|
|
import com.diagbot.entity.ServiceToken;
|
|
import com.diagbot.entity.ServiceToken;
|
|
import com.diagbot.enums.SysTypeEnum;
|
|
import com.diagbot.enums.SysTypeEnum;
|
|
import com.diagbot.exception.CommonErrorCode;
|
|
import com.diagbot.exception.CommonErrorCode;
|
|
|
|
+import com.diagbot.facade.ServiceFilterFacade;
|
|
import com.diagbot.rabbit.MySender;
|
|
import com.diagbot.rabbit.MySender;
|
|
import com.diagbot.util.EnDecodeUtil;
|
|
import com.diagbot.util.EnDecodeUtil;
|
|
import com.diagbot.util.GsonUtil;
|
|
import com.diagbot.util.GsonUtil;
|
|
|
|
+import com.diagbot.util.StringUtil;
|
|
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;
|
|
@@ -19,8 +23,12 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.core.ParameterizedTypeReference;
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
@@ -32,12 +40,9 @@ import java.nio.charset.StandardCharsets;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
-import java.util.LinkedHashMap;
|
|
|
|
import java.util.LinkedHashSet;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
-//import com.diagbot.entity.HasPermissionDTO;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @Description: 全局过滤器
|
|
* @Description: 全局过滤器
|
|
* @author: gaodm
|
|
* @author: gaodm
|
|
@@ -52,8 +57,8 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
private static Boolean IS_GENERATE = false;
|
|
private static Boolean IS_GENERATE = false;
|
|
private static Map<String, Long> SERVICE_FILTER = new HashMap<>();
|
|
private static Map<String, Long> SERVICE_FILTER = new HashMap<>();
|
|
|
|
|
|
- @Value("${lantone.product}")
|
|
|
|
- private String lantonePrduct;
|
|
|
|
|
|
+ @Value("${lantone.gateway-valid}")
|
|
|
|
+ private Boolean gatewayValid;
|
|
|
|
|
|
// @Autowired
|
|
// @Autowired
|
|
// DiagbotmanServiceClient diagbotmanServiceClient;
|
|
// DiagbotmanServiceClient diagbotmanServiceClient;
|
|
@@ -61,98 +66,108 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
private MySender mySender;
|
|
private MySender mySender;
|
|
@Autowired
|
|
@Autowired
|
|
private RestTemplate restTemplate;
|
|
private RestTemplate restTemplate;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ServiceFilterFacade serviceFilterFacade;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
|
|
public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
|
|
log.info("check token and url permission....");
|
|
log.info("check token and url permission....");
|
|
- LinkedHashSet requiredAttribute
|
|
|
|
- = serverWebExchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
|
|
|
|
- ServerHttpRequest request = serverWebExchange.getRequest();
|
|
|
|
- String requestUri = request.getPath().pathWithinApplication().value();
|
|
|
|
- String serviceName = "";
|
|
|
|
- if (requiredAttribute != null) {
|
|
|
|
- Iterator<URI> iterator = requiredAttribute.iterator();
|
|
|
|
- while (iterator.hasNext()) {
|
|
|
|
- URI next = iterator.next();
|
|
|
|
- if (next.getPath().startsWith(GATE_WAY_PREFIX)) {
|
|
|
|
- requestUri = next.getPath().substring(GATE_WAY_PREFIX.length());
|
|
|
|
- serviceName = requestUri.substring(requestUri.indexOf('/') + 1,
|
|
|
|
- requestUri.indexOf('/', 2));
|
|
|
|
|
|
+
|
|
|
|
+ //网关验证产品线接口权限
|
|
|
|
+ if (gatewayValid) {
|
|
|
|
+ LinkedHashSet requiredAttribute
|
|
|
|
+ = serverWebExchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
|
|
|
|
+ ServerHttpRequest request = serverWebExchange.getRequest();
|
|
|
|
+ String requestUri = request.getPath().pathWithinApplication().value();
|
|
|
|
+ String serviceName = "";
|
|
|
|
+ if (requiredAttribute != null) {
|
|
|
|
+ Iterator<URI> iterator = requiredAttribute.iterator();
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ URI next = iterator.next();
|
|
|
|
+ if (next.getPath().startsWith(GATE_WAY_PREFIX)) {
|
|
|
|
+ requestUri = next.getPath().substring(GATE_WAY_PREFIX.length());
|
|
|
|
+ serviceName = requestUri.substring(requestUri.indexOf('/') + 1,
|
|
|
|
+ requestUri.indexOf('/', 2));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
- // log.info("APIURL:{}", requestUri);
|
|
|
|
- // log.info("SERVICENAME:{}", serviceName);
|
|
|
|
-
|
|
|
|
- // if(!IS_GENERATE) {
|
|
|
|
- // RespDTO<List<ServiceFilter>> filter = diagbotmanServiceClient.getAll();
|
|
|
|
- // if (filter != null){
|
|
|
|
- // List<ServiceFilter> list = filter.data;
|
|
|
|
- // if (ListUtil.isNotEmpty(list)){
|
|
|
|
- // for(ServiceFilter bean : list) {
|
|
|
|
- // SERVICE_FILTER.put(bean.getName(), bean.getProductId());
|
|
|
|
- // }
|
|
|
|
- // IS_GENERATE = true;
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
-
|
|
|
|
- Map<String, Long> serviceFilters = this.dealServiceFilter();
|
|
|
|
- if (serviceFilters.get(serviceName) != null) {
|
|
|
|
- String appkey = request.getHeaders().getFirst("appKeyId");
|
|
|
|
- String secret = request.getHeaders().getFirst("appKeySecret");
|
|
|
|
- String securityCode = request.getHeaders().getFirst("securityCode"); //防伪码
|
|
|
|
|
|
+
|
|
|
|
+ ServiceFilterDTO serviceFilterDTO = new ServiceFilterDTO();
|
|
try {
|
|
try {
|
|
- String encodeTime = EnDecodeUtil.decode(securityCode);
|
|
|
|
- Long time = Long.parseLong(encodeTime);
|
|
|
|
- Long curTime = new Date().getTime();
|
|
|
|
- // 防伪码主要是为了防止别人窃取url地址后直接长期使用,这边设了失效,目前暂定为24小时
|
|
|
|
- // 前端实时获取当前时间跟系统时间作比较,如果时差超出24小时将会抛异常
|
|
|
|
- if ((curTime - time) > 1000 * 3600 * 24) {
|
|
|
|
|
|
+ serviceFilterDTO = serviceFilterFacade.getServiceFilter();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ RespDTO respDTO = new RespDTO();
|
|
|
|
+ respDTO.code = CommonErrorCode.RPC_ERROR.getCode();
|
|
|
|
+ respDTO.msg = CommonErrorCode.RPC_ERROR.getMsg();
|
|
|
|
+ return getVoidMono(serverWebExchange, respDTO);
|
|
|
|
+ }
|
|
|
|
+ if (serviceFilterDTO.getApiMap().contains(serviceName)) {
|
|
|
|
+ String productIdStr = request.getHeaders().getFirst("productId");
|
|
|
|
+ if (StringUtil.isBlank(productIdStr)) {
|
|
|
|
+ RespDTO respDTO = new RespDTO();
|
|
|
|
+ respDTO.code = CommonErrorCode.PARAM_IS_NULL.getCode();
|
|
|
|
+ respDTO.msg = "请传入产品ID";
|
|
|
|
+ return getVoidMono(serverWebExchange, respDTO);
|
|
|
|
+ }
|
|
|
|
+ Long productId = Long.valueOf(productIdStr);
|
|
|
|
+ if (!serviceName.equals(serviceFilterDTO.getProductMap().get(productId))) {
|
|
|
|
+ RespDTO respDTO = new RespDTO();
|
|
|
|
+ respDTO.code = CommonErrorCode.NOPERMISSION_ERROR.getCode();
|
|
|
|
+ respDTO.msg = CommonErrorCode.NOPERMISSION_ERROR.getMsg();
|
|
|
|
+ return getVoidMono(serverWebExchange, respDTO);
|
|
|
|
+ }
|
|
|
|
+ String appkey = request.getHeaders().getFirst("appKeyId");
|
|
|
|
+ String secret = request.getHeaders().getFirst("appKeySecret");
|
|
|
|
+ String securityCode = request.getHeaders().getFirst("securityCode"); //防伪码
|
|
|
|
+ try {
|
|
|
|
+ String encodeTime = EnDecodeUtil.decode(securityCode);
|
|
|
|
+ Long time = Long.parseLong(encodeTime);
|
|
|
|
+ Long curTime = new Date().getTime();
|
|
|
|
+ // 防伪码主要是为了防止别人窃取url地址后直接长期使用,这边设了失效,目前暂定为24小时
|
|
|
|
+ // 前端实时获取当前时间跟系统时间作比较,如果时差超出24小时将会抛异常
|
|
|
|
+ if ((curTime - time) > 1000 * 3600 * 24) {
|
|
|
|
+ RespDTO respDTO = new RespDTO();
|
|
|
|
+ respDTO.code = CommonErrorCode.SECURITYCODE_ERROR.getCode();
|
|
|
|
+ respDTO.msg = CommonErrorCode.SECURITYCODE_ERROR.getMsg();
|
|
|
|
+ return getVoidMono(serverWebExchange, respDTO);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
RespDTO respDTO = new RespDTO();
|
|
RespDTO respDTO = new RespDTO();
|
|
respDTO.code = CommonErrorCode.SECURITYCODE_ERROR.getCode();
|
|
respDTO.code = CommonErrorCode.SECURITYCODE_ERROR.getCode();
|
|
respDTO.msg = CommonErrorCode.SECURITYCODE_ERROR.getMsg();
|
|
respDTO.msg = CommonErrorCode.SECURITYCODE_ERROR.getMsg();
|
|
return getVoidMono(serverWebExchange, respDTO);
|
|
return getVoidMono(serverWebExchange, respDTO);
|
|
}
|
|
}
|
|
- } catch (Exception e) {
|
|
|
|
- RespDTO respDTO = new RespDTO();
|
|
|
|
- respDTO.code = CommonErrorCode.SECURITYCODE_ERROR.getCode();
|
|
|
|
- respDTO.msg = CommonErrorCode.SECURITYCODE_ERROR.getMsg();
|
|
|
|
- return getVoidMono(serverWebExchange, respDTO);
|
|
|
|
- }
|
|
|
|
- Long productId = serviceFilters.get(serviceName);
|
|
|
|
- //获取appkey,secret对应的权限信息
|
|
|
|
- ServiceToken st = new ServiceToken();
|
|
|
|
- st.setAppkey(appkey);
|
|
|
|
- st.setSecret(secret);
|
|
|
|
- st.setProductId(productId);
|
|
|
|
- // RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
|
|
|
|
- RespDTO<LinkedHashMap> res = hasPermission(st);
|
|
|
|
- if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
|
|
|
|
- return getVoidMono(serverWebExchange, res);
|
|
|
|
- }
|
|
|
|
|
|
+ //获取appkey,secret对应的权限信息
|
|
|
|
+ ServiceToken st = new ServiceToken();
|
|
|
|
+ st.setAppkey(appkey);
|
|
|
|
+ st.setSecret(secret);
|
|
|
|
+ st.setProductId(productId);
|
|
|
|
+ //权限验证
|
|
|
|
+ RespDTO<HasPermissionDTO> res = hasPermission(st);
|
|
|
|
+ if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
|
|
|
|
+ return getVoidMono(serverWebExchange, res);
|
|
|
|
+ }
|
|
|
|
|
|
- //记录日志
|
|
|
|
- SysLog sysLog = new SysLog();
|
|
|
|
- sysLog.setGmtCreate(new Date());
|
|
|
|
- sysLog.setIp(request.getRemoteAddress().toString());
|
|
|
|
- sysLog.setMethod(requestUri);
|
|
|
|
- ServiceFilter serviceFilter = new ServiceFilter();
|
|
|
|
- serviceFilter.setServiceName(serviceName);
|
|
|
|
- serviceFilter.setProductId(productId);
|
|
|
|
- sysLog.setOperation(GsonUtil.toJson(serviceFilter));
|
|
|
|
- sysLog.setParams(requestUri);
|
|
|
|
- sysLog.setSysType(SysTypeEnum.APPKEY.getKey());
|
|
|
|
- LinkedHashMap<String, String> map = res.data;
|
|
|
|
- sysLog.setUsername(map.get("userId"));
|
|
|
|
- mySender.outputLogSend(sysLog);
|
|
|
|
|
|
+ //记录日志
|
|
|
|
+ SysLog sysLog = new SysLog();
|
|
|
|
+ sysLog.setGmtCreate(new Date());
|
|
|
|
+ sysLog.setIp(request.getRemoteAddress().toString());
|
|
|
|
+ sysLog.setMethod(requestUri);
|
|
|
|
+ ServiceFilter serviceFilter = new ServiceFilter();
|
|
|
|
+ serviceFilter.setServiceName(serviceName);
|
|
|
|
+ serviceFilter.setProductId(productId);
|
|
|
|
+ sysLog.setOperation(GsonUtil.toJson(serviceFilter));
|
|
|
|
+ sysLog.setParams(requestUri);
|
|
|
|
+ sysLog.setSysType(SysTypeEnum.APPKEY.getKey());
|
|
|
|
+ sysLog.setUsername(res.data.getUserId());
|
|
|
|
+ mySender.outputLogSend(sysLog);
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
|
|
ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
|
|
- // builder.header("Authorization", "Authorization Bearer token");
|
|
|
|
- // gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
|
|
return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -175,21 +190,24 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
*
|
|
*
|
|
* @return 过滤的列表
|
|
* @return 过滤的列表
|
|
*/
|
|
*/
|
|
- private Map<String, Long> dealServiceFilter() {
|
|
|
|
- Map<String, Long> serviceFilters = new HashMap<>();
|
|
|
|
- String[] set = lantonePrduct.split(";");
|
|
|
|
- for (String s : set) {
|
|
|
|
- String[] d = s.split(",");
|
|
|
|
- serviceFilters.put(d[0], Long.valueOf(d[1]));
|
|
|
|
- }
|
|
|
|
- return serviceFilters;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public RespDTO<LinkedHashMap> hasPermission(ServiceToken st) {
|
|
|
|
|
|
+ // private Map<String, Long> dealServiceFilter() {
|
|
|
|
+ // Map<String, Long> serviceFilters = new HashMap<>();
|
|
|
|
+ // String[] set = lantonePrduct.split(";");
|
|
|
|
+ // for (String s : set) {
|
|
|
|
+ // String[] d = s.split(",");
|
|
|
|
+ // serviceFilters.put(d[0], Long.valueOf(d[1]));
|
|
|
|
+ // }
|
|
|
|
+ // return serviceFilters;
|
|
|
|
+ // }
|
|
|
|
+ public RespDTO<HasPermissionDTO> hasPermission(ServiceToken st) {
|
|
try {
|
|
try {
|
|
- RespDTO<LinkedHashMap> res
|
|
|
|
- = restTemplate.postForObject("http://diagbotman-service/serviceToken/hasPermission", st, RespDTO.class);
|
|
|
|
- return res;
|
|
|
|
|
|
+ ResponseEntity<RespDTO<HasPermissionDTO>> respDTOResponseEntity
|
|
|
|
+ = restTemplate.exchange("http://diagbotman-service/serviceToken/hasPermission",
|
|
|
|
+ HttpMethod.POST,
|
|
|
|
+ new HttpEntity<ServiceToken>(st),
|
|
|
|
+ new ParameterizedTypeReference<RespDTO<HasPermissionDTO>>() {
|
|
|
|
+ });
|
|
|
|
+ return respDTOResponseEntity.getBody();
|
|
} catch (Exception ex) {
|
|
} catch (Exception ex) {
|
|
return hasPermissionFallback(st);
|
|
return hasPermissionFallback(st);
|
|
}
|
|
}
|