mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
3.2.0
This commit is contained in:
@@ -30,13 +30,10 @@ import reactor.core.publisher.Mono;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,11 +46,7 @@ public class ServerWebExchangeUtil {
|
|||||||
private static final String UNKNOWN_PATH = "/sop/unknown";
|
private static final String UNKNOWN_PATH = "/sop/unknown";
|
||||||
private static final String REST_PATH = "/rest";
|
private static final String REST_PATH = "/rest";
|
||||||
|
|
||||||
private static final String IP_UNKNOWN = "unknown";
|
private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
|
||||||
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 List<HttpMessageReader<?>> messageReaders;
|
private static final List<HttpMessageReader<?>> messageReaders;
|
||||||
|
|
||||||
@@ -119,12 +112,10 @@ public class ServerWebExchangeUtil {
|
|||||||
|
|
||||||
public static ApiParam getApiParamByQuery(ServerWebExchange exchange, String query) {
|
public static ApiParam getApiParamByQuery(ServerWebExchange exchange, String query) {
|
||||||
ApiParam apiParam = new ApiParam();
|
ApiParam apiParam = new ApiParam();
|
||||||
String ip = getIP(exchange.getRequest());
|
String ip = RequestUtil.getIP(exchange.getRequest());
|
||||||
apiParam.setIp(ip);
|
apiParam.setIp(ip);
|
||||||
Map<String, ?> params = RequestUtil.parseQueryToMap(query);
|
Map<String, ?> params = RequestUtil.parseQueryToMap(query);
|
||||||
if (params != null) {
|
apiParam.putAll(params);
|
||||||
apiParam.putAll(params);
|
|
||||||
}
|
|
||||||
setApiParam(exchange, apiParam);
|
setApiParam(exchange, apiParam);
|
||||||
return apiParam;
|
return apiParam;
|
||||||
}
|
}
|
||||||
@@ -135,7 +126,7 @@ public class ServerWebExchangeUtil {
|
|||||||
contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
||||||
}
|
}
|
||||||
ApiParam apiParam = new ApiParam();
|
ApiParam apiParam = new ApiParam();
|
||||||
String ip = getIP(exchange.getRequest());
|
String ip = RequestUtil.getIP(exchange.getRequest());
|
||||||
apiParam.setIp(ip);
|
apiParam.setIp(ip);
|
||||||
Map<String, ?> params = null;
|
Map<String, ?> params = null;
|
||||||
String contentTypeStr = contentType.toString().toLowerCase();
|
String contentTypeStr = contentType.toString().toLowerCase();
|
||||||
@@ -161,44 +152,6 @@ public class ServerWebExchangeUtil {
|
|||||||
return apiParam;
|
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) {
|
public static ApiParam getApiParam(ServerWebExchange exchange, Map<String, String> params) {
|
||||||
ApiParam apiParam = new ApiParam();
|
ApiParam apiParam = new ApiParam();
|
||||||
|
@@ -15,8 +15,10 @@ import org.apache.commons.io.IOUtils;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||||
@@ -36,6 +38,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -46,7 +49,7 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
public class RequestUtil {
|
public class RequestUtil {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(RequestUtil.class);
|
private static final Logger log = LoggerFactory.getLogger(RequestUtil.class);
|
||||||
|
|
||||||
private RequestUtil() {
|
private RequestUtil() {
|
||||||
}
|
}
|
||||||
@@ -197,7 +200,7 @@ public class RequestUtil {
|
|||||||
|
|
||||||
|
|
||||||
public static String getText(HttpServletRequest request) throws IOException {
|
public static String getText(HttpServletRequest request) throws IOException {
|
||||||
return IOUtils.toString(request.getInputStream(), UTF8);
|
return IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -237,6 +240,44 @@ public class RequestUtil {
|
|||||||
return ipAddress;
|
return ipAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取客户端真实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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取上传文件内容
|
* 获取上传文件内容
|
||||||
|
Reference in New Issue
Block a user