From 6afce02f78ccd608233c93c3b8fc18593be4b108 Mon Sep 17 00:00:00 2001 From: zhanghaining <2218834824@qq.com> Date: Sun, 15 Oct 2023 11:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=9F=BA=E6=9C=AC=E6=9D=83?= =?UTF-8?q?=E9=99=90=E9=89=B4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/health/common/domain/User.java | 3 + .../gateway/config/IgnoreWhiteConfig.java | 2 +- .../health/gateway/filters/AuthFilters.java | 83 +++++++++++++++++++ .../health/gateway/utils/GatewayUtils.java | 2 +- 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 health_gateway/src/main/java/com/health/gateway/filters/AuthFilters.java diff --git a/health_common/src/main/java/com/health/common/domain/User.java b/health_common/src/main/java/com/health/common/domain/User.java index 377029a..52a5a25 100644 --- a/health_common/src/main/java/com/health/common/domain/User.java +++ b/health_common/src/main/java/com/health/common/domain/User.java @@ -2,6 +2,8 @@ package com.health.common.domain; import lombok.Data; +import java.util.Date; + /** * @author Administrator */ @@ -12,6 +14,7 @@ public class User { private String username; private String password; private String emil; + private Date lastTime; diff --git a/health_gateway/src/main/java/com/health/gateway/config/IgnoreWhiteConfig.java b/health_gateway/src/main/java/com/health/gateway/config/IgnoreWhiteConfig.java index 848c7fd..e135647 100644 --- a/health_gateway/src/main/java/com/health/gateway/config/IgnoreWhiteConfig.java +++ b/health_gateway/src/main/java/com/health/gateway/config/IgnoreWhiteConfig.java @@ -11,8 +11,8 @@ import java.util.ArrayList; import java.util.List; /** + * @author Administrator * @description: 放行白名单配置 - * @author DongZl */ @Configuration @RefreshScope diff --git a/health_gateway/src/main/java/com/health/gateway/filters/AuthFilters.java b/health_gateway/src/main/java/com/health/gateway/filters/AuthFilters.java new file mode 100644 index 0000000..dbf0f4f --- /dev/null +++ b/health_gateway/src/main/java/com/health/gateway/filters/AuthFilters.java @@ -0,0 +1,83 @@ +package com.health.gateway.filters; + +import cn.hutool.core.date.DateUnit; +import cn.hutool.core.date.DateUtil; +import com.alibaba.fastjson.JSONObject; +import com.health.common.constants.TokenConstants; +import com.health.common.domain.User; +import com.health.common.utils.JwtUtils; +import com.health.common.utils.StringUtils; +import com.health.gateway.config.IgnoreWhiteConfig; +import com.health.gateway.utils.GatewayUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@Component +public class AuthFilters implements GlobalFilter, Ordered { + + @Autowired + private IgnoreWhiteConfig ignoreWhitesConfig; + + @Autowired + private RedisTemplate redisTemplate; + + + /** + * @param exchange + * @param chain + * @return + */ + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + List whites = ignoreWhitesConfig.getWhites(); + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + if (StringUtils.matches(path, whites)) { + return chain.filter(exchange); + } + String token = request.getHeaders().getFirst(TokenConstants.TOKEN); + if (StringUtils.isEmpty(token)) { + return GatewayUtils.errorResponse(exchange, "token不能为空!", HttpStatus.UNAUTHORIZED); + } + try { + JwtUtils.parseToken(token); + } catch (Exception e) { + return GatewayUtils.errorResponse(exchange, "token不合法!"); + } + String userKey = JwtUtils.getUserKey(token); + Boolean hasKey = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey); + if (null == hasKey || !hasKey) { + return GatewayUtils.errorResponse(exchange, "token过期!"); + } + String jsonStr = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + User user = JSONObject.parseObject(jsonStr, User.class); + Date lastLoginTime = user.getLastTime(); + long between = DateUtil.between(lastLoginTime, new Date(), DateUnit.MINUTE); + if (between >= 10) { + redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 15, TimeUnit.MINUTES); + } + // 验证通过放行 + return chain.filter(exchange); + + } + + /** + * @return + */ + @Override + public int getOrder() { + return 0; + } +} diff --git a/health_gateway/src/main/java/com/health/gateway/utils/GatewayUtils.java b/health_gateway/src/main/java/com/health/gateway/utils/GatewayUtils.java index c2a8316..3334207 100644 --- a/health_gateway/src/main/java/com/health/gateway/utils/GatewayUtils.java +++ b/health_gateway/src/main/java/com/health/gateway/utils/GatewayUtils.java @@ -13,7 +13,7 @@ import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; /** - * @author DongZl + * @author Administrator * @description: 网关处理工具类 */ @Log4j2