|
|
@@ -3,9 +3,10 @@ package com.kxs.gateway.api.filter;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.kxs.common.core.constant.enums.ErrorTypeEnum;
|
|
|
+import com.kxs.common.core.constant.CommonConstants;
|
|
|
import com.kxs.common.mq.handlers.IMQSender;
|
|
|
import com.kxs.gateway.api.config.ResponseProperties;
|
|
|
+import com.kxs.gateway.api.rabbit.RabbitDtsMethodQueueMQ;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
@@ -17,20 +18,16 @@ import org.springframework.core.Ordered;
|
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
+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.http.server.reactive.ServerHttpResponseDecorator;
|
|
|
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 reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
-import java.net.URI;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
@@ -50,22 +47,24 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O
|
|
|
@RequiredArgsConstructor
|
|
|
public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
- private final ResponseProperties properties;
|
|
|
+ private final ResponseProperties properties;
|
|
|
|
|
|
private final IMQSender imqSender;
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
|
String path = request.getURI().getPath();
|
|
|
- if(!properties.getFilterUrl().contains(path)){
|
|
|
- return chain.filter(exchange);
|
|
|
+ if (!properties.getFilterUrl().isEmpty() && !properties.getFilterUrl().contains(path)) {
|
|
|
+ return chain.filter(exchange);
|
|
|
}
|
|
|
ServerHttpResponse originalResponse = exchange.getResponse();
|
|
|
|
|
|
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
|
|
|
|
|
|
+
|
|
|
ServerHttpResponseDecorator response = new ServerHttpResponseDecorator(originalResponse) {
|
|
|
@NotNull
|
|
|
@Override
|
|
|
@@ -98,9 +97,17 @@ public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
});
|
|
|
|
|
|
- String responseData = String.join("", list);;
|
|
|
+ String responseData = String.join("", list);
|
|
|
|
|
|
- sendMsg(exchange, responseData);
|
|
|
+ JSONObject parse = JSON.parseObject(responseData);
|
|
|
+ Integer status = parse.getInteger("status");
|
|
|
+ if (Objects.equals(status, CommonConstants.SUCCESS)) {
|
|
|
+ //获取请求token
|
|
|
+ List<String> tokens = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION);
|
|
|
+ assert tokens != null;
|
|
|
+ sendMsg(path, exchange.getAttribute("requestBody"), tokens.get(0));
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
byte[] uppedContent = new String(responseData.getBytes(), StandardCharsets.UTF_8).getBytes();
|
|
|
|
|
|
@@ -133,67 +140,16 @@ public class ResponseFilter implements GlobalFilter, Ordered {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- private void sendMsg(ServerWebExchange exchange, String data){
|
|
|
- JSONObject parse = JSON.parseObject(data);
|
|
|
- Integer status = parse.getInteger("status");
|
|
|
- if(status == 1){
|
|
|
- //获取请求内容
|
|
|
- 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);
|
|
|
- //发送消息
|
|
|
-// RabbitDtsMethodQueueMQ msg = RabbitDtsMethodQueueMQ.build(RabbitDtsMethodQueueMQ.MsgEntity.builder()
|
|
|
-// .url(exchange.getRequest().getURI().getPath())
|
|
|
-// .param(param)
|
|
|
-// .build());
|
|
|
-// imqSender.send(msg);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private String getRequestParam(ServerWebExchange exchange) {
|
|
|
+ private void sendMsg(String url, String param, String token) {
|
|
|
|
|
|
- try {
|
|
|
- MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
|
- return JSON.toJSONString(queryParams);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(ErrorTypeEnum.DECRYPT_ERROR.getDescription(), e);
|
|
|
- }
|
|
|
- return null;
|
|
|
+ //发送消息
|
|
|
+ RabbitDtsMethodQueueMQ msg = RabbitDtsMethodQueueMQ.build(RabbitDtsMethodQueueMQ.MsgEntity.builder()
|
|
|
+ .url(url)
|
|
|
+ .param(param)
|
|
|
+ .build());
|
|
|
+// imqSender.send(msg);
|
|
|
+ log.info("send msg success, msg={}", param);
|
|
|
}
|
|
|
- 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;
|
|
|
- }
|
|
|
|
|
|
}
|