All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cntool.core.ip.IpUtils Maven / Gradle / Ivy

The newest version!
package com.cntool.core.ip;

import com.cntool.core.component.NumberPool;
import com.cntool.core.component.StringPool;
import com.cntool.core.component.SymbolPool;
import com.cntool.core.string.StrUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @program: cntool
 * @description: IP工具类
 * @author: ID-Tang
 * @create: 2022-03-04 11:27
 * @copyright: Copyright (c) [2022] [ID-tang]
 * [cntool] is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * ...
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
public class IpUtils {
    /**
     * 获取IP地址
     *
     * @param request request
     * @return IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader(StringPool.X_FORWARDED_FOR);
            if (StrUtils.isBlank(ipAddress) || ipAddress.length() == NumberPool.ZERO || StringPool.UN_KNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader(StringPool.PROXY_CLIENT_IP);
            }
            if (StrUtils.isBlank(ipAddress) || ipAddress.length() == NumberPool.ZERO || StringPool.UN_KNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader(StringPool.WL_PROXY_CLIENT_IP);
            }
            if (StrUtils.isBlank(ipAddress) || ipAddress.length() == NumberPool.ZERO || StringPool.UN_KNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (StringPool.LOCAL_IP.equals(ipAddress)) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    assert inet != null;
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            // "***.***.***.***".length()
            if (StrUtils.isNotBlank(ipAddress) && ipAddress.length() > NumberPool.FIFTEEN) {
                // = 15
                if (ipAddress.indexOf(SymbolPool.COMMA) > NumberPool.ZERO) {
                    ipAddress = ipAddress.substring(NumberPool.ZERO, ipAddress.indexOf(SymbolPool.COMMA));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }

        return ipAddress;
    }

    /**
     * 判断的是否是IP地址
     *
     * @param str 传入字符串
     * @return true是 false否
     */
    public static boolean isIp(String str) {
        //切割字符串
        String[] ipStr = str.split(SymbolPool.SPOT);
        //返回值
        boolean result = false;
        //判断位数为4则继续校验
        if (ipStr.length != NumberPool.FOUR) {
            return false;
        }
        //遍历对比
        for (String ip : ipStr) {
            //只要有一个不是数字则不是IP
            if (!StrUtils.isInteger(ip)) {
                result = false;
                break;
            }
            //获取值对比是否在0-255之间
            //不在则不是IP
            int parseInt = Integer.parseInt(ip);
            if (NumberPool.ZERO <= parseInt && parseInt <= NumberPool.TWO_HUNDRED_FIFTY_FIVE) {
                result = true;
                continue;
            }
            result = false;
            break;
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy