Browse Source

Merge remote-tracking branch 'origin/ywx' into wn

tags/正式3.13.0
王宁 1 year ago
parent
commit
7f0aee5d40

+ 15
- 1
gateway/pom.xml View File

@@ -43,7 +43,21 @@
43 43
             <groupId>org.springframework</groupId>
44 44
             <artifactId>spring-webmvc</artifactId>
45 45
         </dependency>
46
-
46
+        <dependency>
47
+            <groupId>com.alibaba</groupId>
48
+            <artifactId>fastjson</artifactId>
49
+            <version>1.2.56</version>
50
+        </dependency>
51
+         <!--redis -->
52
+        <dependency>
53
+            <groupId>redis.clients</groupId>
54
+            <artifactId>jedis</artifactId>
55
+            <version>2.9.3</version>
56
+        </dependency>
57
+        <dependency>
58
+            <groupId>org.springframework.boot</groupId>
59
+            <artifactId>spring-boot-starter-data-redis</artifactId>
60
+        </dependency>
47 61
     </dependencies>
48 62
     <dependencyManagement>
49 63
         <dependencies>

+ 58
- 0
gateway/src/main/java/com/xhkjedu/gateway/advice/GlobalExceptionHandlerAdvice.java View File

@@ -0,0 +1,58 @@
1
+package com.xhkjedu.gateway.advice;
2
+
3
+import com.google.common.base.Throwables;
4
+import com.xhkjedu.gateway.exceptions.GatewayException;
5
+import lombok.AllArgsConstructor;
6
+import lombok.Data;
7
+import lombok.extern.slf4j.Slf4j;
8
+import org.springframework.web.bind.annotation.ExceptionHandler;
9
+import org.springframework.web.bind.annotation.RestControllerAdvice;
10
+import org.springframework.web.server.ResponseStatusException;
11
+
12
+import java.net.ConnectException;
13
+
14
+/**
15
+ * @Description 这个不是SpringMVC统一异常处理(具体说明详见:ExceptionHandlerCore)
16
+ * @Author: YWX
17
+ * @Date 2023/11/15 17:52
18
+ **/
19
+@Slf4j
20
+@RestControllerAdvice
21
+public class GlobalExceptionHandlerAdvice {
22
+
23
+    @ExceptionHandler(GatewayException.class)
24
+    public ResultVo handler(GatewayException e) {
25
+        return new ResultVo(e.getCode(), e.getMessage());
26
+    }
27
+
28
+    @ExceptionHandler(ResponseStatusException.class)
29
+    public ResultVo handler(ResponseStatusException e) {
30
+        return new ResultVo(1, e.getReason());
31
+    }
32
+
33
+    @ExceptionHandler(ConnectException.class)
34
+    public ResultVo handler(ConnectException e) {
35
+        log.error(Throwables.getStackTraceAsString(e));
36
+        return new ResultVo(1, "网络异常,请稍候再试!");
37
+    }
38
+
39
+    @ExceptionHandler(Throwable.class)
40
+    public ResultVo handler(Throwable e) {
41
+        log.error(Throwables.getStackTraceAsString(e));
42
+        return new ResultVo(1, "服务器异常");
43
+    }
44
+
45
+}
46
+
47
+@Data
48
+@AllArgsConstructor
49
+class ResultVo{
50
+    private Integer code;// 返回是否成功  0成功 1失败
51
+    private String msg;// 返回提示信息
52
+    private Object obj;// 返回对象或者对象列表
53
+
54
+    public ResultVo(int code, String msg) {
55
+        this.code = code;
56
+        this.msg = msg;
57
+    }
58
+}

+ 68
- 0
gateway/src/main/java/com/xhkjedu/gateway/config/SpringContextHolder.java View File

