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