Commit 6acfb9caf3e7750db7b7daaf595c1d13028292c6
Merge branch 'pudong_jdk8' of http://61.169.120.202:8888/panzhaov5/bsth_control into pudong_jdk8
Showing
1 changed file
with
62 additions
and
62 deletions
src/main/java/com/bsth/util/IpUtils.java
| 1 | -package com.bsth.util; | |
| 2 | - | |
| 3 | -import javax.servlet.http.HttpServletRequest; | |
| 4 | -import java.net.Inet4Address; | |
| 5 | -import java.net.InetAddress; | |
| 6 | -import java.net.NetworkInterface; | |
| 7 | -import java.util.Enumeration; | |
| 8 | - | |
| 9 | -public class IpUtils { | |
| 10 | - | |
| 11 | - public static String getIpAddr(HttpServletRequest request) { | |
| 12 | - if (request == null) { | |
| 13 | - return "unknown"; | |
| 14 | - } | |
| 15 | - String ip = request.getHeader("x-forwarded-for"); | |
| 16 | - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 17 | - ip = request.getHeader("Proxy-Client-IP"); | |
| 18 | - } | |
| 19 | - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 20 | - ip = request.getHeader("X-Forwarded-For"); | |
| 21 | - } | |
| 22 | - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 23 | - ip = request.getHeader("WL-Proxy-Client-IP"); | |
| 24 | - } | |
| 25 | - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 26 | - ip = request.getHeader("X-Real-IP"); | |
| 27 | - } | |
| 28 | - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 29 | - ip = request.getRemoteAddr(); | |
| 30 | - } | |
| 31 | - return ip; | |
| 32 | - } | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * 获取本地IP地址 | |
| 36 | - * @return | |
| 37 | - */ | |
| 38 | - public static String getLocalIpAddress() { | |
| 39 | - try { | |
| 40 | - Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); | |
| 41 | - InetAddress ip = null; | |
| 42 | - while (allNetInterfaces.hasMoreElements()) { | |
| 43 | - NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); | |
| 44 | - if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() || "docker0".equals(netInterface.getDisplayName())) { | |
| 45 | - continue; | |
| 46 | - } else { | |
| 47 | - Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); | |
| 48 | - while (addresses.hasMoreElements()) { | |
| 49 | - ip = addresses.nextElement(); | |
| 50 | - if (ip != null && ip instanceof Inet4Address) { | |
| 51 | - return ip.getHostAddress(); | |
| 52 | - } | |
| 53 | - } | |
| 54 | - } | |
| 55 | - } | |
| 56 | - } catch (Exception e) { | |
| 57 | - System.err.println("IP地址获取失败" + e.toString()); | |
| 58 | - } | |
| 59 | - | |
| 60 | - return ""; | |
| 61 | - } | |
| 62 | -} | |
| 1 | +package com.bsth.util; | |
| 2 | + | |
| 3 | +import javax.servlet.http.HttpServletRequest; | |
| 4 | +import java.net.Inet4Address; | |
| 5 | +import java.net.InetAddress; | |
| 6 | +import java.net.NetworkInterface; | |
| 7 | +import java.util.Enumeration; | |
| 8 | + | |
| 9 | +public class IpUtils { | |
| 10 | + | |
| 11 | + /** | |
| 12 | + * 代理环境下只认X-Forwarded-For和X-Real-IP header,同时代理程序需要重写这两个header为原始IP | |
| 13 | + * 非代理环境只应该用request.getRemoteAddr() | |
| 14 | + * @param request | |
| 15 | + * @return | |
| 16 | + */ | |
| 17 | + public static String getIpAddr(HttpServletRequest request) { | |
| 18 | + if (request == null) { | |
| 19 | + return "unknown"; | |
| 20 | + } | |
| 21 | + String ip = null; | |
| 22 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 23 | + ip = request.getHeader("X-Forwarded-For"); | |
| 24 | + } | |
| 25 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 26 | + ip = request.getHeader("X-Real-IP"); | |
| 27 | + } | |
| 28 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 29 | + ip = request.getRemoteAddr(); | |
| 30 | + } | |
| 31 | + return ip; | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * 获取本地IP地址 | |
| 36 | + * @return | |
| 37 | + */ | |
| 38 | + public static String getLocalIpAddress() { | |
| 39 | + try { | |
| 40 | + Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); | |
| 41 | + InetAddress ip = null; | |
| 42 | + while (allNetInterfaces.hasMoreElements()) { | |
| 43 | + NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); | |
| 44 | + if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() || "docker0".equals(netInterface.getDisplayName())) { | |
| 45 | + continue; | |
| 46 | + } else { | |
| 47 | + Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); | |
| 48 | + while (addresses.hasMoreElements()) { | |
| 49 | + ip = addresses.nextElement(); | |
| 50 | + if (ip != null && ip instanceof Inet4Address) { | |
| 51 | + return ip.getHostAddress(); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + } catch (Exception e) { | |
| 57 | + System.err.println("IP地址获取失败" + e.toString()); | |
| 58 | + } | |
| 59 | + | |
| 60 | + return ""; | |
| 61 | + } | |
| 62 | +} | ... | ... |