@@ -0,0 +1,68 @@
1
+package com.xhkjedu.gateway.config;
2
+
3
+import org.springframework.beans.BeansException;
4
+import org.springframework.context.ApplicationContext;
5
+import org.springframework.context.ApplicationContextAware;
6
+import org.springframework.stereotype.Component;
7
+
8
+import java.lang.annotation.Annotation;
9
+import java.util.Map;
10
+
11
+@Component
12
+public class SpringContextHolder implements ApplicationContextAware {
13
+
14
+    private static ApplicationContext applicationContext;
15
+
16
+    @Override
17
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
18
+        SpringContextHolder.applicationContext = applicationContext;
19
+    }
20
+
21
+    public static ApplicationContext getApplicationContext() {
22
+        assertApplicationContext();
23
+        return applicationContext;
24
+    }
25
+
26
+    /**
27
+     * 通过实例类型获取实例
28
+     *
29
+     * @param requiredType 实例类型
30
+     * @param <T>          T
31
+     * @return T
32
+     */
33
+    public static <T> T getBean(Class<T> requiredType) {
34
+        assertApplicationContext();
35
+        return applicationContext.getBean(requiredType);
36
+    }
37
+
38
+    /**
39
+     * 通过实例,名称获取实例
40
+     *
41
+     * @param beanName 实例名称
42
+     * @param <T>      T
43
+     * @return T
44
+     */
45
+    @SuppressWarnings("unchecked")
46
+    public static <T> T getBean(String beanName) {
47
+        assertApplicationContext();
48
+        return (T) applicationContext.getBean(beanName);
49
+    }
50
+
51
+    /**
52
+     * 通过类上的注解获取类
53
+     *
54
+     * @param annotation anno
55
+     * @return map
56
+     */
57
+    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotation) {
58
+        assertApplicationContext();
59
+        return applicationContext.getBeansWithAnnotation(annotation);
60
+    }
61
+
62
+    private static void assertApplicationContext() {
63
+        if (SpringContextHolder.applicationContext == null) {
64
+            throw new RuntimeException("application Context属性为null,请检查是否注入了SpringContextHolder!");
65
+        }
66
+    }
67
+
68
+}

+ 14
- 0
gateway/src/main/java/com/xhkjedu/gateway/exceptions/GatewayException.java View File

@@ -0,0 +1,14 @@
1
+package com.xhkjedu.gateway.exceptions;
2
+
3
+import lombok.Getter;
4
+
5
+@Getter
6
+public class GatewayException extends RuntimeException {
7
+    private Integer code;
8
+    private String msg;
9
+
10
+    public GatewayException(Integer code, String msg) {
11
+        this.code = code;
12
+        this.msg = msg;
13
+    }
14
+}

+ 58
- 0
gateway/src/main/java/com/xhkjedu/gateway/handler/ErrorHandlerConfiguration.java View File

@@ -0,0 +1,58 @@
1
+package com.xhkjedu.gateway.handler;
2
+
3
+import org.springframework.beans.factory.ObjectProvider;
4
+import org.springframework.boot.autoconfigure.web.ServerProperties;
5
+import org.springframework.boot.autoconfigure.web.WebProperties;
6
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
7
+import org.springframework.boot.web.reactive.error.ErrorAttributes;
8
+import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
9
+import org.springframework.context.ApplicationContext;
10
+import org.springframework.context.annotation.Bean;
11
+import org.springframework.core.annotation.Order;
12
+import org.springframework.http.codec.ServerCodecConfigurer;
13
+import org.springframework.stereotype.Component;
14
+import org.springframework.web.reactive.result.view.ViewResolver;
15
+
16
+import java.util.Collections;
17
+import java.util.List;
18
+
19
+@Component
20
+@EnableConfigurationProperties({ServerProperties.class, WebProperties.class})
21
+public class ErrorHandlerConfiguration {
22
+
23
+    private final ServerProperties serverProperties;
24
+
25
+    private final ApplicationContext applicationContext;
26
+
27
+    private final WebProperties resourceProperties;
28
+
29
+    private final List<ViewResolver> viewResolvers;
30
+
31
+    private final ServerCodecConfigurer serverCodecConfigurer;
32
+
33
+    public ErrorHandlerConfiguration(ServerProperties serverProperties,
34
+                                     WebProperties resourceProperties,
35
+                                     ObjectProvider<List<ViewResolver>> viewResolversProvider,
36
+                                     ServerCodecConfigurer configurer,
37
+                                     ApplicationContext applicationContext) {
38
+        this.serverProperties = serverProperties;
39
+        this.applicationContext = applicationContext;
40
+        this.resourceProperties = resourceProperties;
41
+        this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
42
+        this.serverCodecConfigurer = configurer;
43
+    }
44
+
45
+    @Bean
46
+    @Order
47
+    public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes attributes) {
48
+        GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler(
49
+                attributes,
50
+                this.resourceProperties,
51
+                this.serverProperties.getError(),
52
+                this.applicationContext);
53
+        exceptionHandler.setViewResolvers(this.viewResolvers);
54
+        exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
55
+        exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
56
+        return exceptionHandler;
57
+    }
58
+}

