From 702b2dd7fef4dd2d08e023e46900102e47282cba Mon Sep 17 00:00:00 2001
From: DongZeLiang <2746733890@qq.com>
Date: Wed, 10 Jan 2024 15:29:17 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 32 +++++
muyu-auth/pom.xml | 34 ++++++
.../java/com/muyu/auth/AuthApplication.java | 18 +++
muyu-auth/src/main/resources/bootstrap.yml | 29 +++++
muyu-common/pom.xml | 110 ++++++++++++++++++
.../com/muyu/common/constant/Constants.java | 18 +++
.../muyu/common/constant/JwtConstants.java | 27 +++++
.../muyu/common/constant/TokenConstants.java | 24 ++++
.../com/muyu/common/result/PageResult.java | 34 ++++++
.../java/com/muyu/common/result/Result.java | 53 +++++++++
.../java/com/muyu/common/utils/JwtUtils.java | 104 +++++++++++++++++
.../com/muyu/common/utils/StringUtils.java | 68 +++++++++++
muyu-gateway/pom.xml | 60 ++++++++++
.../java/com/muyu/GatewayApplication.java | 17 +++
.../muyu/config/GatewaySentinelConfig.java | 71 +++++++++++
.../com/muyu/config/IgnoreWhiteConfig.java | 31 +++++
.../main/java/com/muyu/filter/AuthFilter.java | 84 +++++++++++++
.../java/com/muyu/utils/GatewayUtils.java | 73 ++++++++++++
muyu-gateway/src/main/resources/bootstrap.yml | 29 +++++
muyu-modules/muyu-system/pom.xml | 33 ++++++
.../com/muyu/system/SystemApplication.java | 19 +++
.../src/main/resources/bootstrap.yml | 29 +++++
muyu-modules/pom.xml | 24 ++++
pom.xml | 67 +++++++++++
24 files changed, 1088 insertions(+)
create mode 100644 .gitignore
create mode 100644 muyu-auth/pom.xml
create mode 100644 muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java
create mode 100644 muyu-auth/src/main/resources/bootstrap.yml
create mode 100644 muyu-common/pom.xml
create mode 100644 muyu-common/src/main/java/com/muyu/common/constant/Constants.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/constant/JwtConstants.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/constant/TokenConstants.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/result/PageResult.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/result/Result.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/utils/JwtUtils.java
create mode 100644 muyu-common/src/main/java/com/muyu/common/utils/StringUtils.java
create mode 100644 muyu-gateway/pom.xml
create mode 100644 muyu-gateway/src/main/java/com/muyu/GatewayApplication.java
create mode 100644 muyu-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java
create mode 100644 muyu-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java
create mode 100644 muyu-gateway/src/main/java/com/muyu/filter/AuthFilter.java
create mode 100644 muyu-gateway/src/main/java/com/muyu/utils/GatewayUtils.java
create mode 100644 muyu-gateway/src/main/resources/bootstrap.yml
create mode 100644 muyu-modules/muyu-system/pom.xml
create mode 100644 muyu-modules/muyu-system/src/main/java/com/muyu/system/SystemApplication.java
create mode 100644 muyu-modules/muyu-system/src/main/resources/bootstrap.yml
create mode 100644 muyu-modules/pom.xml
create mode 100644 pom.xml
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c6ad711
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,32 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+/.idea
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
diff --git a/muyu-auth/pom.xml b/muyu-auth/pom.xml
new file mode 100644
index 0000000..620bdc2
--- /dev/null
+++ b/muyu-auth/pom.xml
@@ -0,0 +1,34 @@
+
+
+ 4.0.0
+
+ com.muyu
+ cloud-demo
+ 1.0.0
+
+
+ muyu-auth
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ com.muyu
+ muyu-common
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+
diff --git a/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java b/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java
new file mode 100644
index 0000000..7ea2b84
--- /dev/null
+++ b/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java
@@ -0,0 +1,18 @@
+package com.muyu.auth;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+
+/**
+ * @author DongZl
+ * @description: 鉴权中心启动类
+ */
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+@EnableDiscoveryClient
+public class AuthApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(AuthApplication.class);
+ }
+}
diff --git a/muyu-auth/src/main/resources/bootstrap.yml b/muyu-auth/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..0df5467
--- /dev/null
+++ b/muyu-auth/src/main/resources/bootstrap.yml
@@ -0,0 +1,29 @@
+# Tomcat
+server:
+ port: 8265
+# Spring
+spring:
+ application:
+ # 应用名称
+ name: muyu-auth
+ profiles:
+ # 环境配置
+ active: dev
+ main:
+ # 允许使用循环引用
+ allow-circular-references: true
+ # 允许定义相同的bean对象 去覆盖原有的
+ allow-bean-definition-overriding: true
+ cloud:
+ nacos:
+ discovery:
+ # 服务注册地址
+ server-addr: 39.100.132.0:8848
+ config:
+ # 配置中心地址
+ server-addr: 39.100.132.0:8848
+ # 配置文件格式
+ file-extension: yml
+ # 共享配置
+ shared-configs:
+ - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
diff --git a/muyu-common/pom.xml b/muyu-common/pom.xml
new file mode 100644
index 0000000..07d68ce
--- /dev/null
+++ b/muyu-common/pom.xml
@@ -0,0 +1,110 @@
+
+
+ 4.0.0
+
+ com.muyu
+ cloud-demo
+ 1.0.0
+
+
+ muyu-common
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-bootstrap
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-config
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-sentinel
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-loadbalancer
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+
+ io.jsonwebtoken
+ jjwt
+ 0.9.1
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.80
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.2.8
+
+
+
+ mysql
+ mysql-connector-java
+ 8.0.33
+
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 2.2.2
+
+
+
+ com.github.pagehelper
+ pagehelper-spring-boot-starter
+ 1.4.1
+
+
+
+ org.springframework.boot
+ spring-boot-starter-validation
+
+
+
+ org.apache.commons
+ commons-lang3
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
diff --git a/muyu-common/src/main/java/com/muyu/common/constant/Constants.java b/muyu-common/src/main/java/com/muyu/common/constant/Constants.java
new file mode 100644
index 0000000..c88c620
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/constant/Constants.java
@@ -0,0 +1,18 @@
+package com.muyu.common.constant;
+
+/**
+ * @description: 系统常量
+ * @author DongZl
+ */
+public class Constants {
+ /**
+ * 成功标记
+ */
+ public static final Integer SUCCESS = 200;
+ public static final String SUCCESS_MSG = "操作成功";
+ /**
+ * 失败标记
+ */
+ public static final Integer ERROR = 500;
+ public static final String ERROR_MSG = "操作异常";
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/constant/JwtConstants.java b/muyu-common/src/main/java/com/muyu/common/constant/JwtConstants.java
new file mode 100644
index 0000000..28226ca
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/constant/JwtConstants.java
@@ -0,0 +1,27 @@
+package com.muyu.common.constant;
+
+/**
+ * @author DongZl
+ * @description: Jwt常量
+ */
+public class JwtConstants {
+ /**
+ * 用户ID字段
+ */
+ public static final String DETAILS_USER_ID = "user_id";
+
+ /**
+ * 用户名字段
+ */
+ public static final String DETAILS_USERNAME = "username";
+
+ /**
+ * 用户标识
+ */
+ public static final String USER_KEY = "user_key";
+
+ /**
+ * 令牌秘钥
+ */
+ public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/constant/TokenConstants.java b/muyu-common/src/main/java/com/muyu/common/constant/TokenConstants.java
new file mode 100644
index 0000000..3ab1a45
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/constant/TokenConstants.java
@@ -0,0 +1,24 @@
+package com.muyu.common.constant;
+
+/**
+ * @author DongZl
+ * @description: 令牌常量
+ */
+public class TokenConstants {
+ /**
+ * 缓存有效期,默认720(分钟)
+ */
+ public final static long EXPIRATION = 720;
+ /**
+ * 缓存刷新时间,默认120(分钟)
+ */
+ public final static long REFRESH_TIME = 120;
+ /**
+ * 权限缓存前缀
+ */
+ public final static String LOGIN_TOKEN_KEY = "login_tokens:";
+ /**
+ * token标识
+ */
+ public static final String TOKEN = "token";
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/result/PageResult.java b/muyu-common/src/main/java/com/muyu/common/result/PageResult.java
new file mode 100644
index 0000000..b2cf555
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/result/PageResult.java
@@ -0,0 +1,34 @@
+package com.muyu.common.result;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author DongZl
+ * @description: 列表返回结果集
+ */
+@Data
+public class PageResult implements Serializable {
+ /**
+ * 总条数
+ */
+ private long total;
+ /**
+ * 结果集合
+ */
+ private List list;
+ public PageResult() {
+ }
+ public PageResult(long total, List list) {
+ this.total = total;
+ this.list = list;
+ }
+ public static PageResult toPageResult(long total, List list){
+ return new PageResult(total , list);
+ }
+ public static Result> toResult(long total, List list){
+ return Result.success(PageResult.toPageResult(total,list));
+ }
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/result/Result.java b/muyu-common/src/main/java/com/muyu/common/result/Result.java
new file mode 100644
index 0000000..1076493
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/result/Result.java
@@ -0,0 +1,53 @@
+package com.muyu.common.result;
+
+import com.muyu.common.constant.Constants;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @description: 响应信息主体
+ * @author DongZl
+ */
+@Data
+public class Result implements Serializable {
+ private static final long serialVersionUID = 1L;
+ /** 成功 */
+ public static final int SUCCESS = Constants.SUCCESS;
+ /** 失败 */
+ public static final int FAIL = Constants.ERROR;
+ private int code;
+ private String msg;
+ private T data;
+ public static Result success() {
+ return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
+ }
+ public static Result success(T data) {
+ return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
+ }
+ public static Result success(T data, String msg) {
+ return restResult(data, SUCCESS, msg);
+ }
+ public static Result error() {
+ return restResult(null, FAIL, Constants.ERROR_MSG);
+ }
+ public static Result error(String msg) {
+ return restResult(null, FAIL, msg);
+ }
+ public static Result error(T data) {
+ return restResult(data, FAIL, Constants.ERROR_MSG);
+ }
+ public static Result error(T data, String msg) {
+ return restResult(data, FAIL, msg);
+ }
+ public static Result error(int code, String msg) {
+ return restResult(null, code, msg);
+ }
+ private static Result restResult(T data, int code, String msg) {
+ Result apiResult = new Result<>();
+ apiResult.setCode(code);
+ apiResult.setData(data);
+ apiResult.setMsg(msg);
+ return apiResult;
+ }
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/utils/JwtUtils.java b/muyu-common/src/main/java/com/muyu/common/utils/JwtUtils.java
new file mode 100644
index 0000000..b91af0e
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/utils/JwtUtils.java
@@ -0,0 +1,104 @@
+package com.muyu.common.utils;
+
+import com.muyu.common.constant.JwtConstants;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+
+import java.util.Map;
+
+/**
+ * @description: Jwt工具类
+ * @author DongZl
+ */
+public class JwtUtils {
+
+ public static String secret = JwtConstants.SECRET;
+ /**
+ * 从数据声明生成令牌
+ *
+ * @param claims 数据声明
+ * @return 令牌
+ */
+ public static String createToken(Map claims){
+ String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
+ return token;
+ }
+ /**
+ * 从令牌中获取数据声明
+ *
+ * @param token 令牌
+ * @return 数据声明
+ */
+ public static Claims parseToken(String token){
+ return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
+ }
+ /**
+ * 根据令牌获取用户标识
+ *
+ * @param token 令牌
+ * @return 用户ID
+ */
+ public static String getUserKey(String token){
+ Claims claims = parseToken(token);
+ return getValue(claims, JwtConstants.USER_KEY);
+ }
+ /**
+ * 根据令牌获取用户标识
+ *
+ * @param claims 身份信息
+ * @return 用户ID
+ */
+ public static String getUserKey(Claims claims){
+ return getValue(claims, JwtConstants.USER_KEY);
+ }
+ /**
+ * 根据令牌获取用户ID
+ *
+ * @param token 令牌
+ * @return 用户ID
+ */
+ public static String getUserId(String token){
+ Claims claims = parseToken(token);
+ return getValue(claims, JwtConstants.DETAILS_USER_ID);
+ }
+ /**
+ * 根据身份信息获取用户ID
+ *
+ * @param claims 身份信息
+ * @return 用户ID
+ */
+ public static String getUserId(Claims claims){
+ return getValue(claims, JwtConstants.DETAILS_USER_ID);
+ }
+ /**
+ * 根据令牌获取用户名
+ *
+ * @param token 令牌
+ * @return 用户名
+ */
+ public static String getUserName(String token){
+ Claims claims = parseToken(token);
+ return getValue(claims, JwtConstants.DETAILS_USERNAME);
+ }
+ /**
+ * 根据身份信息获取用户名
+ *
+ * @param claims 身份信息
+ * @return 用户名
+ */
+ public static String getUserName(Claims claims){
+ return getValue(claims, JwtConstants.DETAILS_USERNAME);
+ }
+ /**
+ * 根据身份信息获取键值
+ *
+ * @param claims 身份信息
+ * @param key 键
+ * @return 值
+ */
+ public static String getValue(Claims claims, String key){
+ Object obj = claims.get(key);
+ return obj == null ? "" : obj.toString();
+ }
+}
diff --git a/muyu-common/src/main/java/com/muyu/common/utils/StringUtils.java b/muyu-common/src/main/java/com/muyu/common/utils/StringUtils.java
new file mode 100644
index 0000000..c806d19
--- /dev/null
+++ b/muyu-common/src/main/java/com/muyu/common/utils/StringUtils.java
@@ -0,0 +1,68 @@
+package com.muyu.common.utils;
+
+import org.springframework.util.AntPathMatcher;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author DongZl
+ * @description: 字符串处理工具类
+ */
+public class StringUtils extends org.apache.commons.lang3.StringUtils {
+
+ /**
+ * * 判断一个对象是否为空
+ *
+ * @param object Object
+ * @return true:为空 false:非空
+ */
+ public static boolean isNull(Object object) {
+ return object == null;
+ }
+
+ /**
+ * * 判断一个Collection是否为空, 包含List,Set,Queue
+ *
+ * @param coll 要判断的Collection
+ * @return true:为空 false:非空
+ */
+ public static boolean isEmpty(Collection> coll) {
+ return isNull(coll) || coll.isEmpty();
+ }
+
+ /**
+ * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
+ *
+ * @param str 指定字符串
+ * @param strs 需要检查的字符串数组
+ * @return 是否匹配
+ */
+ public static boolean matches(String str, List strs) {
+ if (isEmpty(str) || isEmpty(strs)) {
+ return false;
+ }
+ for (String pattern : strs) {
+ if (isMatch(pattern, str))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * 判断url是否与规则配置:
+ * ? 表示单个字符;
+ * * 表示一层路径内的任意字符串,不可跨层级;
+ * ** 表示任意层路径;
+ *
+ * @param pattern 匹配规则
+ * @param url 需要匹配的url
+ * @return
+ */
+ public static boolean isMatch(String pattern, String url) {
+ AntPathMatcher matcher = new AntPathMatcher();
+ return matcher.match(pattern, url);
+ }
+}
diff --git a/muyu-gateway/pom.xml b/muyu-gateway/pom.xml
new file mode 100644
index 0000000..edef58f
--- /dev/null
+++ b/muyu-gateway/pom.xml
@@ -0,0 +1,60 @@
+
+
+ 4.0.0
+
+ com.muyu
+ cloud-demo
+ 1.0.0
+
+
+ muyu-gateway
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ com.muyu
+ muyu-common
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-gateway
+
+
+
+ com.alibaba.cloud
+ spring-cloud-alibaba-sentinel-gateway
+
+
+
+ com.alibaba.csp
+ sentinel-spring-cloud-gateway-adapter
+
+
+
+
+ ${project.artifactId}
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/muyu-gateway/src/main/java/com/muyu/GatewayApplication.java b/muyu-gateway/src/main/java/com/muyu/GatewayApplication.java
new file mode 100644
index 0000000..4a0f9fd
--- /dev/null
+++ b/muyu-gateway/src/main/java/com/muyu/GatewayApplication.java
@@ -0,0 +1,17 @@
+package com.muyu;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+/**
+ * @author DongZl
+ * @description: 服务网关启动程序
+ * 排除数据源自动配置
+ */
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+public class GatewayApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(GatewayApplication.class, args);
+ }
+}
diff --git a/muyu-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java b/muyu-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java
new file mode 100644
index 0000000..79176f2
--- /dev/null
+++ b/muyu-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java
@@ -0,0 +1,71 @@
+package com.muyu.config;
+
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
+import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
+import org.springframework.beans.factory.ObjectProvider;
+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 javax.annotation.PostConstruct;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @deprecation: 网关限流控件
+ * @author DongZl
+ */
+@Configuration
+public class GatewaySentinelConfig {
+ /**
+ * 查看解析器
+ */
+ private final List viewResolvers;
+ /**
+ * 服务器编解码器配置
+ */
+ private final ServerCodecConfigurer serverCodecConfigurer;
+ public GatewaySentinelConfig(ObjectProvider> viewResolversProvider,
+ ServerCodecConfigurer serverCodecConfigurer) {
+ this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
+ this.serverCodecConfigurer = serverCodecConfigurer;
+ }
+ /**
+ * Sentinel 网关块异常处理程序
+ * @return
+ */
+ @Bean
+ @Order(Ordered.HIGHEST_PRECEDENCE)
+ public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
+ // 给 Spring Cloud Gateway 注册块异常处理程序。
+ return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
+ }
+
+ /**
+ * 初始化网关配置
+ */
+ @PostConstruct
+ public void doInit() {
+ initGatewayRules();
+ }
+ /**
+ * 配置限流规则
+ */
+ private void initGatewayRules() {
+ Set rules = new HashSet<>();
+ rules.add(new GatewayFlowRule("cloud-user")
+ // 限流阈值
+ .setCount(1)
+ // 统计时间窗口,单位是秒,默认是 1 秒
+ .setIntervalSec(5)
+ );
+ //添加到限流规则当中
+ GatewayRuleManager.loadRules(rules);
+ }
+}
diff --git a/muyu-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java b/muyu-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java
new file mode 100644
index 0000000..216d521
--- /dev/null
+++ b/muyu-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java
@@ -0,0 +1,31 @@
+package com.muyu.config;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @description: 放行白名单配置
+ * @author DongZl
+ */
+@Configuration
+@RefreshScope
+@ConfigurationProperties(prefix = "ignore")
+@Data
+@Log4j2
+public class IgnoreWhiteConfig {
+ /**
+ * 放行白名单配置,网关不校验此处的白名单
+ */
+ private List whites = new ArrayList<>();
+ public void setWhites(List whites) {
+ log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
+ this.whites = whites;
+ }
+}
diff --git a/muyu-gateway/src/main/java/com/muyu/filter/AuthFilter.java b/muyu-gateway/src/main/java/com/muyu/filter/AuthFilter.java
new file mode 100644
index 0000000..cd684b0
--- /dev/null
+++ b/muyu-gateway/src/main/java/com/muyu/filter/AuthFilter.java
@@ -0,0 +1,84 @@
+package com.muyu.filter;
+
+import com.muyu.common.constant.JwtConstants;
+import com.muyu.common.constant.TokenConstants;
+import com.muyu.common.utils.StringUtils;
+import com.muyu.common.utils.JwtUtils;
+import com.muyu.config.IgnoreWhiteConfig;
+import com.muyu.utils.GatewayUtils;
+import io.jsonwebtoken.Claims;
+import lombok.extern.log4j.Log4j2;
+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.StringRedisTemplate;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.stereotype.Component;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * @description: 鉴权过滤器
+ * @author DongZl
+ */
+@Component
+@Log4j2
+public class AuthFilter implements GlobalFilter, Ordered {
+ /**
+ * redis操作
+ */
+ private final StringRedisTemplate redisTemplate;
+ /**
+ * 白名单
+ */
+ private final IgnoreWhiteConfig ignoreWhite;
+ public AuthFilter(StringRedisTemplate redisTemplate, IgnoreWhiteConfig ignoreWhite) {
+ this.redisTemplate = redisTemplate;
+ this.ignoreWhite = ignoreWhite;
+ }
+ @Override
+ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+ // 请求作用域
+ ServerHttpRequest request = exchange.getRequest();
+ // 请求头
+ HttpHeaders headers = request.getHeaders();
+ // 请求方式
+ HttpMethod method = request.getMethod();
+ // header操作对象
+ ServerHttpRequest.Builder mutate = request.mutate();
+ String uri = request.getURI().getPath();
+ log.info("请求日志:uri:[{}] , 请求方式:[{}]", uri, method);
+ // 跳过不需要验证的路径
+ if (StringUtils.matches(uri, ignoreWhite.getWhites())) {
+ return chain.filter(exchange);
+ }
+ String token = headers.getFirst(TokenConstants.TOKEN);
+ if (StringUtils.isEmpty(token)) {
+ return GatewayUtils.errorResponse(exchange, "令牌不能为空");
+ }
+ Claims claims = JwtUtils.parseToken(token);
+ if (claims == null) {
+ return GatewayUtils.errorResponse(exchange, "令牌已过期或验证不正确!");
+ }
+ String userKey = JwtUtils.getUserKey(claims);
+ boolean isLogin = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey);
+ if (!isLogin) {
+ return GatewayUtils.errorResponse(exchange, "登录状态已过期");
+ }
+ String userid = JwtUtils.getUserId(claims);
+ String username = JwtUtils.getUserName(claims);
+ // 设置用户信息到请求
+ GatewayUtils.addHeader(mutate, JwtConstants.USER_KEY, userKey);
+ GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USER_ID, userid);
+ GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USERNAME, username);
+ // 内部请求来源参数清除
+ GatewayUtils.removeHeader(mutate, TokenConstants.TOKEN);
+ return chain.filter(exchange.mutate().request(mutate.build()).build());
+ }
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+}
diff --git a/muyu-gateway/src/main/java/com/muyu/utils/GatewayUtils.java b/muyu-gateway/src/main/java/com/muyu/utils/GatewayUtils.java
new file mode 100644
index 0000000..dcbe7d8
--- /dev/null
+++ b/muyu-gateway/src/main/java/com/muyu/utils/GatewayUtils.java
@@ -0,0 +1,73 @@
+package com.muyu.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.muyu.common.result.Result;
+import com.muyu.common.utils.StringUtils;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.http.server.reactive.ServerHttpResponse;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * @author DongZl
+ * @description: 网关处理工具类
+ */
+@Log4j2
+public class GatewayUtils {
+ /**
+ * 添加请求头参数
+ * @param mutate 修改对象
+ * @param key 键
+ * @param value 值
+ */
+ public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) {
+ if (StringUtils.isEmpty(key)){
+ log.warn("添加请求头参数键不可以为空");
+ return;
+ }
+ if (value == null) {
+ log.warn("添加请求头参数:[{}]值为空",key);
+ return;
+ }
+ String valueStr = value.toString();
+ mutate.header(key, valueStr);
+ log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value);
+ }
+ /**
+ * 删除请求头参数
+ * @param mutate 修改对象
+ * @param key 键
+ */
+ public static void removeHeader(ServerHttpRequest.Builder mutate, String key) {
+ if (StringUtils.isEmpty(key)){
+ log.warn("删除请求头参数键不可以为空");
+ return;
+ }
+ mutate.headers(httpHeaders -> httpHeaders.remove(key)).build();
+ log.info("删除请求头参数 - 键:[{}]",key);
+ }
+ /**
+ * 错误结果响应
+ * @param exchange 响应上下文
+ * @param msg 响应消息
+ * @return
+ */
+ public static Mono errorResponse(ServerWebExchange exchange, String msg) {
+ ServerHttpResponse response = exchange.getResponse();
+ //设置HTTP响应头状态
+ response.setStatusCode(HttpStatus.OK);
+ //设置HTTP响应头文本格式
+ response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
+ //定义响应内容
+ Result> result = Result.error(msg);
+ String resultJson = JSONObject.toJSONString(result);
+ log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
+ DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
+ //进行响应
+ return response.writeWith(Mono.just(dataBuffer));
+ }
+ }
diff --git a/muyu-gateway/src/main/resources/bootstrap.yml b/muyu-gateway/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..385bdce
--- /dev/null
+++ b/muyu-gateway/src/main/resources/bootstrap.yml
@@ -0,0 +1,29 @@
+# Tomcat
+server:
+ port: 8080
+# Spring
+spring:
+ application:
+ # 应用名称
+ name: muyu-gateway
+ profiles:
+ # 环境配置
+ active: dev
+ main:
+ # 允许使用循环引用
+ allow-circular-references: true
+ # 允许定义相同的bean对象 去覆盖原有的
+ allow-bean-definition-overriding: true
+ cloud:
+ nacos:
+ discovery:
+ # 服务注册地址
+ server-addr: 39.100.132.0:8848
+ config:
+ # 配置中心地址
+ server-addr: 39.100.132.0:8848
+ # 配置文件格式
+ file-extension: yml
+ # 共享配置
+ shared-configs:
+ - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
diff --git a/muyu-modules/muyu-system/pom.xml b/muyu-modules/muyu-system/pom.xml
new file mode 100644
index 0000000..08a6665
--- /dev/null
+++ b/muyu-modules/muyu-system/pom.xml
@@ -0,0 +1,33 @@
+
+
+ 4.0.0
+
+ com.muyu
+ muyu-modules
+ 1.0.0
+
+
+ muyu-system
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ com.muyu
+ muyu-common
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
diff --git a/muyu-modules/muyu-system/src/main/java/com/muyu/system/SystemApplication.java b/muyu-modules/muyu-system/src/main/java/com/muyu/system/SystemApplication.java
new file mode 100644
index 0000000..cb4f21d
--- /dev/null
+++ b/muyu-modules/muyu-system/src/main/java/com/muyu/system/SystemApplication.java
@@ -0,0 +1,19 @@
+package com.muyu.system;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+
+/**
+ * @author DongZl
+ * @description: 系统服务启动类
+ */
+@SpringBootApplication
+@EnableFeignClients( basePackages = {"com.muyu.**"})
+@EnableDiscoveryClient
+public class SystemApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(SystemApplication.class);
+ }
+}
diff --git a/muyu-modules/muyu-system/src/main/resources/bootstrap.yml b/muyu-modules/muyu-system/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..e7017e2
--- /dev/null
+++ b/muyu-modules/muyu-system/src/main/resources/bootstrap.yml
@@ -0,0 +1,29 @@
+# Tomcat
+server:
+ port: 9201
+# Spring
+spring:
+ main:
+ allow-circular-references: true
+ jackson:
+ date-format: yyyy-MM-dd HH:mm:ss
+ time-zone: GMT+8
+ application:
+ # 应用名称
+ name: muyu-system
+ profiles:
+ # 环境配置
+ active: dev
+ cloud:
+ nacos:
+ discovery:
+ # 服务注册地址
+ server-addr: 39.100.132.0:8848
+ config:
+ # 配置中心地址
+ server-addr: 39.100.132.0:8848
+ # 配置文件格式
+ file-extension: yml
+ # 共享配置
+ shared-configs:
+ - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
diff --git a/muyu-modules/pom.xml b/muyu-modules/pom.xml
new file mode 100644
index 0000000..feaf37b
--- /dev/null
+++ b/muyu-modules/pom.xml
@@ -0,0 +1,24 @@
+
+
+ 4.0.0
+
+ com.muyu
+ cloud-demo
+ 1.0.0
+
+
+ muyu-modules
+ pom
+
+ muyu-system
+
+
+
+ 17
+ 17
+ UTF-8
+
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..b4b0a46
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,67 @@
+
+
+ 4.0.0
+
+ com.muyu
+ cloud-demo
+ 1.0.0
+ pom
+
+
+ muyu-common
+ muyu-gateway
+ muyu-auth
+ muyu-modules
+
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ spring-boot-starter-parent
+ org.springframework.boot
+ 2.7.15
+
+
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ 2021.0.8
+ pom
+ import
+
+
+
+ com.alibaba.cloud
+ spring-cloud-alibaba-dependencies
+ 2021.0.5.0
+ pom
+ import
+
+
+
+ com.alibaba.nacos
+ nacos-client
+ 2.0.4
+
+
+
+ com.muyu
+ muyu-common
+ 1.0.0
+
+
+
+
+