| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.sandu.ximon.admin.utils; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.net.*; |
| | | import java.util.Enumeration; |
| | | |
| | | @Slf4j |
| | | public class IPUtils { |
| | | /** |
| | | * è·åIPå°å |
| | | * <p> |
| | | * 使ç¨Nginxçåå代çè½¯ä»¶ï¼ åä¸è½éè¿request.getRemoteAddr()è·åIPå°å |
| | | * å¦æä½¿ç¨äºå¤çº§åå代ççè¯ï¼X-Forwarded-Forçå¼å¹¶ä¸æ¢ä¸ä¸ªï¼èæ¯ä¸ä¸²IPå°åï¼X-Forwarded-Forä¸ç¬¬ä¸ä¸ªéunknownçææIPå符串ï¼å为çå®IPå°å |
| | | */ |
| | | public static String getIpAddr(HttpServletRequest request) { |
| | | String ip = null; |
| | | try { |
| | | ip = request.getHeader("x-forwarded-for"); |
| | | if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | if (StringUtils.isBlank(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_CLIENT_IP"); |
| | | } |
| | | if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
| | | } |
| | | if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getRemoteAddr(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("IPUtils ERROR ", e); |
| | | } |
| | | |
| | | //使ç¨ä»£çï¼åè·å第ä¸ä¸ªIPå°å |
| | | if (StringUtils.isBlank(ip) && ip.length() > 15) { |
| | | if (ip.indexOf(",") > 0) { |
| | | ip = ip.substring(0, ip.indexOf(",")); |
| | | } |
| | | } |
| | | |
| | | return ip; |
| | | } |
| | | |
| | | /** |
| | | * @description è·åç³»ç»ç¯å¢ip |
| | | */ |
| | | public static String getCurrentSystemIp() { |
| | | try { |
| | | Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); |
| | | while (networkInterfaces.hasMoreElements()) { |
| | | NetworkInterface ni = networkInterfaces.nextElement(); |
| | | Enumeration<InetAddress> nias = ni.getInetAddresses(); |
| | | while (nias.hasMoreElements()) { |
| | | InetAddress ia = nias.nextElement(); |
| | | if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { |
| | | return ia.toString().substring(1); |
| | | } |
| | | } |
| | | } |
| | | } catch (SocketException e) { |
| | | log.error("Failed to get current system Ip ", e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static String getIpAddrs(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.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(); |
| | | if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) { |
| | | //æ ¹æ®ç½å¡åæ¬æºé
ç½®çIP |
| | | InetAddress inet = null; |
| | | try { |
| | | inet = InetAddress.getLocalHost(); |
| | | ip = inet.getHostAddress(); |
| | | } catch (UnknownHostException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (ip != null && ip.length() > 0) { |
| | | //å第ä¸ä¸ªéunknownçææIPå符串 |
| | | String[] split = ip.split(","); |
| | | for (String s : split) { |
| | | if (!"unknown".equalsIgnoreCase(s)) { |
| | | ip = s; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return ip; |
| | | } |
| | | |
| | | public static String getPublicIp() { |
| | | try { |
| | | String ip = "http://pv.sohu.com/cityjson?ie=utf-8"; |
| | | URL url = new URL(ip); |
| | | HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); |
| | | BufferedReader in = new BufferedReader(new |
| | | InputStreamReader(urlConnection.getInputStream())); |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | String read; |
| | | while ((read = in.readLine()) != null) { |
| | | stringBuilder.append(read); |
| | | } |
| | | //坿å°stringBuilderåè§£æå¾å°çä¿¡æ¯ |
| | | String string = StringUtils.split(stringBuilder.toString(), "=")[1]; |
| | | JSONObject jsonObject = JSONUtil.parseObj(string); |
| | | return jsonObject.get("cip").toString(); |
| | | } catch (Exception e) { |
| | | return "æªç¥"; |
| | | } |
| | | } |
| | | |
| | | } |