+ 156
- 0
gateway/src/main/java/com/xhkjedu/gateway/handler/ExceptionHandlerCore.java View File

@@ -0,0 +1,156 @@
1
+package com.xhkjedu.gateway.handler;
2
+
3
+import com.google.common.collect.Maps;
4
+import com.xhkjedu.gateway.config.SpringContextHolder;
5
+import lombok.extern.slf4j.Slf4j;
6
+import org.springframework.boot.ApplicationArguments;
7
+import org.springframework.boot.ApplicationRunner;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.stereotype.Component;
10
+import org.springframework.web.bind.annotation.*;
11
+
12
+import java.lang.reflect.InvocationTargetException;
13
+import java.lang.reflect.Method;
14
+import java.util.*;
15
+
16
+/**
17
+ * @Description 虽然spring webflux支持springMVC注解,
18
+ *但是,Filter抛出的异常不属于MVC层,所以,处理Filter抛出的异常依旧不能用SpringMVC那一套注解
19
+ *但是我们可以自己模仿做一套和MVC类似的注解
20
+ *该处理类使用SpringMVC的注解
21
+ *让用户可以无感知的像处理MVC层抛出的异常那样处理Filter抛出的异常
22
+ * @Author: YWX
23
+ * @Date 2023/11/16 8:56
24
+ **/
25
+@Slf4j
26
+@Component
27
+public class ExceptionHandlerCore implements ApplicationRunner {
28
+
29
+    /**
30
+     * key是处理异常的类型
31
+     * value是处理异常的方法
32
+     */
33
+    private LinkedHashMap<Class<? extends Throwable>, Node> exceptionHandlerMap;
34
+
35
+    /**
36
+     * 解析类上的注解
37
+     * 将处理异常的方法注册到map中
38
+     */
39
+    private void register(Object exceptionAdvice) {
40
+        Method[] methods = exceptionAdvice.getClass().getMethods();
41
+        Arrays.stream(methods).forEach(method -> {
42
+            ExceptionHandler exceptionHandler = method.getAnnotation(ExceptionHandler.class);
43
+            if (Objects.isNull(exceptionHandler)) {
44
+                return;
45
+            }
46
+            ResponseStatus responseStatus = method.getAnnotation(ResponseStatus.class);
47
+            HttpStatus status = null;
48
+            if (Objects.nonNull(responseStatus)) {
49
+                status = responseStatus.value();
50
+            }
51
+            HttpStatus finalStatus = status;
52
+            Arrays.asList(exceptionHandler.value()).forEach(a -> exceptionHandlerMap.put(a, new Node(method, exceptionAdvice, finalStatus)));
53
+        });
54
+    }
55
+
56
+    /**
57
+     * 根据异常对象获取解决异常的方法
58
+     *
59
+     * @param throwable 异常对象
60
+     * @return handler method
61
+     */
62
+    private Node getHandlerExceptionMethodNode(Throwable throwable) {
63
+        ArrayList<Class<?>> superClass = this.getSuperClass(throwable.getClass());
64
+        for (Class<?> aClass : superClass) {
65
+            Node handlerNode = null;
66
+            if ((handlerNode = exceptionHandlerMap.get(aClass)) != null) {
67
+                return handlerNode;
68
+            }
69
+        }
70
+        return null;
71
+    }
72
+
73
+    @Override
74
+    public void run(ApplicationArguments args) throws Exception {
75
+        Map<String, Object> beans = SpringContextHolder.getBeansWithAnnotation(RestControllerAdvice.class);
76
+        log.info("-------------异常处理对象获取完毕-------------");
77
+        exceptionHandlerMap = Maps.newLinkedHashMapWithExpectedSize(beans.size());
78
+        log.info("-------------异常处理容器内存分配完毕-------------");
79
+        beans.keySet()
80
+             .stream()
81
+             .map(beans::get)
82
+             .forEach(this::register);
83
+        log.info("-------------异常处理方法注册完毕-------------");
84
+    }
85
+
86
+    /**
87
+     * 对外暴露的处理异常的方法
88
+     *
89
+     * @param throwable 处理的异常
90
+     * @return 调用异常后的返回值
91
+     */
92
+    public HashMap<String, Object> handlerException(Throwable throwable) {
93
+        Node exceptionMethodNode = this.getHandlerExceptionMethodNode(throwable);
94
+        if (Objects.isNull(exceptionMethodNode)) {
95
+            log.error("未定义异常处理方法,处理异常失败,异常处理信息如下:{}", throwable.getMessage());
96
+            throw new RuntimeException("未定义异常处理方法,处理异常失败");
97
+        }
98
+        /**
99
+         * 本人水平有限。
100
+         * 现在支持持一个入参类型类{@link Throwable}
101
+         * 后人可以随意扩展~
102
+         */
103
+        Object returnResult = null;
104
+        try {
105
+            returnResult = exceptionMethodNode.method.invoke(exceptionMethodNode.thisObj, throwable);
106
+        } catch (IllegalAccessException | InvocationTargetException e) {
107
+            e.printStackTrace();
108
+        }
109
+        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
110
+        resultMap.put("status", exceptionMethodNode.status);
111
+        resultMap.put("result", returnResult);
112
+        return resultMap;
113
+    }
114
+
115
+    /**
116
+     * 用于存放方法和方法所在的实例
117
+     */
118
+    private static class Node {
119
+        Node(Method method, Object thisObj, HttpStatus status) {
120
+            this.method = method;
121
+            this.thisObj = thisObj;
122
+            this.status = status;
123
+        }
124
+
125
+        /**
126
+         * 状态码
127
+         */
128
+        HttpStatus status;
129
+        /**
130
+         * 处理异常的方法
131
+         */
132
+        Method method;
133
+        /**
134
+         * 方法所在的实例
135
+         */
136
+        Object thisObj;
137
+    }
138
+
139
+
140
+    /**
141
+     * 获取该类的class以及所有父的class
142
+     *
143
+     * @param clazz this.class
144
+     * @return list
145
+     */
146
+    public ArrayList<Class<?>> getSuperClass(Class<?> clazz) {
147
+        ArrayList<Class<?>> classes = new ArrayList<>();
148
+        classes.add(clazz);
149
+        Class<?> suCl = clazz.getSuperclass();
150
+        while (suCl != null) {
151
+            classes.add(suCl);
152
+            suCl = suCl.getSuperclass();
153
+        }
154
+        return classes;
155
+    }
156
+}

