|
@@ -2,45 +2,27 @@ package com.ydd.gateway.filter;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
import com.ydd.gateway.config.GatewayConfig;
|
|
|
import com.ydd.gateway.service.FilterService;
|
|
|
import com.ydd.gateway.service.GatewayContext;
|
|
|
import com.ydd.gateway.service.RecorderServerHttpRequestDecorator;
|
|
|
import com.ydd.gateway.util.StringUtils;
|
|
|
-import io.netty.buffer.ByteBufAllocator;
|
|
|
-import org.apache.commons.logging.Log;
|
|
|
-import org.apache.commons.logging.LogFactory;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
|
|
-import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
|
|
-import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
-import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
-import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
-import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.net.URLEncoder;
|
|
|
import java.nio.CharBuffer;
|
|
|
-import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.TreeMap;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
/**
|
|
@@ -48,18 +30,19 @@ import java.util.concurrent.atomic.AtomicReference;
|
|
|
*
|
|
|
*/
|
|
|
@Component
|
|
|
+@Slf4j
|
|
|
public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthorizeGatewayFilterFactory.Config> {
|
|
|
|
|
|
- private static final Log logger = LogFactory.getLog(AuthorizeGatewayFilterFactory.class);
|
|
|
-
|
|
|
-// private static final String AUTHORIZE_TOKEN = "clientId";
|
|
|
+ private static final String SIGN = "sign";
|
|
|
+ private static final String TIME_STAMP_KEY = "timeStamp";
|
|
|
+ private static final String APP_ID = "appId";
|
|
|
|
|
|
@Autowired
|
|
|
FilterService filterService;
|
|
|
|
|
|
public AuthorizeGatewayFilterFactory() {
|
|
|
super(Config.class);
|
|
|
- logger.info("Loaded GatewayFilterFactory [Authorize]");
|
|
|
+ log.info("Loaded GatewayFilterFactory [Authorize]");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -83,7 +66,7 @@ public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<
|
|
|
|
|
|
// url白名单
|
|
|
String url = request.getURI().getPath();
|
|
|
- logger.info(GatewayConfig.NACOS_SERVER_ADDR);
|
|
|
+ log.info(GatewayConfig.NACOS_SERVER_ADDR);
|
|
|
String path = request.getPath().pathWithinApplication().value();
|
|
|
System.out.println(path.substring(path.indexOf("/", 1), path.lastIndexOf("/")));
|
|
|
System.out.println(URL.contains(path.substring(path.indexOf("/", 1), path.lastIndexOf("/"))));
|
|
@@ -93,7 +76,9 @@ public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<
|
|
|
HttpHeaders headers = request.getHeaders();
|
|
|
String method = request.getMethodValue();
|
|
|
MediaType contentType = headers.getContentType();
|
|
|
- if ("POST".equals(method)&&MediaType.APPLICATION_JSON.equals(contentType)){
|
|
|
+ JSONObject requestParams = new JSONObject();
|
|
|
+ Map reqMap = new HashMap<>();
|
|
|
+ if ("POST".equals(method) && MediaType.APPLICATION_JSON.equals(contentType)) {
|
|
|
AtomicReference<String> requestBody = new AtomicReference<>("");
|
|
|
RecorderServerHttpRequestDecorator requestDecorator = new RecorderServerHttpRequestDecorator(request);
|
|
|
Flux<DataBuffer> body = requestDecorator.getBody();
|
|
@@ -102,41 +87,45 @@ public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<
|
|
|
requestBody.set(charBuffer.toString());
|
|
|
});
|
|
|
//获取body参数
|
|
|
- JSONObject requestParams = JSONObject.parseObject(requestBody.get());
|
|
|
+ requestParams = JSONObject.parseObject(requestBody.get());
|
|
|
+ reqMap = JSON.parseObject(requestBody.get(), LinkedHashMap.class);
|
|
|
System.out.println("**********************"+requestParams);
|
|
|
}
|
|
|
if(MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)){
|
|
|
GatewayContext gatewayContext = exchange.getAttribute(GatewayContext.CACHE_GATEWAY_CONTEXT);
|
|
|
System.out.println(gatewayContext.getFormData());
|
|
|
}
|
|
|
- if("GET".equals(method)){
|
|
|
- MultiValueMap<String, String> map = request.getQueryParams();
|
|
|
- System.out.println("**********************"+JSONObject.toJSONString(map));
|
|
|
+ if("GET".equals(method)) {
|
|
|
+ reqMap = request.getQueryParams();
|
|
|
+ System.out.println("**********************"+JSONObject.toJSONString(reqMap));
|
|
|
}
|
|
|
|
|
|
|
|
|
-// String remoteIP = headers.getFirst("x-real-ip");
|
|
|
-// if (!StringUtils.hasText(remoteIP)) {
|
|
|
-// remoteIP = request.getRemoteAddress().getHostName();
|
|
|
-// }
|
|
|
-
|
|
|
- // 请求信息打印
|
|
|
- // logInfo(request, headers);
|
|
|
- TreeMap<String, String> params = new TreeMap<>();
|
|
|
- String signStr = headers.getFirst("sign");
|
|
|
+ String remoteIP = headers.getFirst("x-real-ip");
|
|
|
+ if (!StringUtils.hasText(remoteIP)) {
|
|
|
+ remoteIP = request.getRemoteAddress().getHostName();
|
|
|
+ log.info("请求IP: {}" + remoteIP);
|
|
|
+ }
|
|
|
|
|
|
- String sourceType = headers.getFirst("sourceType");
|
|
|
- String time = headers.getFirst("time");// 日期 yyyymmddHHmiss
|
|
|
- String nonce = headers.getFirst("nonce");// 随机数
|
|
|
- if (StringUtils.hasText(sourceType)) {
|
|
|
- params.put("sourceType", sourceType);
|
|
|
+ TreeMap<String, Object> params = new TreeMap<>(reqMap);
|
|
|
+ if (StringUtils.isNull(params.get(SIGN)) || StringUtils.isBlank(params.get(SIGN).toString())) {
|
|
|
+ String signStr = headers.getFirst(SIGN);
|
|
|
+ params.put(SIGN, signStr);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNull(params.get(APP_ID)) || StringUtils.isBlank(params.get(APP_ID).toString())) {
|
|
|
+ String appId = headers.getFirst(APP_ID);
|
|
|
+ params.put(APP_ID, appId);
|
|
|
}
|
|
|
|
|
|
- params.put("time", time);
|
|
|
- params.put("nonce", nonce);
|
|
|
+ if (StringUtils.isNull(params.get(TIME_STAMP_KEY))) {
|
|
|
+ String timeStamp = headers.getFirst(TIME_STAMP_KEY);
|
|
|
+ params.put(TIME_STAMP_KEY, timeStamp);
|
|
|
+ }
|
|
|
|
|
|
+ Set<String> exclude = new HashSet<>();
|
|
|
+ exclude.add(SIGN);
|
|
|
// 签名校验
|
|
|
- Mono<DataBuffer> mono = filterService.validateSign(params, signStr, response);
|
|
|
+ Mono<DataBuffer> mono = filterService.validateSign(params, exclude, response);
|
|
|
if (mono != null) {
|
|
|
//return response.writeWith(mono);
|
|
|
}
|