houweiyu 4 년 전
부모
커밋
c99d7c28d2

+ 39 - 0
src/main/java/com/fcb/gateway/enums/ResultCodeEnum.java

@@ -0,0 +1,39 @@
+package com.fcb.gateway.enums;
+
+/**
+ * @author abnerhou
+ * @date 2020/5/6 8:23
+ * @desciption
+ */
+public enum ResultCodeEnum {
+
+    D3001(3001 , "缺少必要参数" , true),
+    D3002(3002 , "token非法" , false),
+
+
+    D100(3100 , "系统异常" , true),
+    D101(3101 , "数据异常" , true),
+    ;
+
+    ResultCodeEnum(Integer code, String desc , Boolean canBeReplace) {
+        this.code = code;
+        this.desc = desc;
+        this.canBeReplace = canBeReplace;
+    }
+
+    private Integer code;
+    private String desc;
+    private Boolean canBeReplace;
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public Boolean getCanBeReplace() {
+        return canBeReplace;
+    }
+}

+ 48 - 0
src/main/java/com/fcb/gateway/exception/CommonBaseException.java

@@ -0,0 +1,48 @@
+package com.fcb.gateway.exception;
+
+import com.fcb.gateway.enums.ResultCodeEnum;
+import lombok.Data;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * @author abnerhou
+ * @date 2020/5/25 11:39
+ * @desciption
+ */
+@ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR,reason="server error")
+@Data
+public class CommonBaseException extends RuntimeException{
+    private static final long serialVersionUID = 2899335020273674737L;
+
+    private Integer code;
+
+    private String msg;
+
+    public CommonBaseException(Integer code, String msg){
+        super(msg);
+        this.code = code;
+        this.msg = msg;
+    }
+
+
+    public CommonBaseException(ResultCodeEnum resultCodeEnum){
+        super(resultCodeEnum.getDesc());
+        this.code = resultCodeEnum.getCode();
+        this.msg = resultCodeEnum.getDesc();
+    }
+
+    public CommonBaseException(ResultCodeEnum resultCodeEnum , String replaceMsg){
+        super(resultCodeEnum.getDesc());
+        this.code = resultCodeEnum.getCode();
+        if(resultCodeEnum.getCanBeReplace()){
+            this.msg = replaceMsg;
+        }else{
+            this.msg = resultCodeEnum.getDesc();
+        }
+    }
+
+
+
+
+}

+ 65 - 0
src/main/java/com/fcb/gateway/exception/ExceptionHandlerConfiguration.java

@@ -0,0 +1,65 @@
+package com.fcb.gateway.exception;
+
+import org.springframework.beans.factory.ObjectProvider;
+import org.springframework.boot.autoconfigure.web.ResourceProperties;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.boot.web.reactive.error.ErrorAttributes;
+import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.codec.ServerCodecConfigurer;
+import org.springframework.web.reactive.result.view.ViewResolver;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author abnerhou
+ * @date 2020/9/29 15:30
+ * @desciption
+ */
+@Configuration
+@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
+public class ExceptionHandlerConfiguration {
+
+    private final ServerProperties serverProperties;
+
+    private final ApplicationContext applicationContext;
+
+    private final ResourceProperties resourceProperties;
+
+    private final List<ViewResolver> viewResolvers;
+
+    private final ServerCodecConfigurer serverCodecConfigurer;
+
+    public ExceptionHandlerConfiguration(ServerProperties serverProperties,
+                                         ResourceProperties resourceProperties,
+                                         ObjectProvider<List<ViewResolver>> viewResolversProvider,
+                                         ServerCodecConfigurer serverCodecConfigurer,
+                                         ApplicationContext applicationContext) {
+        this.serverProperties = serverProperties;
+        this.applicationContext = applicationContext;
+        this.resourceProperties = resourceProperties;
+        this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
+        this.serverCodecConfigurer = serverCodecConfigurer;
+    }
+
+    @Bean
+    @Order(Ordered.HIGHEST_PRECEDENCE)
+    public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
+        JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(
+                errorAttributes,
+                this.resourceProperties,
+                this.serverProperties.getError(),
+                this.applicationContext);
+        exceptionHandler.setViewResolvers(this.viewResolvers);
+        exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
+        exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
+        return exceptionHandler;
+    }
+
+}

+ 97 - 0
src/main/java/com/fcb/gateway/exception/JsonExceptionHandler.java