+ 84
- 0
gateway/src/main/java/com/xhkjedu/gateway/handler/GlobalExceptionHandler.java View File

@@ -0,0 +1,84 @@
1
+package com.xhkjedu.gateway.handler;
2
+
3
+import lombok.extern.slf4j.Slf4j;
4
+import org.springframework.boot.autoconfigure.web.ErrorProperties;
5
+import org.springframework.boot.autoconfigure.web.WebProperties;
6
+import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
7
+import org.springframework.boot.web.error.ErrorAttributeOptions;
8
+import org.springframework.boot.web.reactive.error.ErrorAttributes;
9
+import org.springframework.context.ApplicationContext;
10
+import org.springframework.http.HttpStatus;
11
+import org.springframework.http.MediaType;
12
+import org.springframework.web.reactive.function.BodyInserters;
13
+import org.springframework.web.reactive.function.server.*;
14
+import reactor.core.publisher.Mono;
15
+
16
+import javax.annotation.Resource;
17
+import java.util.Map;
18
+import java.util.Objects;
19
+
20
+/**
21
+ * @Description 全局异常处理
22
+ * @Author: YWX
23
+ * @Date 2023/11/16 8:57
24
+ **/
25
+@Slf4j
26
+public class GlobalExceptionHandler extends DefaultErrorWebExceptionHandler {
27
+
28
+    @Resource
29
+    private ExceptionHandlerCore handlerCore;
30
+
31
+
32
+    public GlobalExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) {
33
+        super(errorAttributes, webProperties.getResources(), errorProperties, applicationContext);
34
+    }
35
+
36
+    /**
37
+     * 统一处理异常信息
38
+     */
39
+    @Override
40
+    @SuppressWarnings(value = "unchecked")
41
+    protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
42
+        Throwable error = super.getError(request);
43
+        //调用处理异常的方法,并将对象转换成map
44
+        return handlerCore.handlerException(error);
45
+    }
46
+
47
+    /**
48
+     * Extract the error attributes from the current request, to be used to populate error
49
+     * views or JSON payloads.
50
+     *
51
+     * @param request the source request
52
+     * @param options options to control error attributes
53
+     * @return the error attributes as a Map
54
+     */
55
+    @Override
56
+    protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
57
+        Throwable error = super.getError(request);
58
+        return handlerCore.handlerException(error);
59
+    }
60
+
61
+    @Override
62
+    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
63
+        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
64
+    }
65
+
66
+    @Override
67
+    protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
68
+        boolean includeStackTrace = isIncludeStackTrace(request, MediaType.ALL);
69
+        Map<String, Object> error = getErrorAttributes(request, includeStackTrace);
70
+        return ServerResponse
71
+                .status(getHttpStatus(error))
72
+                .contentType(MediaType.APPLICATION_JSON_UTF8)
73
+                .body(BodyInserters.fromObject(error.get("result")));
74
+    }
75
+
76
+    @Override
77
+    protected int getHttpStatus(Map<String, Object> errorAttributes) {
78
+        HttpStatus status = (HttpStatus) errorAttributes.get("status");
79
+        if (Objects.isNull(status)) {
80
+            return HttpStatus.OK.value();
81
+        }
82
+        return status.value();
83
+    }
84
+}

