Browse Source

网关服务

zhoutg 6 years ago
parent
commit
3b28240898

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

@@ -6,6 +6,7 @@ import com.diagbot.entity.ServiceToken;
 import com.diagbot.exception.CommonErrorCode;
 import com.diagbot.exception.CommonException;
 import com.diagbot.service.impl.ServiceTokenServiceImpl;
+import com.diagbot.vo.ServiceTokenVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -28,7 +29,9 @@ public class ServiceTokenFacade extends ServiceTokenServiceImpl {
      * @Author: ztg
      * @Date: 2018/9/18 16:54
      */
-    public RespDTO<Boolean> hasPermission(String appkey, String secret, Long productId) {
+    public RespDTO<Boolean> hasPermission(ServiceTokenVo serviceTokenVo) {
+        String appkey = serviceTokenVo.getAppkey();
+        String secret = serviceTokenVo.getSecret();
         ServiceToken st = this.getServiceToken(appkey, secret);
         if(null == st) {
             throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "appkey或secret错误,appkey=【" + appkey + "】," + "secret=【" + secret + "】");
@@ -37,7 +40,7 @@ public class ServiceTokenFacade extends ServiceTokenServiceImpl {
         if(ps == null) {
             throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "无权限访问!");
         }
-        if(!ps.getProductId().equals(productId)) {
+        if(!ps.getProductId().equals(serviceTokenVo.getProductId())) {
             throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "无权限访问!");
         }
         Date date = new Date();

+ 22 - 0
diagbotman-service/src/main/java/com/diagbot/vo/ServiceTokenVo.java

@@ -0,0 +1,22 @@
+package com.diagbot.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+
+/**
+ * @Description: appkey 和 secret接收类
+ * @Author: ztg
+ * @Date: 2018/9/19 13:14
+ */
+@Getter
+@Setter
+public class ServiceTokenVo implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+   private String appkey;
+   private String secret;
+   private Long productId;
+}

+ 3 - 4
diagbotman-service/src/main/java/com/diagbot/web/ServiceTokenController.java

@@ -6,6 +6,7 @@ import com.diagbot.dto.ProductServiceDTO;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.facade.ProductServiceFacade;
 import com.diagbot.facade.ServiceTokenFacade;
+import com.diagbot.vo.ServiceTokenVo;
 import com.diagbot.vo.ProductServiceSaveVO;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -39,10 +40,8 @@ public class ServiceTokenController {
 
     @PostMapping("/hasPermission")
     @ApiIgnore
-    public RespDTO<String> hasPermission(String appkey) {
-        System.out.println(appkey);
-        return RespDTO.onSuc("1111");
-        //        return serviceTokenFacade.hasPermission(appkey, secret, productId);
+    public RespDTO<Boolean> hasPermission(@RequestBody ServiceTokenVo serviceTokenVo) {
+        return serviceTokenFacade.hasPermission(serviceTokenVo);
     }
 
     @ApiOperation(value = "生成令牌",

+ 1 - 1
diagbotman-service/src/main/resources/mapper/ServiceTokenMapper.xml

@@ -22,7 +22,7 @@
 
 
     <select id="getServiceToken" resultMap="BaseResultMap" parameterType="java.util.Map">
-        SELECT a.* FROM `diag_service_token` a  and a.is_deleted = 'N' and a.app_key_id = #{appkey} and a.app_key_secret = #{secret}
+        SELECT a.* FROM `diag_service_token` a  where a.is_deleted = 'N' and a.app_key_id = #{appkey} and a.app_key_secret = #{secret}
     </select>
 
     <select id="getByProductServiceId" resultMap="BaseResultMap" parameterType="java.lang.Long">

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

@@ -3,8 +3,10 @@ package com.diagbot.client;
 import com.diagbot.client.hystrix.DiagbotmanServiceHystrix;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.ServiceFilter;
+import com.diagbot.entity.ServiceToken;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 
 import java.util.List;
 
@@ -21,7 +23,7 @@ public interface DiagbotmanServiceClient {
     RespDTO<List<ServiceFilter>> getAll();
 
     @PostMapping("/serviceToken/hasPermission")
-    RespDTO<String> hasPermission(String appkey);
+    RespDTO<Boolean> hasPermission(@RequestBody ServiceToken st);
 
 }
 

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

@@ -3,8 +3,10 @@ package com.diagbot.client.hystrix;
 
 import com.diagbot.client.DiagbotmanServiceClient;
 import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.ServiceToken;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RequestBody;
 
 /**
  * @Description: 产品服务客户端(请求失败熔断)
@@ -22,7 +24,7 @@ public class DiagbotmanServiceHystrix implements DiagbotmanServiceClient {
     }
 
     @Override
-    public RespDTO<String> hasPermission(String appkey) {
+    public RespDTO<Boolean> hasPermission(@RequestBody ServiceToken st) {
         log.error("【hystrix】调用{}异常","hasPermisson");
         return null;
     }

+ 2 - 2
gateway-service/src/main/java/com/diagbot/dto/RespDTO.java

@@ -9,8 +9,8 @@ import java.io.Serializable;
  */
 public class RespDTO<T> implements Serializable{
 
-
-    public String code = "0";
+    public static final String TRUE_CODE = "0";
+    public String code = TRUE_CODE;
     public String msg = "";
     public T data;
 

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

@@ -0,0 +1,22 @@
+package com.diagbot.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+
+/**
+ * @Description: appkey 和 secret发送类
+ * @Author: ztg
+ * @Date: 2018/9/19 13:14
+ */
+@Getter
+@Setter
+public class ServiceToken implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+   private String appkey;
+   private String secret;
+   private Long productId;
+}

+ 13 - 14
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -4,7 +4,7 @@ import com.diagbot.client.DiagbotmanServiceClient;
 import com.diagbot.client.UserServiceClient;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.ServiceFilter;
-import com.diagbot.exception.ErrorCode;
+import com.diagbot.entity.ServiceToken;
 import com.diagbot.util.GsonUtil;
 import com.diagbot.util.ListUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -79,21 +79,21 @@ public class GlobalGatewayFilter implements GlobalFilter {
                 }
             }
         }
-        serviceName = "icss";
         if(SERVICE_FILTER.get(serviceName) != null) {
             String appkey = request.getHeaders().getFirst("appkey");
             String secret = request.getHeaders().getFirst("secret");
-            Long productId = SERVICE_FILTER.get(SERVICE_FILTER.get(serviceName));
-            productId = 1L;
+            Long productId = SERVICE_FILTER.get(serviceName);
             //获取appkey,secret对应的权限信息
-            RespDTO<String> res = diagbotmanServiceClient.hasPermission(appkey);
-            System.out.println(11);
+            ServiceToken st = new ServiceToken();
+            st.setAppkey(appkey);
+            st.setSecret(secret);
+            st.setProductId(productId);
+            RespDTO<Boolean> res = diagbotmanServiceClient.hasPermission(st);
+            if(res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
+                return getVoidMono(serverWebExchange, res);
+            }
         }
 
-//        if(1==1){
-//            return getVoidMono(serverWebExchange, CommonErrorCode.NO_PERMISSION);
-//        }
-
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
         builder.header("Authorization","Authorization Bearer token");
         gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
@@ -103,13 +103,12 @@ public class GlobalGatewayFilter implements GlobalFilter {
     /**
      * 网关抛异常
      *
-     * @param body
      */
-    private Mono<Void> getVoidMono(ServerWebExchange serverWebExchange, ErrorCode body) {
+    private Mono<Void> getVoidMono(ServerWebExchange serverWebExchange, RespDTO res) {
         RespDTO resp = new RespDTO();
         serverWebExchange.getResponse().setStatusCode(HttpStatus.OK);
-        resp.msg = body.getMsg();
-        resp.code = body.getCode();
+        resp.msg = res.msg;
+        resp.code = res.code;
         resp.data = "";
         byte[] bytes = GsonUtil.toJson(resp).getBytes(StandardCharsets.UTF_8);
         DataBuffer buffer = serverWebExchange.getResponse().bufferFactory().wrap(bytes);