@@ -0,0 +1,97 @@
+package com.fcb.gateway.exception;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.web.ErrorProperties;
+import org.springframework.boot.autoconfigure.web.ResourceProperties;
+import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
+import org.springframework.boot.web.reactive.error.ErrorAttributes;
+import org.springframework.context.ApplicationContext;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.reactive.function.server.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author abnerhou
+ * @date 2020/9/29 15:32
+ * @desciption
+ */
+public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
+
+    private static Logger logger = LoggerFactory.getLogger(JsonExceptionHandler.class);
+
+    public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
+                                ErrorProperties errorProperties, ApplicationContext applicationContext) {
+        super(errorAttributes, resourceProperties, errorProperties, applicationContext);
+    }
+
+    /**
+     * 获取异常属性
+     */
+    @Override
+    protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
+        int code = HttpStatus.INTERNAL_SERVER_ERROR.value();
+        Throwable error = super.getError(request);
+        if (error instanceof org.springframework.cloud.gateway.support.NotFoundException) {
+            code = HttpStatus.NOT_FOUND.value();
+        }
+//        return response(code, this.buildMessage(request, error));
+        return response(code, error.getMessage());
+    }
+
+    /**
+     * 指定响应处理方法为JSON处理的方法
+     * @param errorAttributes
+     */
+    @Override
+    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
+        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
+    }
+
+
+    /**
+     * 根据code获取对应的HttpStatus
+     * @param errorAttributes
+     */
+    @Override
+    protected int getHttpStatus(Map<String, Object> errorAttributes) {
+        int statusCode = (int) errorAttributes.get("code");
+        return statusCode;
+    }
+
+    /**
+     * 构建异常信息
+     * @param request
+     * @param ex
+     * @return
+     */
+    private String buildMessage(ServerRequest request, Throwable ex) {
+        StringBuilder message = new StringBuilder("Failed to handle request [");
+        message.append(request.methodName());
+        message.append(" ");
+        message.append(request.uri());
+        message.append("]");
+        if (ex != null) {
+            message.append(": ");
+            message.append(ex.getMessage());
+        }
+        return message.toString();
+    }
+
+    /**
+     * 构建返回的JSON数据格式
+     * @param status        状态码
+     * @param errorMessage  异常信息
+     * @return
+     */
+    public static Map<String, Object> response(int status, String errorMessage) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("code", status);
+        map.put("message", errorMessage);
+        map.put("data", null);
+        logger.error(map.toString());
+        return map;
+    }
+}

+ 5 - 1
src/main/java/com/fcb/gateway/filter/AuthAndLogFilter.java

@@ -45,11 +45,15 @@ public class AuthAndLogFilter implements GlobalFilter, Ordered {
                 || requestUrl.startsWith("/api/scene/app/getScreencapVoice")
                 || requestUrl.startsWith("/api/scene/getInfo")
                 || requestUrl.startsWith("/fcb/pano/common/")
+                || requestUrl.startsWith("/api/scene/file/")
+                || requestUrl.startsWith("/api/manage/user/getVerify")
+                || requestUrl.startsWith("/api/manage/user/checkVerify")
         ) {
             log.info("白名单路径,无需校验token");
         } else {
-            /*if (!CollectionUtils.isEmpty(tokenList)) {
+           /* if (!CollectionUtils.isEmpty(tokenList)) {
                 String token = tokenList.get(0);
+                log.info("上送的token为:{}" , token);
                 if (!redisTemplate.hasKey(token)) {
                     log.info("登录超时,需要重新登录");
                     mapResult.put("code", 500);

+ 1 - 1
src/main/resources/application-test.properties

@@ -1,6 +1,6 @@
 
 spring.redis.cluster.max-redirects=3
-spring.redis.cluster.nodes=127.0.0.1:30064
+spring.redis.cluster.nodes=10.1.152.54:6379
 spring.redis.timeout=5000ms
 spring.redis.jedis.pool.max-active=8
 spring.redis.jedis.pool.max-idle=8

+ 2 - 2
src/main/resources/bootstrap.yml

@@ -25,9 +25,9 @@ spring:
             fallbackUri: 'forward:/defaultFallback'
 
   profiles:
-    active: test
+    active: dev
 logging:
-  config: classpath:log4j2-spring.xml
+  config: classpath:log4j2.xml
 
 hystrix:
   command: