1、在web.xml中配置一個listener
<!-- 配置Request的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
2、在特定類中注入即可
@Autowired
private HttpServletRequest request;
3、獲取ip地址
private static final String COMMA = ",";
public static String getRemortIP(HttpServletRequest request) {
String ip = null;
if (request.getHeader(HeaderBuilder.HttpHeaderKey.X_Forwarded_For.getKey()) != null) {
ip = request.getRemoteAddr();
System.out.println("This is ips : " + ip);
if (ip.contains(COMMA)) {
ip = ip.substring(0, ip.indexOf(COMMA));
System.out.println("This is ip : " + ip);
return ip;
}
}
return ip;
}
private static final String COMMA = ",";
public static String getRemortIP(HttpServletRequest request) {
String ip;
if (request.getHeader(HeaderBuilder.HttpHeaderKey.X_Forwarded_For.getKey()) != null) {
ip = request.getRemoteAddr();
String[] test = ip.split(COMMA);
List<String> list = Arrays.asList(test);
return list.get(0);
}
return null;
}
public static void main(String[] args) throws UnknownHostException {
String ip = "10.232.136.189, 172.16.0.30";
//用 getLocalHost() 方法創建的InetAddress的對象
InetAddress address = address = InetAddress.getLocalHost();
//獲取IP地址
String hostAddress = address.getHostAddress();
String[] test = ip.split(COMMA);
List<String> list = Arrays.asList(test);
System.out.println(list.get(0));
}
public static String hostAddress() {
//用 getLocalHost() 方法創建的InetAddress的對象
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
//獲取IP地址
String hostAddress = address.getHostAddress();
return hostAddress;
}
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader(" x-forwarded-for ");
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" Proxy-Client-IP ");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" WL-Proxy-Client-IP ");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
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 (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
} 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;
}