master
zhouzhou 2023-12-10 14:58:21 +08:00
parent 23fcac41aa
commit 7721ee369c
11 changed files with 337 additions and 2 deletions

View File

@ -3,6 +3,7 @@
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/bwie-common/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/bwie-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/bwie-common/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/bwie-common/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/bwie-gateway/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/bwie-modules/bwie-blood/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/bwie-modules/bwie-blood/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/bwie-modules/bwie-blood/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/bwie-modules/bwie-blood/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/bwie-modules/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/bwie-modules/src/main/java" charset="UTF-8" />

View File

@ -22,4 +22,5 @@ public class HealthEducation {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date publishTime; private Date publishTime;
} }

38
bwie-gateway/.gitignore vendored 100644
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### 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

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bw</groupId>
<artifactId>day02</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>bwie-gateway</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 公共模块 -->
<dependency>
<groupId>com.bw</groupId>
<artifactId>bwie-common</artifactId>
</dependency>
<!-- 网关依赖 -->
<!-- SpringCloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel Gateway -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<!-- 引入阿里巴巴sentinel限流 依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package com.bwie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: zhx
* @Package: com.bwie
* @Datetime: 2023/12/10 14:43
* @Filename: GateWayApplication
*/
@SpringBootApplication
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class);
}
}

View File

@ -0,0 +1,32 @@
package com.bwie.gateway.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<String> whites = new ArrayList<>();
public void setWhites(List<String> whites) {
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
this.whites = whites;
}
}

View File

@ -0,0 +1,74 @@
package com.bwie.gateway.filters;
import com.bwie.common.constants.JwtConstants;
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 io.jsonwebtoken.Claims;
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.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class AuthFilter implements GlobalFilter, Ordered {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private IgnoreWhiteConfig ignoreWhiteConfig;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//获取白名单
List<String> whites = ignoreWhiteConfig.getWhites();
//路径
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
System.out.println("路径>>>>>>"+path);
//放行
if (StringUtils.matches(path,whites)){
return chain.filter(exchange);
}
//
// //获取token
// String token = request.getHeaders().getFirst("token");
// if(token==null){
// return GatewayUtils.errorResponse(exchange,"token不存在");
// }
//
// //token合法性
// Claims claims = null;
// try {
// claims = JwtUtils.parseToken(token);
// } catch (Exception e) {
// return GatewayUtils.errorResponse(exchange,"token不合法");
// }
//
// if (!redisTemplate.hasKey(JwtConstants.LOGIN_TOKEN+token)){
// return GatewayUtils.errorResponse(exchange,"token过期");
// }
// redisTemplate.expire(JwtConstants.LOGIN_TOKEN+token,10, TimeUnit.MINUTES);
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}

View File

@ -0,0 +1,98 @@
package com.bwie.gateway.utils;
import com.alibaba.fastjson.JSONObject;
import com.bwie.common.result.Result;
import com.bwie.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<Void> errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) {
ServerHttpResponse response = exchange.getResponse();
//设置HTTP响应头状态
response.setStatusCode(httpStatus);
//设置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));
}
/**
*
* @param exchange
* @param msg
* @return
*/
public static Mono<Void> 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));
}
}

View File

@ -0,0 +1,29 @@
# Tomcat
server:
port: 18080
# Spring
spring:
application:
# 应用名称
name: bwie-gateway
profiles:
# 环境配置
active: dev
main:
# 允许使用循环引用
allow-circular-references: true
# 允许定义相同的bean对象 去覆盖原有的
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 124.223.114.120:8848
config:
# 配置中心地址
server-addr: 124.223.114.120:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

View File

@ -1,6 +1,6 @@
# Tomcat # Tomcat
server: server:
port: 9002 port: 9006
# Spring # Spring
spring: spring:
main: main:
@ -10,7 +10,7 @@ spring:
time-zone: GMT+8 time-zone: GMT+8
application: application:
# 应用名称 # 应用名称
name: bwie-user name: bwie-blood
profiles: profiles:
# 环境配置 # 环境配置
active: dev active: dev

View File

@ -11,6 +11,7 @@
<modules> <modules>
<module>bwie-common</module> <module>bwie-common</module>
<module>bwie-modules</module> <module>bwie-modules</module>
<module>bwie-gateway</module>
</modules> </modules>
<properties> <properties>