|
|
|
@ -30,13 +30,10 @@ import reactor.core.publisher.Mono; |
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.net.InetAddress; |
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.function.Consumer; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -49,11 +46,7 @@ public class ServerWebExchangeUtil { |
|
|
|
|
private static final String UNKNOWN_PATH = "/sop/unknown"; |
|
|
|
|
private static final String REST_PATH = "/rest"; |
|
|
|
|
|
|
|
|
|
private static final String IP_UNKNOWN = "unknown"; |
|
|
|
|
private static final String IP_LOCAL = "127.0.0.1"; |
|
|
|
|
private static final int IP_LEN = 15; |
|
|
|
|
|
|
|
|
|
private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); |
|
|
|
|
private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); |
|
|
|
|
|
|
|
|
|
private static final List<HttpMessageReader<?>> messageReaders; |
|
|
|
|
|
|
|
|
@ -119,12 +112,10 @@ public class ServerWebExchangeUtil { |
|
|
|
|
|
|
|
|
|
public static ApiParam getApiParamByQuery(ServerWebExchange exchange, String query) { |
|
|
|
|
ApiParam apiParam = new ApiParam(); |
|
|
|
|
String ip = getIP(exchange.getRequest()); |
|
|
|
|
String ip = RequestUtil.getIP(exchange.getRequest()); |
|
|
|
|
apiParam.setIp(ip); |
|
|
|
|
Map<String, ?> params = RequestUtil.parseQueryToMap(query); |
|
|
|
|
if (params != null) { |
|
|
|
|
apiParam.putAll(params); |
|
|
|
|
} |
|
|
|
|
apiParam.putAll(params); |
|
|
|
|
setApiParam(exchange, apiParam); |
|
|
|
|
return apiParam; |
|
|
|
|
} |
|
|
|
@ -135,7 +126,7 @@ public class ServerWebExchangeUtil { |
|
|
|
|
contentType = MediaType.APPLICATION_FORM_URLENCODED; |
|
|
|
|
} |
|
|
|
|
ApiParam apiParam = new ApiParam(); |
|
|
|
|
String ip = getIP(exchange.getRequest()); |
|
|
|
|
String ip = RequestUtil.getIP(exchange.getRequest()); |
|
|
|
|
apiParam.setIp(ip); |
|
|
|
|
Map<String, ?> params = null; |
|
|
|
|
String contentTypeStr = contentType.toString().toLowerCase(); |
|
|
|
@ -161,44 +152,6 @@ public class ServerWebExchangeUtil { |
|
|
|
|
return apiParam; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取客户端真实ip |
|
|
|
|
* @param request request |
|
|
|
|
* @return 返回ip |
|
|
|
|
*/ |
|
|
|
|
public static String getIP(ServerHttpRequest request) { |
|
|
|
|
HttpHeaders headers = request.getHeaders(); |
|
|
|
|
String ipAddress = headers.getFirst("x-forwarded-for"); |
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
ipAddress = headers.getFirst("Proxy-Client-IP"); |
|
|
|
|
} |
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
ipAddress = headers.getFirst("WL-Proxy-Client-IP"); |
|
|
|
|
} |
|
|
|
|
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { |
|
|
|
|
ipAddress = Optional.ofNullable(request.getRemoteAddress()) |
|
|
|
|
.map(address -> address.getAddress().getHostAddress()) |
|
|
|
|
.orElse(""); |
|
|
|
|
if (IP_LOCAL.equals(ipAddress)) { |
|
|
|
|
// 根据网卡取本机配置的IP
|
|
|
|
|
try { |
|
|
|
|
InetAddress inet = InetAddress.getLocalHost(); |
|
|
|
|
ipAddress = inet.getHostAddress(); |
|
|
|
|
} catch (UnknownHostException e) { |
|
|
|
|
// ignore
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
|
if (ipAddress != null && ipAddress.length() > IP_LEN) { |
|
|
|
|
int index = ipAddress.indexOf(","); |
|
|
|
|
if (index > 0) { |
|
|
|
|
ipAddress = ipAddress.substring(0, index); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ipAddress; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static ApiParam getApiParam(ServerWebExchange exchange, Map<String, String> params) { |
|
|
|
|
ApiParam apiParam = new ApiParam(); |
|
|
|
|