|
@@ -10,11 +10,9 @@ public class RequestUtil implements ApiConstant {
|
|
private static final String FIELD_IP = "_IP_";
|
|
private static final String FIELD_IP = "_IP_";
|
|
|
|
|
|
public static String getIpAddress(HttpServletRequest request) {
|
|
public static String getIpAddress(HttpServletRequest request) {
|
|
- Object obj = request.getAttribute(FIELD_IP);
|
|
|
|
- if (obj instanceof String) {
|
|
|
|
- return (String) obj;
|
|
|
|
- } else {
|
|
|
|
- String ip = request.getHeader("X-FORWARDED-FOR");
|
|
|
|
|
|
+ String ip = getAttribute(request, FIELD_IP);
|
|
|
|
+ if (StringUtils.isEmpty(ip)) {
|
|
|
|
+ ip = request.getHeader("X-FORWARDED-FOR");
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) {
|
|
if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) {
|
|
ip = request.getHeader("X-Real-IP");
|
|
ip = request.getHeader("X-Real-IP");
|
|
}
|
|
}
|
|
@@ -36,28 +34,24 @@ public class RequestUtil implements ApiConstant {
|
|
ip = ip.split(",")[0];
|
|
ip = ip.split(",")[0];
|
|
}
|
|
}
|
|
request.setAttribute(FIELD_IP, ip);
|
|
request.setAttribute(FIELD_IP, ip);
|
|
- return ip;
|
|
|
|
}
|
|
}
|
|
|
|
+ return ip;
|
|
}
|
|
}
|
|
|
|
|
|
- public static <T> T getAttribute(HttpServletRequest request, String name) {
|
|
|
|
|
|
+ public static <T> T getAttribute(HttpServletRequest request, String name, T defaultValue) {
|
|
Object obj = request.getAttribute(name);
|
|
Object obj = request.getAttribute(name);
|
|
- try {
|
|
|
|
- return (T) obj;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- return null;
|
|
|
|
|
|
+ T value = null;
|
|
|
|
+ if (obj != null) {
|
|
|
|
+ try {
|
|
|
|
+ value = (T) obj;
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ return value != null ? value : defaultValue;
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getAttribute(HttpServletRequest request, String name, String defaultValue) {
|
|
|
|
- Object obj = request.getAttribute(name);
|
|
|
|
- if (obj instanceof String) {
|
|
|
|
- return (String) obj;
|
|
|
|
- } else if (defaultValue != null) {
|
|
|
|
- return defaultValue;
|
|
|
|
- } else {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
|
|
+ public static <T> T getAttribute(HttpServletRequest request, String name) {
|
|
|
|
+ return getAttribute(request, name, null);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|