|
@@ -3,7 +3,8 @@ package com.kxs.gateway.api.filter;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.kxs.gateway.api.config.GatewayConfigProperties;
|
|
|
|
|
|
|
+import com.kxs.common.core.constant.enums.ErrorTypeEnum;
|
|
|
|
|
+import com.kxs.common.mq.handlers.IMQSender;
|
|
|
import com.kxs.gateway.api.config.ResponseProperties;
|
|
import com.kxs.gateway.api.config.ResponseProperties;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -16,15 +17,20 @@ import org.springframework.core.Ordered;
|
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
|
|
+import org.springframework.web.reactive.function.server.HandlerStrategies;
|
|
|
|
|
+import org.springframework.web.reactive.function.server.ServerRequest;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
+import java.net.URI;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -44,22 +50,23 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
|
public class ResponseFilter implements GlobalFilter, Ordered {
|
|
public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
|
|
- private final ResponseProperties properties;
|
|
|
|
|
|
|
+ private final ResponseProperties properties;
|
|
|
|
|
+
|
|
|
|
|
+ private final IMQSender imqSender;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
|
|
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
|
String path = request.getURI().getPath();
|
|
String path = request.getURI().getPath();
|
|
|
- // 排除不需要的路径
|
|
|
|
|
if(!properties.getFilterUrl().contains(path)){
|
|
if(!properties.getFilterUrl().contains(path)){
|
|
|
- return chain.filter(exchange);
|
|
|
|
|
|
|
+ return chain.filter(exchange);
|
|
|
}
|
|
}
|
|
|
ServerHttpResponse originalResponse = exchange.getResponse();
|
|
ServerHttpResponse originalResponse = exchange.getResponse();
|
|
|
|
|
|
|
|
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
|
|
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
|
|
|
|
|
|
|
|
- new ServerHttpResponseDecorator(originalResponse) {
|
|
|
|
|
|
|
+ ServerHttpResponseDecorator response = new ServerHttpResponseDecorator(originalResponse) {
|
|
|
@NotNull
|
|
@NotNull
|
|
|
@Override
|
|
@Override
|
|
|
public Mono<Void> writeWith(@NotNull Publisher<? extends DataBuffer> body) {
|
|
public Mono<Void> writeWith(@NotNull Publisher<? extends DataBuffer> body) {
|
|
@@ -93,8 +100,7 @@ public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
|
|
String responseData = String.join("", list);;
|
|
String responseData = String.join("", list);;
|
|
|
|
|
|
|
|
- log.info("responseData:" + responseData);
|
|
|
|
|
- sendMsg(request, responseData);
|
|
|
|
|
|
|
+ sendMsg(exchange, responseData);
|
|
|
|
|
|
|
|
byte[] uppedContent = new String(responseData.getBytes(), StandardCharsets.UTF_8).getBytes();
|
|
byte[] uppedContent = new String(responseData.getBytes(), StandardCharsets.UTF_8).getBytes();
|
|
|
|
|
|
|
@@ -117,7 +123,8 @@ public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
};
|
|
};
|
|
|
- return chain.filter(exchange);
|
|
|
|
|
|
|
+ return chain.filter(exchange.mutate().response(response).build());
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -126,16 +133,67 @@ public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void sendMsg(ServerHttpRequest request, String data){
|
|
|
|
|
|
|
+ private void sendMsg(ServerWebExchange exchange, String data){
|
|
|
JSONObject parse = JSON.parseObject(data);
|
|
JSONObject parse = JSON.parseObject(data);
|
|
|
Integer status = parse.getInteger("status");
|
|
Integer status = parse.getInteger("status");
|
|
|
if(status == 1){
|
|
if(status == 1){
|
|
|
- log.info("操作成功,发送消息");
|
|
|
|
|
|
|
+ //获取请求内容
|
|
|
|
|
+ String param = null;
|
|
|
|
|
+ if(exchange.getRequest().getMethod() == HttpMethod.DELETE){
|
|
|
|
|
+ param = getDeleteRequestParam(exchange);
|
|
|
|
|
+ }
|
|
|
|
|
+ if(exchange.getRequest().getMethod() == HttpMethod.GET){
|
|
|
|
|
+ param = getRequestParam(exchange);
|
|
|
|
|
+ }
|
|
|
|
|
+ if(exchange.getRequest().getMethod() == HttpMethod.POST || exchange.getRequest().getMethod() == HttpMethod.PUT){
|
|
|
|
|
+ Mono<String> postBodyParam = getPostBodyParam(exchange);
|
|
|
|
|
+ param = postBodyParam.block();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("操作成功,发送消息:{}", param);
|
|
|
//发送消息
|
|
//发送消息
|
|
|
- String msg = parse.getString("msg");
|
|
|
|
|
|
|
+// RabbitDtsMethodQueueMQ msg = RabbitDtsMethodQueueMQ.build(RabbitDtsMethodQueueMQ.MsgEntity.builder()
|
|
|
|
|
+// .url(exchange.getRequest().getURI().getPath())
|
|
|
|
|
+// .param(param)
|
|
|
|
|
+// .build());
|
|
|
|
|
+// imqSender.send(msg);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getRequestParam(ServerWebExchange exchange) {
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
|
|
|
+ return JSON.toJSONString(queryParams);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(ErrorTypeEnum.DECRYPT_ERROR.getDescription(), e);
|
|
|
}
|
|
}
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ private Mono<String> getPostBodyParam(ServerWebExchange exchange) {
|
|
|
|
|
+ //获取请求体,修改请求体
|
|
|
|
|
+ ServerRequest serverRequest = ServerRequest.create(exchange,
|
|
|
|
|
+ HandlerStrategies.withDefaults().messageReaders());
|
|
|
|
|
+
|
|
|
|
|
+ return serverRequest.bodyToMono(String.class).flatMap(body -> {
|
|
|
|
|
+ log.info("获取请求参数:{}", body);
|
|
|
|
|
+ //添加到请求体
|
|
|
|
|
+ return Mono.just(body);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getDeleteRequestParam(ServerWebExchange exchange) {
|
|
|
|
|
|
|
|
|
|
+ URI uri = exchange.getRequest().getURI();
|
|
|
|
|
+ String path = uri.getPath();
|
|
|
|
|
+ try{
|
|
|
|
|
+ String[] pathSegments = path.split("/");
|
|
|
|
|
+ return pathSegments[pathSegments.length - 1];
|
|
|
|
|
+ }catch (Exception e) {
|
|
|
|
|
+ log.error("解密异常,请求地址:{}", path);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|