From f820822b9ee0f177ef8ac614d9509488e2a51b0e Mon Sep 17 00:00:00 2001 From: lijiayao <3377164217@qq.com> Date: Tue, 19 Dec 2023 09:12:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=80=E5=94=AE=E6=97=A5=E5=BF=97+=E9=94=80?= =?UTF-8?q?=E5=94=AE=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bwie/gateway/filters/AuthFilter.java | 170 +++++++++--------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java b/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java index b570369..483141b 100644 --- a/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java +++ b/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java @@ -1,85 +1,85 @@ -//package com.bwie.gateway.filters; -// -//import com.bwie.common.constants.TokenConstants; -//import com.bwie.common.utils.JwtUtils; -//import com.bwie.common.utils.StringUtils; -//import com.bwie.gateway.config.IgnoreWhiteConfig; -//import com.bwie.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.List; -// -///** -// * @ClassName: -// * @Description: 鉴权过滤器 -// * @Author: zhuwenqiang -// * @Date: 2023/8/18 -// */ -//@Component -//public class AuthFilter implements GlobalFilter, Ordered { -// -// @Autowired -// private IgnoreWhiteConfig ignoreWhiteConfig; -// -// @Autowired -// private RedisTemplate redisTemplate; -// -// /** -// * 过滤方法 验证 token -// * @param exchange 请求的上下文 通过这个参数 可以获取到 请求对象 以及 响应对象 -// * @param chain 网关过滤器链 chain 放行 或者 拦截 -// * @return Mono -// */ -// @Override -// public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { -// // 验证当前的请求 是否需要拦截 【配置白名单请求 不拦截的请求 】 将白名单请求配置到配置文件中 -// // 获取系统白名单请求 -// List whites = ignoreWhiteConfig.getWhites(); -// // 获取当前的请求 URI -// ServerHttpRequest request = exchange.getRequest(); -// String path = request.getURI().getPath(); -// boolean matches = StringUtils.matches(path, whites); -// if (matches) { // 放行 -// return chain.filter(exchange); -// } -// // 获取token -// String token = request.getHeaders().getFirst(TokenConstants.TOKEN); -// // token 非空验证 -// if (StringUtils.isEmpty(token)) { -// // 不放行 提示错误信息 -// return GatewayUtils.errorResponse(exchange, "token不能为空!", HttpStatus.UNAUTHORIZED); -// } -// try { -// // token 合法性性 -// JwtUtils.parseToken(token); -// } catch (Exception ex) { -// return GatewayUtils.errorResponse(exchange, "token格式错误!"); -// } -// // token 是否过期 -// // 获取 UserKey -// String userKey = JwtUtils.getUserKey(token); -// if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) { -// return GatewayUtils.errorResponse(exchange, "token过期!"); -// } -// // 放行 -// return chain.filter(exchange); -// } -// -// /** -// * 当存在多个filter的时候 用来执行执行的优先级 -// * @return 数字 数字的值越小 执行的优先级越高 -// */ -// @Override -// public int getOrder() { -// return 0; -// } -//} +package com.bwie.gateway.filters; + +import com.bwie.common.constants.TokenConstants; +import com.bwie.common.utils.JwtUtils; +import com.bwie.common.utils.StringUtils; +import com.bwie.gateway.config.IgnoreWhiteConfig; +import com.bwie.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.List; + +/** + * @ClassName: + * @Description: 鉴权过滤器 + * @Author: zhuwenqiang + * @Date: 2023/8/18 + */ +@Component +public class AuthFilter implements GlobalFilter, Ordered { + + @Autowired + private IgnoreWhiteConfig ignoreWhiteConfig; + + @Autowired + private RedisTemplate redisTemplate; + + /** + * 过滤方法 验证 token + * @param exchange 请求的上下文 通过这个参数 可以获取到 请求对象 以及 响应对象 + * @param chain 网关过滤器链 chain 放行 或者 拦截 + * @return Mono + */ + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + // 验证当前的请求 是否需要拦截 【配置白名单请求 不拦截的请求 】 将白名单请求配置到配置文件中 + // 获取系统白名单请求 + List whites = ignoreWhiteConfig.getWhites(); + // 获取当前的请求 URI + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + boolean matches = StringUtils.matches(path, whites); + if (matches) { // 放行 + return chain.filter(exchange); + } + // 获取token + String token = request.getHeaders().getFirst(TokenConstants.TOKEN); + // token 非空验证 + if (StringUtils.isEmpty(token)) { + // 不放行 提示错误信息 + return GatewayUtils.errorResponse(exchange, "token不能为空!", HttpStatus.UNAUTHORIZED); + } + try { + // token 合法性性 + JwtUtils.parseToken(token); + } catch (Exception ex) { + return GatewayUtils.errorResponse(exchange, "token格式错误!"); + } + // token 是否过期 + // 获取 UserKey + String userKey = JwtUtils.getUserKey(token); + if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) { + return GatewayUtils.errorResponse(exchange, "token过期!"); + } + // 放行 + return chain.filter(exchange); + } + + /** + * 当存在多个filter的时候 用来执行执行的优先级 + * @return 数字 数字的值越小 执行的优先级越高 + */ + @Override + public int getOrder() { + return 0; + } +}