Browse Source

网关记录日志

zhoutg 6 years ago
parent
commit
a3520aa546

+ 2 - 1
common/src/main/java/com/diagbot/enums/SysTypeEnum.java

@@ -18,7 +18,8 @@ public enum SysTypeEnum implements KeyedNamed {
     BI_SERVICE(5, "bi-service"),
     KNOWLEDGE_SERVICE(6, "knowledge-service"),
     FEEDBACK_SERVICE(7, "feedback-service"),
-    ICSS_WEB(8, "icss-web");
+    ICSS_WEB(8, "icss-web"),
+    APPKEY(9, "appkey");
 
     @Setter
     private int key;

+ 22 - 0
diagbotman-service/src/main/java/com/diagbot/dto/HasPermissionDTO.java

@@ -0,0 +1,22 @@
+package com.diagbot.dto;
+
+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
+}

+ 6 - 2
diagbotman-service/src/main/java/com/diagbot/facade/ServiceTokenFacade.java

@@ -2,6 +2,7 @@ package com.diagbot.facade;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.diagbot.dto.HasPermissionDTO;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.LantoneProduct;
 import com.diagbot.entity.OpenedProducts;
@@ -52,7 +53,8 @@ public class ServiceTokenFacade extends ServiceTokenServiceImpl {
      * @param serviceTokenVo 参数
      * @return 是否有权限
      */
-    public RespDTO<Boolean> hasPermission(ServiceTokenVo serviceTokenVo) {
+    public RespDTO<HasPermissionDTO> hasPermission(ServiceTokenVo serviceTokenVo) {
+        HasPermissionDTO hasPermissionDTO = new HasPermissionDTO();
         String appkey = serviceTokenVo.getAppkey();
         String secret = serviceTokenVo.getSecret();
         Map<String, Object> paramMap = new HashMap<>();
@@ -95,7 +97,9 @@ public class ServiceTokenFacade extends ServiceTokenServiceImpl {
                 throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "访问权限已过期");
             }
         }
-        return RespDTO.onSuc(true);
+        hasPermissionDTO.setHasPermission(true);
+        hasPermissionDTO.setUserId(UserUtils.getCurrentPrincipleID());
+        return RespDTO.onSuc(hasPermissionDTO);
     }
 
     /**

+ 2 - 1
diagbotman-service/src/main/java/com/diagbot/web/ServiceTokenController.java

@@ -2,6 +2,7 @@ package com.diagbot.web;
 
 
 import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.HasPermissionDTO;
 import com.diagbot.dto.ProductServiceDTO;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.wrapper.ServiceTokenWrapper;
@@ -48,7 +49,7 @@ public class ServiceTokenController {
     @PostMapping("/hasPermission")
     @ApiOperation(value = "查询当前appkey和secret是否有权限[by:zhoutg]")
     @ApiIgnore
-    public RespDTO<Boolean> hasPermission(@RequestBody ServiceTokenVo serviceTokenVo) {
+    public RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceTokenVo serviceTokenVo) {
         return serviceTokenFacade.hasPermission(serviceTokenVo);
     }
 

+ 4 - 0
gateway-service/pom.xml

@@ -79,6 +79,10 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
+        </dependency>
         <dependency>
             <groupId>com.diagbot</groupId>
             <artifactId>common</artifactId>

+ 2 - 1
gateway-service/src/main/java/com/diagbot/client/DiagbotmanServiceClient.java

@@ -2,6 +2,7 @@ package com.diagbot.client;
 
 import com.diagbot.client.hystrix.DiagbotmanServiceHystrix;
 import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceFilter;
 import com.diagbot.entity.ServiceToken;
 import org.springframework.cloud.openfeign.FeignClient;
@@ -23,7 +24,7 @@ public interface DiagbotmanServiceClient {
     RespDTO<List<ServiceFilter>> getAll();
 
     @PostMapping("/serviceToken/hasPermission")
-    RespDTO<Boolean> hasPermission(@RequestBody ServiceToken st);
+    RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st);
 
 }
 

+ 2 - 1
gateway-service/src/main/java/com/diagbot/client/hystrix/DiagbotmanServiceHystrix.java

@@ -3,6 +3,7 @@ package com.diagbot.client.hystrix;
 
 import com.diagbot.client.DiagbotmanServiceClient;
 import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceToken;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,7 +25,7 @@ public class DiagbotmanServiceHystrix implements DiagbotmanServiceClient {
     }
 
     @Override
-    public RespDTO<Boolean> hasPermission(@RequestBody ServiceToken st) {
+    public RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st) {
         log.error("【hystrix】调用{}异常", "hasPermisson");
         return null;
     }

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

@@ -0,0 +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
+}

+ 192 - 0
gateway-service/src/main/java/com/diagbot/entity/SysLog.java

@@ -0,0 +1,192 @@
+package com.diagbot.entity;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * <p>
+ * 系统操作日志表
+ * </p>
+ *
+ * @author gaodm
+ * @since 2018-09-14
+ */
+public class SysLog implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 日志ID
+     */
+    private Long id;
+
+    /**
+     * 是否删除,N:未删除,Y:删除
+     */
+    private String isDeleted;
+
+    /**
+     * 记录创建时间
+     */
+    private Date gmtCreate;
+
+    /**
+     * 记录修改时间,如果时间是1970年则表示纪录未修改
+     */
+    private Date gmtModified;
+
+    /**
+     * 创建人,0表示无创建人值
+     */
+    private String creator;
+
+    /**
+     * 修改人,如果为0则表示纪录未修改
+     */
+    private String modifier;
+
+    /**
+     * 访问者的IP
+     */
+    private String ip;
+
+    /**
+     * 访问的系统类型 1:user-service,2:diagbotman-service,3:uaa-service,4:log-service,5:bi-service,6:knowledge-service,7:feedback-service,8:icss-web
+     */
+    private Integer sysType;
+
+    /**
+     * 方法
+     */
+    private String method;
+
+    /**
+     * 操作名
+     */
+    private String operation;
+
+    /**
+     * 参数
+     */
+    private String params;
+
+    /**
+     * 用户名
+     */
+    private String username;
+
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getIsDeleted() {
+        return isDeleted;
+    }
+
+    public void setIsDeleted(String isDeleted) {
+        this.isDeleted = isDeleted;
+    }
+
+    public Date getGmtCreate() {
+        return gmtCreate;
+    }
+
+    public void setGmtCreate(Date gmtCreate) {
+        this.gmtCreate = gmtCreate;
+    }
+
+    public Date getGmtModified() {
+        return gmtModified;
+    }
+
+    public void setGmtModified(Date gmtModified) {
+        this.gmtModified = gmtModified;
+    }
+
+    public String getCreator() {
+        return creator;
+    }
+
+    public void setCreator(String creator) {
+        this.creator = creator;
+    }
+
+    public String getModifier() {
+        return modifier;
+    }
+
+    public void setModifier(String modifier) {
+        this.modifier = modifier;
+    }
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public Integer getSysType() {
+        return sysType;
+    }
+
+    public void setSysType(Integer sysType) {
+        this.sysType = sysType;
+    }
+
+    public String getMethod() {
+        return method;
+    }
+
+    public void setMethod(String method) {
+        this.method = method;
+    }
+
+    public String getOperation() {
+        return operation;
+    }
+
+    public void setOperation(String operation) {
+        this.operation = operation;
+    }
+
+    public String getParams() {
+        return params;
+    }
+
+    public void setParams(String params) {
+        this.params = params;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    @Override
+    public String toString() {
+        return "SysLog{" +
+                "id=" + id +
+                ", isDeleted=" + isDeleted +
+                ", gmtCreate=" + gmtCreate +
+                ", gmtModified=" + gmtModified +
+                ", creator=" + creator +
+                ", modifier=" + modifier +
+                ", ip=" + ip +
+                ", sysType=" + sysType +
+                ", method=" + method +
+                ", operation=" + operation +
+                ", params=" + params +
+                ", username=" + username +
+                "}";
+    }
+}

