Quellcode durchsuchen

产品拦截列表放到数据库,并且加载到缓存

gaodm vor 5 Jahren
Ursprung
Commit
e8114f85ba

+ 22 - 22
gateway-service/src/main/java/com/diagbot/entity/HasPermissionDTO.java

@@ -1,22 +1,22 @@
-//package com.diagbot.entity;
-//
-//import lombok.Getter;
-//import lombok.Setter;
-//
-//import java.io.Serializable;
-//
-///**
-// * @Description: 是否有权限返回类
-// * @Author: ztg
-// * @Date: 2018/9/20 10:32
-// */
-//
-//@Getter
-//@Setter
-//public class HasPermissionDTO implements Serializable {
-//
-//    private static final long serialVersionUID = 1L;
-//
-//    private Boolean hasPermission ;//标志
-//    private String userId;         //用户ID
-//}
+package com.diagbot.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+
+/**
+ * @Description: 是否有权限返回类
+ * @Author: ztg
+ * @Date: 2018/9/20 10:32
+ */
+
+@Getter
+@Setter
+public class HasPermissionDTO implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private Boolean hasPermission ;//标志
+    private String userId;         //用户ID
+}

+ 10 - 20
gateway-service/src/main/java/com/diagbot/facade/ServiceFilterFacade.java

@@ -3,17 +3,15 @@ package com.diagbot.facade;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.dto.ServiceFilterDTO;
 import com.diagbot.dto.ServiceFiltersDTO;
-import com.diagbot.util.ListUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -40,22 +38,14 @@ public class ServiceFilterFacade {
     @Cacheable(value = SERVICEFILTER, key = "'ServiceFilter'")
     public ServiceFilterDTO getServiceFilter() {
         ServiceFilterDTO serviceFilterDTO = new ServiceFilterDTO();
-        RespDTO<List<LinkedHashMap>> resFilter
-                = restTemplate.postForObject("http://diagbotman-service/service_filter/getAll", null, RespDTO.class);
+        ResponseEntity<RespDTO<List<ServiceFiltersDTO>>> respDTOResponseEntity
+                = restTemplate.exchange("http://diagbotman-service/service_filter/getAll", HttpMethod.POST,
+                null, new ParameterizedTypeReference<RespDTO<List<ServiceFiltersDTO>>>() {
+                });
+        RespDTO<List<ServiceFiltersDTO>> resFilter = respDTOResponseEntity.getBody();
         if (resFilter != null && RespDTO.TRUE_CODE.equals(resFilter.code)) {
-            Set<String> apiMap = new HashSet<>();
-            Map<Long, String> productMap = new HashMap<>();
-            if (ListUtil.isNotEmpty(resFilter.data)) {
-                List<ServiceFiltersDTO> serviceFiltersDTOList = new ArrayList<>();
-                for (LinkedHashMap map: resFilter.data){
-                    ServiceFiltersDTO serviceFiltersDTO = new ServiceFiltersDTO();
-                    serviceFiltersDTO.setName(map.get("name").toString());
-                    serviceFiltersDTO.setProductId(Long.valueOf(map.get("productId").toString()));
-                    serviceFiltersDTOList.add(serviceFiltersDTO);
-                }
-                apiMap = serviceFiltersDTOList.stream().distinct().map(ServiceFiltersDTO::getName).collect(Collectors.toSet());
-                productMap = serviceFiltersDTOList.stream().collect(Collectors.toMap(ServiceFiltersDTO::getProductId, ServiceFiltersDTO::getName));
-            }
+            Set<String> apiMap = resFilter.data.stream().distinct().map(ServiceFiltersDTO::getName).collect(Collectors.toSet());
+            Map<Long, String> productMap = resFilter.data.stream().collect(Collectors.toMap(ServiceFiltersDTO::getProductId, ServiceFiltersDTO::getName));
             serviceFilterDTO.setApiMap(apiMap);
             serviceFilterDTO.setProductMap(productMap);
             log.info("读取了拦截产品线缓存!");

+ 15 - 10
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -5,6 +5,7 @@ package com.diagbot.filter;
 import com.diagbot.biz.log.entity.SysLog;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.dto.ServiceFilterDTO;
+import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceFilter;
 import com.diagbot.entity.ServiceToken;
 import com.diagbot.enums.SysTypeEnum;
@@ -22,8 +23,12 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
 import org.springframework.cloud.gateway.filter.GlobalFilter;
 import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.ParameterizedTypeReference;
 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.ResponseEntity;
 import org.springframework.http.server.reactive.ServerHttpRequest;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.server.ServerWebExchange;
@@ -35,12 +40,9 @@ import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 
-//import com.diagbot.entity.HasPermissionDTO;
-
 /**
  * @Description: 全局过滤器
  * @author: gaodm
@@ -143,7 +145,7 @@ public class GlobalGatewayFilter implements GlobalFilter {
                 st.setSecret(secret);
                 st.setProductId(productId);
                 //权限验证
-                RespDTO<LinkedHashMap> res = hasPermission(st);
+                RespDTO<HasPermissionDTO> res = hasPermission(st);
                 if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
                     return getVoidMono(serverWebExchange, res);
                 }
@@ -159,8 +161,7 @@ public class GlobalGatewayFilter implements GlobalFilter {
                 sysLog.setOperation(GsonUtil.toJson(serviceFilter));
                 sysLog.setParams(requestUri);
                 sysLog.setSysType(SysTypeEnum.APPKEY.getKey());
-                LinkedHashMap<String, String> map = res.data;
-                sysLog.setUsername(map.get("userId"));
+                sysLog.setUsername(res.data.getUserId());
                 mySender.outputLogSend(sysLog);
 
             }
@@ -198,11 +199,15 @@ public class GlobalGatewayFilter implements GlobalFilter {
     //        }
     //        return serviceFilters;
     //    }
-    public RespDTO<LinkedHashMap> hasPermission(ServiceToken st) {
+    public RespDTO<HasPermissionDTO> hasPermission(ServiceToken st) {
         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) {
             return hasPermissionFallback(st);
         }