Commit 5fe8b35b authored by 张永's avatar 张永

1

parent 34e15bb1
...@@ -98,7 +98,7 @@ public class IDCSystemLogAspect { ...@@ -98,7 +98,7 @@ public class IDCSystemLogAspect {
if (request != null) { if (request != null) {
log.setsUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); log.setsUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setsHttpMethod(request.getMethod()); log.setsHttpMethod(request.getMethod());
log.setsIp(request.getRemoteAddr()); log.setsIp(ShopUtil.getIpAddress(request));
log.setsRequestText(GsonUtil.toJson(ShopUtil.getParameterMap(request))); log.setsRequestText(GsonUtil.toJson(ShopUtil.getParameterMap(request)));
} }
......
...@@ -7,7 +7,13 @@ import java.util.Map.Entry; ...@@ -7,7 +7,13 @@ import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShopUtil { public class ShopUtil {
private static final Logger logger = LoggerFactory.getLogger(ShopUtil.class);
public static Map<String, Object> getParameterMap(HttpServletRequest request) { public static Map<String, Object> getParameterMap(HttpServletRequest request) {
// 参数Map // 参数Map
Map<String, String[]> properties = request.getParameterMap(); Map<String, String[]> properties = request.getParameterMap();
...@@ -37,4 +43,57 @@ public class ShopUtil { ...@@ -37,4 +43,57 @@ public class ShopUtil {
return returnMap; return returnMap;
} }
public static String getIpAddress(HttpServletRequest request) {
//注意本地测试时,浏览器请求不要用localhost,要用本机IP访问项目地址,不然这里取不到ip
// 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
String ip = request.getHeader("X-Forwarded-For");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
}
}
} else if (ip.length() > 15) {
String[] ips = ip.split(",");
for (int index = 0; index < ips.length; index++) {
String strIp = (String) ips[index];
if (!("unknown".equalsIgnoreCase(strIp))) {
ip = strIp;
break;
}
}
}
return ip;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment