gateway
This commit is contained in:
38
src/main/java/com/flagnote/gateway/config/GwCorsFilter.java
Normal file
38
src/main/java/com/flagnote/gateway/config/GwCorsFilter.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.flagnote.gateway.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.reactive.CorsWebFilter;
|
||||||
|
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
|
//@Configuration
|
||||||
|
public class GwCorsFilter {
|
||||||
|
|
||||||
|
// @Bean
|
||||||
|
public CorsWebFilter corsFilter() {
|
||||||
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
|
config.addAllowedMethod("*");
|
||||||
|
config.addAllowedOrigin("*");
|
||||||
|
config.addAllowedHeader("*");
|
||||||
|
|
||||||
|
config.setAllowCredentials(true); // 允许cookies跨域
|
||||||
|
config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
|
||||||
|
config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
|
||||||
|
config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
|
||||||
|
config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
|
||||||
|
config.addAllowedMethod("HEAD");
|
||||||
|
config.addAllowedMethod("GET");
|
||||||
|
config.addAllowedMethod("PUT");
|
||||||
|
config.addAllowedMethod("POST");
|
||||||
|
config.addAllowedMethod("DELETE");
|
||||||
|
config.addAllowedMethod("PATCH");
|
||||||
|
|
||||||
|
UrlBasedCorsConfigurationSource source =
|
||||||
|
new UrlBasedCorsConfigurationSource(new PathPatternParser());
|
||||||
|
source.registerCorsConfiguration("/**", config);
|
||||||
|
|
||||||
|
return new CorsWebFilter(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,22 @@
|
|||||||
package com.flagnote.gateway.filter;
|
package com.flagnote.gateway.filter;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
import com.flagnote.gateway.utils.BizKeyUtils;
|
import com.flagnote.gateway.utils.BizKeyUtils;
|
||||||
|
import com.flagnote.gateway.utils.JsonUtils;
|
||||||
|
import com.flagnote.gateway.utils.NoteMeta;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,8 +32,9 @@ public class ValidateNoteKeyFilter implements GatewayFilter, Ordered {
|
|||||||
String noteKey = uriVariables.get("key");
|
String noteKey = uriVariables.get("key");
|
||||||
|
|
||||||
if (!BizKeyUtils.validateKey(noteKey)) {
|
if (!BizKeyUtils.validateKey(noteKey)) {
|
||||||
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
|
NoteMeta meta = new NoteMeta();
|
||||||
return exchange.getResponse().setComplete();
|
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(JsonUtils.toJson(meta).getBytes(StandardCharsets.UTF_8));
|
||||||
|
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
|
|||||||
112
src/main/java/com/flagnote/gateway/utils/JsonUtils.java
Normal file
112
src/main/java/com/flagnote/gateway/utils/JsonUtils.java
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package com.flagnote.gateway.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class JsonUtils {
|
||||||
|
|
||||||
|
private static ObjectMapper om = new ObjectMapper();
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
// 对象的所有字段全部列入,还是其他的选项,可以忽略null等
|
||||||
|
om.setSerializationInclusion(Include.ALWAYS);
|
||||||
|
// 设置Date类型的序列化及反序列化格式
|
||||||
|
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
|
||||||
|
// 忽略空Bean转json的错误
|
||||||
|
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
|
// 忽略未知属性,防止json字符串中存在,java对象中不存在对应属性的情况出现错误
|
||||||
|
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
|
||||||
|
// 注册一个时间序列化及反序列化的处理模块,用于解决jdk8中localDateTime等的序列化问题
|
||||||
|
om.registerModule(new JavaTimeModule());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对象 => json字符串
|
||||||
|
*
|
||||||
|
* @param obj 源对象
|
||||||
|
*/
|
||||||
|
public static <T> String toJson(T obj) {
|
||||||
|
|
||||||
|
String json = null;
|
||||||
|
if (obj != null) {
|
||||||
|
try {
|
||||||
|
json = om.writeValueAsString(obj);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.warn(e.getMessage(), e);
|
||||||
|
throw new IllegalArgumentException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json字符串 => 对象
|
||||||
|
*
|
||||||
|
* @param json 源json串
|
||||||
|
* @param clazz 对象类
|
||||||
|
* @param <T> 泛型
|
||||||
|
*/
|
||||||
|
public static <T> T parse(String json, Class<T> clazz) {
|
||||||
|
|
||||||
|
return parse(json, clazz, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json字符串 => 对象
|
||||||
|
*
|
||||||
|
* @param json 源json串
|
||||||
|
* @param type 对象类型
|
||||||
|
* @param <T> 泛型
|
||||||
|
*/
|
||||||
|
public static <T> T parse(String json, TypeReference<T> type) {
|
||||||
|
|
||||||
|
return parse(json, null, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json => 对象处理方法
|
||||||
|
* <br>
|
||||||
|
* 参数clazz和type必须一个为null,另一个不为null
|
||||||
|
* <br>
|
||||||
|
* 此方法不对外暴露,访问权限为private
|
||||||
|
*
|
||||||
|
* @param json 源json串
|
||||||
|
* @param clazz 对象类
|
||||||
|
* @param type 对象类型
|
||||||
|
* @param <T> 泛型
|
||||||
|
*/
|
||||||
|
private static <T> T parse(String json, Class<T> clazz, TypeReference<T> type) {
|
||||||
|
|
||||||
|
T obj = null;
|
||||||
|
if (StringUtils.hasLength(json)) {
|
||||||
|
try {
|
||||||
|
if (clazz != null) {
|
||||||
|
obj = om.readValue(json, clazz);
|
||||||
|
} else {
|
||||||
|
obj = om.readValue(json, type);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn(e.getMessage(), e);
|
||||||
|
throw new IllegalArgumentException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/main/java/com/flagnote/gateway/utils/NoteMeta.java
Normal file
30
src/main/java/com/flagnote/gateway/utils/NoteMeta.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.flagnote.gateway.utils;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class NoteMeta implements Serializable {/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -8234044213813670440L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
private Integer state;
|
||||||
|
|
||||||
|
private String cipher;
|
||||||
|
|
||||||
|
private Integer lock;
|
||||||
|
|
||||||
|
private String md5;
|
||||||
|
|
||||||
|
private Long serverTime;
|
||||||
|
|
||||||
|
private Long ttl;
|
||||||
|
}
|
||||||
@@ -10,8 +10,8 @@ spring:
|
|||||||
# 最大支持请求大小
|
# 最大支持请求大小
|
||||||
max-request-size: 100MB
|
max-request-size: 100MB
|
||||||
redis:
|
redis:
|
||||||
host: cq.openif.com
|
host: service.flagnote.com
|
||||||
port: 6379
|
port: 36379
|
||||||
timeout: 1000
|
timeout: 1000
|
||||||
poolMaxTotal: 10
|
poolMaxTotal: 10
|
||||||
poolMaxIdle: 10
|
poolMaxIdle: 10
|
||||||
@@ -39,11 +39,11 @@ spring:
|
|||||||
# 指定此客户端的ip
|
# 指定此客户端的ip
|
||||||
default-ip-address: localhost
|
default-ip-address: localhost
|
||||||
gateway:
|
gateway:
|
||||||
globalcors:
|
# globalcors:
|
||||||
corsConfigurations:
|
# corsConfigurations:
|
||||||
'[/**]':
|
# '[/**]':
|
||||||
allowedOrigins: "*"
|
# allowedOrigins: "*"
|
||||||
allowedMethods: "*"
|
# allowedMethods: "*"
|
||||||
routes:
|
routes:
|
||||||
- id: keyMeta
|
- id: keyMeta
|
||||||
uri: lb://keyMetaService
|
uri: lb://keyMetaService
|
||||||
@@ -60,7 +60,7 @@ spring:
|
|||||||
# 每秒产生多少个令牌
|
# 每秒产生多少个令牌
|
||||||
redis-rate-limiter.replenishRate: 1
|
redis-rate-limiter.replenishRate: 1
|
||||||
# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
|
# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
|
||||||
redis-rate-limiter.burstCapacity: 1
|
redis-rate-limiter.burstCapacity: 10
|
||||||
# 每次请求申请几个令牌
|
# 每次请求申请几个令牌
|
||||||
redis-rate-limiter.requestedTokens: 1
|
redis-rate-limiter.requestedTokens: 1
|
||||||
# IP 地址限流
|
# IP 地址限流
|
||||||
@@ -75,9 +75,9 @@ spring:
|
|||||||
# 每秒产生多少个令牌
|
# 每秒产生多少个令牌
|
||||||
redis-rate-limiter.replenishRate: 1
|
redis-rate-limiter.replenishRate: 1
|
||||||
# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
|
# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
|
||||||
redis-rate-limiter.burstCapacity: 3
|
redis-rate-limiter.burstCapacity: 10
|
||||||
# 每次请求申请几个令牌
|
# 每次请求申请几个令牌
|
||||||
redis-rate-limiter.requestedTokens: 3
|
redis-rate-limiter.requestedTokens: 2
|
||||||
# IP 地址限流
|
# IP 地址限流
|
||||||
key-resolver: "#{@remoteSessionKeyResolver}"
|
key-resolver: "#{@remoteSessionKeyResolver}"
|
||||||
|
|
||||||
@@ -106,11 +106,7 @@ spring:
|
|||||||
- Path=/note/{key:[abcdefhikmnopqstuvwxyz23456789]{16}}
|
- Path=/note/{key:[abcdefhikmnopqstuvwxyz23456789]{16}}
|
||||||
- Method=POST
|
- Method=POST
|
||||||
filters:
|
filters:
|
||||||
- name: ValidateNoteKey
|
- ValidateNoteKey
|
||||||
- name: CacheRequestBody
|
|
||||||
args:
|
|
||||||
bodyClass: java.lang.String
|
|
||||||
- name: ValidateNoteCipher
|
|
||||||
- id: deleteNote
|
- id: deleteNote
|
||||||
uri: lb://keyMetaService
|
uri: lb://keyMetaService
|
||||||
order: -1
|
order: -1
|
||||||
|
|||||||
Reference in New Issue
Block a user