异常处理器获取 IP
This commit is contained in:
+27
-36
@@ -1,31 +1,14 @@
|
||||
package com.fangjia.open.controllers;
|
||||
|
||||
import com.fangjia.open.model.BasicModel;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.sothis.web.mvc.ActionContext;
|
||||
import org.sothis.web.mvc.annotation.Param;
|
||||
|
||||
import com.fangjia.dal.common.ServerAreaData;
|
||||
import com.fangjia.dal.mongo.fangjia.model.ServerArea;
|
||||
import com.fangjia.dal.mongo.mobile.model.SearchKeyword;
|
||||
import com.fangjia.open.HttpCode;
|
||||
import com.fangjia.open.OpenHttpCodeEnum;
|
||||
import com.fangjia.open.dto.SearchKeywordResultDto;
|
||||
import com.fangjia.open.interceptors.Authority;
|
||||
import com.fangjia.open.interceptors.Role;
|
||||
import com.fangjia.open.model.RestfulModelAndView;
|
||||
import com.fangjia.service.api.AutoCompleteService;
|
||||
import com.fangjia.service.api.CategoryEnum;
|
||||
import com.fangjia.service.datastore.DistrictService;
|
||||
import com.fangjia.service.datastore.HouseService;
|
||||
import com.fangjia.service.datastore.RentHouseService;
|
||||
import com.fangjia.service.datastore.bean.DistrictQueryBean;
|
||||
import com.fangjia.service.datastore.bean.HouseQueryBean;
|
||||
import com.fangjia.service.datastore.bean.RentHouseQueryBean;
|
||||
import com.fangjia.service.mongo.mobile.SearchKeywordService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
/**
|
||||
* 站内搜索controller
|
||||
*
|
||||
@@ -35,25 +18,33 @@ public class TestController {
|
||||
|
||||
public Object testIpAction(@Param(name = "city") String city, @Param(name = "keyword") String keyword) {
|
||||
RestfulModelAndView mav = new RestfulModelAndView();
|
||||
ActionContext context = ActionContext.getContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
|
||||
String ip = getVisitorIp(request);
|
||||
String ip = getLocalHostIP();
|
||||
mav.setModel(BasicModel.buildOKModel().setResult(ip));
|
||||
return mav;
|
||||
}
|
||||
private String getVisitorIp(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
public static String getLocalHostIP() {
|
||||
/*k8s系统上运行时,使用hostname区分*/
|
||||
if ("true".equalsIgnoreCase(System.getenv("ON_K8S"))) {
|
||||
return System.getenv("HOSTNAME");
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
String result = null;
|
||||
try {
|
||||
Enumeration<?> enumeration = NetworkInterface.getNetworkInterfaces();
|
||||
InetAddress ip;
|
||||
while (enumeration.hasMoreElements()) {
|
||||
NetworkInterface netInterface = (NetworkInterface) enumeration.nextElement();
|
||||
Enumeration<?> addresses = netInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
ip = (InetAddress) addresses.nextElement();
|
||||
if (ip != null && ip instanceof Inet4Address && (ip.getHostAddress().contains("192.168.0") || ip.getHostAddress().contains("192.168.10"))) {
|
||||
result = ip.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
return ip.split(",")[0]; // 处理多个 IP 的情况
|
||||
return result == null ? "192.168.20.6" : result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+26
-13
@@ -12,7 +12,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.sothis.web.mvc.ActionContext;
|
||||
import org.sothis.web.mvc.ExceptionHandler;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
public class FangjiaOpenExceptionHandler implements ExceptionHandler {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FangjiaOpenExceptionHandler.class);
|
||||
|
||||
@@ -21,9 +24,8 @@ public class FangjiaOpenExceptionHandler implements ExceptionHandler {
|
||||
LOGGER.error("", e);
|
||||
}
|
||||
ActionContext context = ActionContext.getContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
|
||||
String ip = getVisitorIp(request);
|
||||
String ip = getLocalHostIP();
|
||||
String token = "2307d2fec8314b3e8b4b83dd1d42da2e2051638e3e668f21e77ca01258bf682c";
|
||||
DingDingMessageUtil.sendTextMessage(token, "[OpenApi] "+ip+"服务器内部错误,请及时关注!");
|
||||
context.getResponse().setStatus(500);
|
||||
@@ -32,17 +34,28 @@ public class FangjiaOpenExceptionHandler implements ExceptionHandler {
|
||||
context.getResponse().getWriter().println("{\"code\":500, \"msg\":\"服务器内部错误\"}");
|
||||
}
|
||||
|
||||
private String getVisitorIp(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
public static String getLocalHostIP() {
|
||||
/*k8s系统上运行时,使用hostname区分*/
|
||||
if ("true".equalsIgnoreCase(System.getenv("ON_K8S"))) {
|
||||
return System.getenv("HOSTNAME");
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
String result = null;
|
||||
try {
|
||||
Enumeration<?> enumeration = NetworkInterface.getNetworkInterfaces();
|
||||
InetAddress ip;
|
||||
while (enumeration.hasMoreElements()) {
|
||||
NetworkInterface netInterface = (NetworkInterface) enumeration.nextElement();
|
||||
Enumeration<?> addresses = netInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
ip = (InetAddress) addresses.nextElement();
|
||||
if (ip != null && ip instanceof Inet4Address && (ip.getHostAddress().contains("192.168.0") || ip.getHostAddress().contains("192.168.10"))) {
|
||||
result = ip.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
return ip.split(",")[0]; // 处理多个 IP 的情况
|
||||
return result == null ? "192.168.20.6" : result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user