+ 20 - 1
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -2,7 +2,11 @@ package com.diagbot.filter;
 
 import com.diagbot.client.DiagbotmanServiceClient;
 import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceToken;
+import com.diagbot.entity.SysLog;
+import com.diagbot.enums.SysTypeEnum;
+import com.diagbot.rabbit.MySender;
 import com.diagbot.util.GsonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +25,7 @@ import reactor.core.publisher.Mono;
 
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -45,6 +50,8 @@ public class GlobalGatewayFilter implements GlobalFilter {
 
     @Autowired
     DiagbotmanServiceClient diagbotmanServiceClient;
+    @Autowired
+    private MySender mySender;
 
 
     @Override
@@ -80,6 +87,17 @@ public class GlobalGatewayFilter implements GlobalFilter {
         //            }
         //        }
 
+
+        SysLog sysLog = new SysLog();
+        sysLog.setGmtCreate(new Date());
+        sysLog.setIp("3333");
+        sysLog.setMethod(request.getURI().toString());
+        sysLog.setOperation(request.getURI().toString());
+        sysLog.setParams(request.getURI().toString());
+        sysLog.setSysType(SysTypeEnum.APPKEY.getKey());
+        sysLog.setUsername("userId");
+        mySender.outputLogSend(sysLog);
+
         Map<String, Long> serviceFilters = this.dealServiceFilter();
         if (serviceFilters.get(serviceName) != null) {
             String appkey = request.getHeaders().getFirst("appkey");
@@ -90,10 +108,11 @@ public class GlobalGatewayFilter implements GlobalFilter {
             st.setAppkey(appkey);
             st.setSecret(secret);
             st.setProductId(productId);
-            RespDTO<Boolean> res = diagbotmanServiceClient.hasPermission(st);
+            RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
             if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
                 return getVoidMono(serverWebExchange, res);
             }
+
         }
 
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();

+ 23 - 0
gateway-service/src/main/java/com/diagbot/rabbit/MyProcessor.java

@@ -0,0 +1,23 @@
+package com.diagbot.rabbit;
+
+import org.springframework.cloud.stream.annotation.Input;
+import org.springframework.cloud.stream.annotation.Output;
+import org.springframework.messaging.MessageChannel;
+import org.springframework.messaging.SubscribableChannel;
+
+/**
+ * @Description: 自定义Stream发布和消费对象
+ * @author: gaodm
+ * @time: 2018/8/29 13:39
+ */
+public interface MyProcessor {
+
+    String INPUT_LOG = "inputLog";
+    String OUTPUT_LOG = "outputLog";
+
+    @Input(INPUT_LOG)
+    SubscribableChannel inputLog();
+
+    @Output(OUTPUT_LOG)
+    MessageChannel outputLog();
+}

+ 27 - 0
gateway-service/src/main/java/com/diagbot/rabbit/MySender.java

@@ -0,0 +1,27 @@
+package com.diagbot.rabbit;
+
+import com.diagbot.entity.SysLog;
+import com.diagbot.util.GsonUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.cloud.stream.annotation.EnableBinding;
+import org.springframework.integration.support.MessageBuilder;
+import org.springframework.messaging.MessageChannel;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 发布者
+ * @author: gaodm
+ * @time: 2018/8/29 13:41
+ */
+@Component
+@EnableBinding({ MyProcessor.class })
+public class MySender {
+    @Autowired
+    @Qualifier("outputLog")
+    MessageChannel outputLog;
+
+    public void outputLogSend(SysLog sysLog) {
+        outputLog.send(MessageBuilder.withPayload(GsonUtil.toJson(sysLog)).build());
+    }
+}