+ 27
- 6
gateway/src/main/java/com/xhkjedu/gateway/interceptors/DecryptRequestionBodyFilter.java View File

@@ -1,8 +1,10 @@
1 1
 package com.xhkjedu.gateway.interceptors;
2 2
 
3
-import com.xhkjedu.gateway.utils.AES;
3
+import com.xhkjedu.gateway.exceptions.GatewayException;
4
+import com.xhkjedu.gateway.utils.*;
4 5
 import lombok.SneakyThrows;
5 6
 import lombok.extern.slf4j.Slf4j;
7
+import org.springframework.beans.factory.annotation.Value;
6 8
 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
7 9
 import org.springframework.cloud.gateway.filter.GlobalFilter;
8 10
 import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
@@ -19,8 +21,7 @@ import org.springframework.util.AntPathMatcher;
19 21
 import org.springframework.util.CollectionUtils;
20 22
 import org.springframework.web.reactive.function.BodyInserter;
21 23
 import org.springframework.web.reactive.function.BodyInserters;
22
-import org.springframework.web.reactive.function.server.HandlerStrategies;
23
-import org.springframework.web.reactive.function.server.ServerRequest;
24
+import org.springframework.web.reactive.function.server.*;
24 25
 import org.springframework.web.server.ServerWebExchange;
25 26
 import reactor.core.publisher.Flux;
26 27
 import reactor.core.publisher.Mono;
@@ -34,7 +35,11 @@ import java.util.List;
34 35
  **/
35 36
 @Component
36 37
 @Slf4j
