异常处理器获取 IP
This commit is contained in:
+38
-1587
File diff suppressed because it is too large
Load Diff
+16
-9
@@ -5,6 +5,7 @@ import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fangjia.core.util.DingDingMessageUtil;
|
||||
import org.slf4j.Logger;
|
||||
@@ -19,23 +20,29 @@ public class FangjiaOpenExceptionHandler implements ExceptionHandler {
|
||||
if (LOGGER.isErrorEnabled()) {
|
||||
LOGGER.error("", e);
|
||||
}
|
||||
String ip = getServerIp();
|
||||
ActionContext context = ActionContext.getContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
|
||||
String ip = getVisitorIp(request);
|
||||
String token = "2307d2fec8314b3e8b4b83dd1d42da2e2051638e3e668f21e77ca01258bf682c";
|
||||
DingDingMessageUtil.sendTextMessage(token, "[OpenApi] "+ip+"服务器内部错误,请及时关注!");
|
||||
ActionContext context = ActionContext.getContext();
|
||||
context.getResponse().setStatus(500);
|
||||
context.getResponse().setCharacterEncoding("UTF-8");
|
||||
context.getResponse().setContentType("application/json;charset=UTF-8");
|
||||
context.getResponse().getWriter().println("{\"code\":500, \"msg\":\"服务器内部错误\"}");
|
||||
}
|
||||
|
||||
private String getServerIp() {
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getLocalHost();
|
||||
return inetAddress.getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
LOGGER.error("Unable to get server IP address", e);
|
||||
return "Unknown IP";
|
||||
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");
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
return ip.split(",")[0]; // 处理多个 IP 的情况
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user