1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /* 공인 및 프록시 */ public String getClientIP(HttpServletRequest request) { String ip = request.getHeader("확인 X-FORWARDED-FOR"); if(ip == null || ip.length() == 0) { ip = request.getHeader("Proxy-Client-IP"); } if(ip == null || ip.length() == 0) { ip = request.getHeader("WL-Proxy-Client-IP"); // 웹로직 } if(ip == null || ip.length() == 0) { ip = request.getRemoteAddr(); } return ip; } /* 내부 */ import java.net.*; /* 만약 공인 IP없으면 내부 IP 가져오도록 처리 */ public static String getCurrentEnvironmentNetworkIp(){ Enumeration netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return getLocalIp(); } while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface)netInterfaces.nextElement(); Enumeration address = ni.getInetAddresses(); if (address == null) { return getLocalIp(); } while (address.hasMoreElements()) { InetAddress addr = (InetAddress)address.nextElement(); if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress() && !addr.isAnyLocalAddress() ) { String ip = addr.getHostAddress(); if( ip.indexOf(".") != -1 && ip.indexOf(":") == -1 ){ return ip; } } } } return getLocalIp(); } public static String getLocalIp(){ try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { return null; } } | cs |
java 접속 ip 조회
2015. 7. 22. 12:33