Skip to content

Commit

Permalink
IP tool
Browse files Browse the repository at this point in the history
  • Loading branch information
snowxwolf committed Mar 27, 2018
1 parent 25ea8f8 commit 637108f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/xwolf/boot/utils/IPUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.xwolf.boot.utils;

import javax.servlet.http.HttpServletRequest;

/**
* IP工具
* @author xwolf
* @since 1.8
*/
public class IPUtil {

/**
* 获取IP地址
* @param request
* @return
*/
public static String getIP(HttpServletRequest request) {
if (request == null) {
return "unknown";
}
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("X-Forwarded-For");
}
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("X-Real-IP");
}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}



}

0 comments on commit 637108f

Please sign in to comment.