设置路由

This commit is contained in:
weiyachao 2023-09-22 13:46:50 +08:00
parent 9a08de16eb
commit fe1a976732
5 changed files with 44 additions and 54 deletions

View File

@ -2,8 +2,10 @@ package com.qiuguo.iot.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {

View File

@ -37,30 +37,28 @@ public class AuthFilter implements GlobalFilter, Ordered {
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String url = request.getPath().toString();
System.out.println("url = " + url);
return chain.filter(exchange);
// if (xssProperties.getExcludeUrls().contains(url)) {
// return chain.filter(exchange);
// }
//
// String api_token = exchange.getRequest().getHeaders().getFirst(UserAuthContains.API_TOKEN);
// String api_type = exchange.getRequest().getHeaders().getFirst(UserAuthContains.API_TYPE);
// if (ObjectUtils.isEmpty(api_token) || ObjectUtils.isEmpty(api_type)) {
// return Mono.error(new RuntimeException("未登录"));
// }
// String key = RedisConstans.IOT_TOKEN.concat(api_token);
// return reactiveRedisTemplate.getExpire(key).map(Duration::getSeconds).flatMap(ttl -> {
// if (ttl == -1) {
// // 用户没登陆
// return Mono.error(new RuntimeException("未登录"));
// } else if (ttl <= 3600) {
// // token 将要失效
// return reactiveRedisTemplate.expire(key, Duration.ofDays(7)).then(chain.filter(exchange));
// } else {
// // 正常登录
// return chain.filter(exchange);
// }
// });
if (xssProperties.getExcludeUrls().contains(url)) {
return chain.filter(exchange);
}
String api_token = exchange.getRequest().getHeaders().getFirst(UserAuthContains.API_TOKEN);
String api_type = exchange.getRequest().getHeaders().getFirst(UserAuthContains.API_TYPE);
if (ObjectUtils.isEmpty(api_token) || ObjectUtils.isEmpty(api_type)) {
return Mono.error(new RuntimeException("未登录"));
}
String key = RedisConstans.IOT_TOKEN.concat(api_token);
return reactiveRedisTemplate.getExpire(key).map(Duration::getSeconds).flatMap(ttl -> {
if (ttl == -1) {
// 用户没登陆
return Mono.error(new RuntimeException("未登录"));
} else if (ttl <= 3600) {
// token 将要失效
return reactiveRedisTemplate.expire(key, Duration.ofDays(7)).then(chain.filter(exchange));
} else {
// 正常登录
return chain.filter(exchange);
}
});
}

View File

@ -35,6 +35,8 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
} else if (ex instanceof ResponseStatusException) {
ResponseStatusException responseStatusException = (ResponseStatusException) ex;
msg = responseStatusException.getMessage();
} else if (ex instanceof RuntimeException) {
msg = ex.getMessage();
} else {
msg = "内部服务器错误";
}

View File

@ -5,13 +5,13 @@ spring:
password: 123456
timeout: 5000
cloud:
# config:
# # 如果本地配置优先级高,那么 override-none 设置为 true包括系统环境变量、本地配置文件等配置
# override-none: true
# # 如果想要远程配置优先级高,那么 allow-override 设置为 false如果想要本地配置优先级高那么 allow-override 设置为 true
# allow-override: true
# # 只有系统环境变量或者系统属性才能覆盖远程配置文件的配置,本地配置文件中配置优先级低于远程配置;注意本地配置文件不是系统属性
# override-system-properties: false
config:
# 如果本地配置优先级高,那么 override-none 设置为 true包括系统环境变量、本地配置文件等配置
override-none: true
# 如果想要远程配置优先级高,那么 allow-override 设置为 false如果想要本地配置优先级高那么 allow-override 设置为 true
allow-override: true
# 只有系统环境变量或者系统属性才能覆盖远程配置文件的配置,本地配置文件中配置优先级低于远程配置;注意本地配置文件不是系统属性
override-system-properties: false
nacos:
discovery:
# 服务注册地址
@ -25,12 +25,14 @@ spring:
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
gateway:
discovery:
locator:
# 开启服务发现
enabled: true
# 忽略注册中心服务的大小写
lower-case-service-id: true
routes:
- id: qiuguo-iot-box-user-api
uri: lb://qiuguo-iot-box-user-api
predicates:
- Path=/iot-user/**
filters:
# 转发时去掉一层路径
- StripPrefix=1
globalcors:
corsConfigurations:
'[/**]':
@ -42,22 +44,6 @@ spring:
allowedMethods: "*"
# 允许跨域请求里的head字段设置*为全部
allowedHeaders: "*"
routes:
# # 系统服务
# - id: qiuguo-iot-box-user-api
# # 匹配后路由地址
# uri: lb://qiuguo-iot-box-user-api
# # 断言,路径相匹配的进行路由
# predicates:
# - Path=/qiuguo-iot-box-user-api/**
# filters:
# # 转发时去掉一层路径
# - StripPrefix=1
- id: route_qiuguo-iot-box-user-api
uri: lb://qiuguo-iot-box-user-api
predicates:
- Path=/**
# 安全配置
security:
# 防止XSS攻击
@ -65,7 +51,7 @@ security:
enabled: true
# 排除的路径
exclude-urls:
- /iot-user/user/user/login/pwd
- /iot-user/user/login/pwd
#application:
# cors:
# allowed-crigin-patterns:

View File

@ -4,12 +4,14 @@ import com.tuya.connector.spring.annotations.ConnectorScan;
import org.hswebframework.web.crud.annotation.EnableEasyormRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication(scanBasePackages = {"com.qiuguo.iot.user.api", "com.qiuguo.iot.data.service"})
@EnableEasyormRepository(value = "com.qiuguo.iot.data.entity.*")
@ConnectorScan(basePackages = "com.qiuguo.iot.user.api.service")
@EnableAspectJAutoProxy
@EnableDiscoveryClient
public class IotBoxUserApiApplication {
public static void main(String[] args) {