37
-public class DecryptRequestionBodyFilter implements GlobalFilter, Ordered {
38
+public class DecryptRequestionBodyFilter extends JedisUtil implements GlobalFilter, Ordered {
39
+    @Value("${Xh-St}")
40
+    private String xhSt;
41
+    @Value("${whitelistAPI}")
42
+    private String whitelistAPI;
38 43
 
39 44
     private static String AES_KEY = "XINGHUOLIAOYUAN7";
40 45
 
@@ -51,12 +56,28 @@ public class DecryptRequestionBodyFilter implements GlobalFilter, Ordered {
51 56
 //        log.info("HttpMethod:{},Url:{}", request.getMethod(), request.getURI().getRawPath());
52 57
         List<String> decrypts = headers.get("st");
53 58
         String decrypt = isListNotEmpty(decrypts)?decrypts.get(0):"false";
59
+        response.getHeaders().add("Xh-St", xhSt);
60
+        response.getHeaders().add("Access-Control-Expose-Headers","Xh-St");
61
+        Mono<Void> voidMono;
54 62
         if (!decrypt.equals("true")) {
55
-            return chain.filter(exchange);
63
+            voidMono = chain.filter(exchange);
56 64
         }else{
57
-            return readBody(exchange, chain);
65
+            voidMono = readBody(exchange, chain);
58 66
         }
59 67
 
68
+        String key = ApiUtil.listToStr(headers.get("Token-Key"));
69
+        String token = ApiUtil.listToStr(headers.get("Token-Value"));
70
+        if (!ApiUtil.whiteAPI(path, whitelistAPI)) {//非白名单接口验证token
71
+            String redis_token = get(key);//系统缓存中保存的token
72
+            if (ApiUtil.isEmpty(token) || !token.equals(redis_token)) {
73
+                GatewayException throwable = new GatewayException(2, "登录失效了");
74
+                return Mono.error(throwable);
75
+            } else {
76
+                refreshToken(key, redis_token);//刷新token
77
+            }
78
+        }
79
+        return voidMono;
80
+
60 81
 //        //  登录跳过网关验证,检查白名单(配置)最好把不拦截路径放入配置文件,此处通过正则
61 82
 //        if(antPathMatcher.match("/**/api/login/auth/**",path)){
62 83
 //            return readBody(exchange, chain);

+ 128
- 0
gateway/src/main/java/com/xhkjedu/gateway/utils/ApiUtil.java View File

@@ -0,0 +1,128 @@
1
+package com.xhkjedu.gateway.utils;
2
+
3
+import lombok.extern.slf4j.Slf4j;
4
+import org.springframework.util.CollectionUtils;
5
+
6
+import java.text.SimpleDateFormat;
7
+import java.util.Date;
8
+import java.util.List;
9
+
10
+/**
11
+ * @ClassName ApiUtil
12
+ * Description api工具类
13
+ * Author WN
14
+ * Date 2022/2/21 11:08
15
+ **/
16
+@Slf4j
17
+public class ApiUtil {
18
+
19
+    /*
20
+    * @Description 比较当前时间和时间段
21
+    * @Author WN
22
+    * @Date 2023/6/15 11:24:59
23
+    */
24
+    public static boolean compareCurrDateTime(String starttimeStr,String stoptimeStr) throws Exception{
25
+        boolean rtn = false;
26
+        if(isEmpty(starttimeStr) || isEmpty(stoptimeStr)){
27
+            rtn = false;
28
+        }else{
29
+            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
30
+
31
+            Date starttime = sdf.parse(starttimeStr); //开始时间
32
+            Date stoptime = sdf.parse(stoptimeStr); //结束时间
33
+
34
+            Date nowtime = new Date();
35
+            String times = sdf.format(nowtime);
36
+            Date currtime = sdf.parse(times);//当前时间
37
+
38
+            //当前时间在开始时间之前或者等于开始时间  并且 当前时间在结束时间之后或者等于结束时间
39
+            if((starttime.before(currtime) || starttime.equals(currtime)) && (stoptime.after(currtime) || stoptime.equals(currtime))){
40
+                rtn = true;
41
+            }
42
+        }
43
+
44
+        return rtn;
45
+    }
46
+    /**
47
+     * 字符串是否为空
48
+     *
49
+     * @param: @param obj
50
+     * @param: @return
51
+     * @author WN
52
+     */
53
+    public static boolean isNotEmpty(Object obj) {
54
+        if (null == obj || "".equals(obj.toString().trim()) || "null".equals(obj.toString().trim())) {
55
+            return false;
56
+        } else {
57
+            return true;
58
+        }
59
+    }
60
+
61
+    /**
62
+     * 字符串是否为空
63
+     *
64
+     * @param: @param obj
65
+     * @param: @return
66
+     * @author zj
67
+     */
68
+    public static boolean isEmpty(Object obj) {
69
+        if (null == obj || "".equals(obj.toString().trim()) || "null".equals(obj.toString().trim())) {
70
+            return true;
71
+        } else {
72
+            return false;
73
+        }
74
+    }
75
+
76
+    /**
77
+     * @Description 是否是白名单接口
78
+     * @Date 2023/11/16 9:02
79
+     * @Author YWX
80
+     * @Param [requestURI, whitelistAPI]
81
+     * @Return boolean
82
+     **/
83
+    public static boolean whiteAPI(String requestURI, String whitelistAPI) {
84
+        boolean rtn = false;
85
+        if (isEmpty(whitelistAPI)) return rtn;
86
+
87
+        String[] apis = whitelistAPI.split(",");
88
+        for (String api : apis) {
89
+            if (requestURI.contains(api)) {
90
+                rtn = true;
91
+                break;
92
+            }
93
+        }
94
+        return rtn;
95
+    }
96
+
97
+    /**
98
+     * @Description 判断list是否不为空
99
+     * @Date 2023/11/16 9:01
100
+     * @Author YWX
101
+     * @Param [list]
102
+     * @Return boolean
103
+     **/
104
+    public static boolean isListNotEmpty(List list) {
105
+        if (CollectionUtils.isEmpty(list)){
106
+            return false;
107
+        } else {
108
+            return true;
109
+        }
110
+    }
111
+
112
+    /**
113
+     * @Description list转字符
114
+     * @Date 2023/11/16 9:01
115
+     * @Author YWX
116
+     * @Param [list]
117
+     * @Return java.lang.String
118
+     **/
119
+    public static String listToStr(List<String> list) {
120
+        String str = "";
121
+        if (isListNotEmpty(list)) {
122
+            for (String s : list) {
123
+                str += s;
124
+            }
125
+        }
126
+        return str;
127
+    }
128
+}

+ 123
- 0
gateway/src/main/java/com/xhkjedu/gateway/utils/JedisUtil.java View File

@@ -0,0 +1,123 @@
1
+package com.xhkjedu.gateway.utils;
2
+
3
+import com.alibaba.fastjson.JSON;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.beans.factory.annotation.Value;
6
+import org.springframework.data.redis.core.StringRedisTemplate;
7
+import org.springframework.data.redis.core.ValueOperations;
8
+import org.springframework.stereotype.Component;
9
+
10
+import javax.annotation.PostConstruct;
11
+import java.util.concurrent.TimeUnit;
12
+
13
+/**
14
+ * @author ywx
15
+ * @classname JedisUtil
16
+ * @description Redis工具类
17
+ * @date 2019/8/2 16:48
18
+ **/
19
+@Component
20
+public class JedisUtil {
21
+	@Autowired
22
+	private StringRedisTemplate stringRedisTemplate;
23
+	private ValueOperations<String, String> valueOperations;
24
+
25
+	@PostConstruct
26
+	public void init(){
27
+		this.valueOperations = stringRedisTemplate.opsForValue();
28
+	}
29
+
30
+	/**  默认过期时长,单位:秒 */
31
+	//redis失效时间
32
+	@Value("${redisdatatime}")
33
+	private Integer redisdatatime;
34
+	/**  不设置过期时长 */
35
+	public final static long NOT_EXPIRE = -1;
36
+
37
+	public boolean set(String key, Object value, Integer expire){
38
+		boolean rtn = false;
39
+		valueOperations.set(key, toJson(value));
40
+		if(expire != NOT_EXPIRE){
41
+			rtn = stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
42
+		}
43
+		return rtn;
44
+	}
45
+
46
+	public void set(String key, Object value){
47
+		set(key, value, redisdatatime);
48
+	}
49
+
50
+	public Boolean setObject(String key, Object value, int cacheSeconds) {
51
+		String val = toJson(value);
52
+		return set(key,val,cacheSeconds);
53
+	}
54
+
55
+	public <T> T get(String key, Class<T> clazz, long expire) {
56
+		String value = valueOperations.get(key);
57
+		if(expire != NOT_EXPIRE){
58
+			stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
59
+		}
60
+		return value == null ? null : fromJson(value, clazz);
61
+	}
62
+
63
+	public <T> T get(String key, Class<T> clazz) {
64
+		return get(key, clazz, NOT_EXPIRE);
65
+	}
66
+
67
+	public String get(String key, long expire) {
68
+		String value = valueOperations.get(key);
69
+		if (expire != NOT_EXPIRE) {
70
+			stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
71
+		}
72
+		return value;
73
+	}
74
+
75
+	public String get(String key) {
76
+		try {
77
+			return get(key, NOT_EXPIRE);
78
+		} catch (Exception e) {
79
+			return "0";
80
+		}
81
+	}
82
+
83
+	//获取剩余有效期
84
+	public Long getExpire(String key) {
85
+		return stringRedisTemplate.getExpire(key);
86
+	}
87
+
88
+	//如果token有效期低于10分钟更新有效期
89
+	public void refreshToken(String key,String value) {
90
+		Long expire = getExpire(key);
91
+		if (expire <= 10 * 60) {
92
+			set(key, value);
93
+		}
94
+	}
95
+
96
+	public Object getObject(String key, Class<?> clazz) {
97
+		String value = get(key);
98
+		return fromJson(value, clazz);
99
+	}
100
+
101
+	public boolean delete(String key) {
102
+		return stringRedisTemplate.delete(key);
103
+	}
104
+
105
+	/**
106
+	 * Object转成JSON数据
107
+	 */
108
+	private String toJson(Object object){
109
+		if(object instanceof Integer || object instanceof Long || object instanceof Float ||
110
+				object instanceof Double || object instanceof Boolean || object instanceof String){
111
+			return String.valueOf(object);
112
+		}
113
+		return JSON.toJSONString(object);
114
+	}
115
+
116
+	/**
117
+	 * JSON数据,转成Object
118
+	 */
119
+	private <T> T fromJson(String json, Class<T> clazz){
120
+		return JSON.parseObject(json, clazz);
121
+	}
122
+
123
+}

+ 24
- 0
gateway/src/main/resources/application.properties View File

@@ -66,3 +66,27 @@ logging.level.com.netflix=error
66 66
 
67 67
 #请求超时时间30s
68 68
 rq.overtime = 30000
69
+
70
+#接口是否加密
71
+Xh-St = false
72
+
73
+#redis配置
74
+spring.redis.host=cachetest.xhkjedu.com
75
+spring.redis.port=6379
76
+spring.redis.password=xhkjedud07
77
+#超时时间:单位ms
78
+spring.redis.timeout=5000
79
+#设置过期时间10min
80
+spring.redis.jedis.pool.max-idle=300
81
+#最大等待时间:单位ms //高版本改为maxWaitMillis
82
+spring.redis.jedis.pool.max-wait=1000000
83
+spring.redis.jedis.pool.max-active=60000
84
+#缓存访问数据有效时长60*60*24*7
85
+redisdatatime=604800
86
+
87
+#白名单接口
88
+whitelistAPI=user/login,/class_student/stu_class,/user/detail,/status/info,/user/qrg,/user/check_user\
89
+  ,/user/update_code,/user/update_pwd_byCode,/user/list_usecret,/user/check_usecret,/user/reset_pwd\
90
+  ,/get_new,/stype/list,/cloud/detail\
91
+  ,/pdf\
92
+  ,/section/get_zjr,/csw/add

Loading…
Cancel
Save