• ip效验和ip段的效验


     
    package com.juchen.utils;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * ip 相关的工具方法
     */
    public class IpUtil {
    
      /*  *//**
         * 根据request获取用户的IP地址
         * @param request
         * @return
         */
        public static String getRemoteHost(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.getRemoteAddr();
            }
            return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
        }
        
        
        
        
        /**
         * 
         * @param request
         * @return	192.168.1.121 	192.168.1.121,192.168.1.122
         */
        public static String getIpAddress(HttpServletRequest request){
    		// 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
        	String ip = "192.168.0.1";
        	try {
        		
        		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.getHeader("X-Real-IP");
        	    }
        		
        		//如果没有代理,则获取真实ip
        		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        			ip = request.getRemoteAddr();
        		}
        		
        		if(ip.equals("0:0:0:0:0:0:0:1")){
        			ip = "127.0.0.1";
        		}
        				
    		} catch (Exception e) {
    			ip = "192.168.0.1";
    		}
    		
    		return ip;
    	}
    
        
        
        
        
        
        
        
        
    	/*
    	 * 验证IP是否属于某个IP段
    	 *
    	 * 
    	 * 
    	 * ipSection IP段(以'-'分隔)
    	 * 
    	 * ip 所验证的IP号码
    	 *
    	 * 
    	 * 
    	 */
    
    	public static boolean ipExistsInRange(String ip, String ipSection) {
    		ipSection = ipSection.trim();
    		ip = ip.trim();
    		int idx = ipSection.indexOf('-');
    		String beginIP = ipSection.substring(0, idx);
    		String endIP = ipSection.substring(idx + 1);
    		return getIp2long(beginIP) <= getIp2long(ip) && getIp2long(ip) <= getIp2long(endIP);
    	}
    
    	public static long getIp2long(String ip) {
    		ip = ip.trim();
    		String[] ips = ip.split("\.");
    		long ip2long = 0L;
    		for (int i = 0; i < 4; ++i) {
    			ip2long = ip2long << 8 | Integer.parseInt(ips[i]);
    		}
    		return ip2long;
    	}
    	
    	
    	public static long getIp2long2(String ip) {
    		ip = ip.trim();
    		String[] ips = ip.split("\.");
    		long ip1 = Integer.parseInt(ips[0]);
    		long ip2 = Integer.parseInt(ips[1]);
    		long ip3 = Integer.parseInt(ips[2]);
    		long ip4 = Integer.parseInt(ips[3]);
    		long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
    		return ip2long;
    	} 
        
        
        
        
        
    	  public static void main(String[] args){
    
    	       //10.10.10.116 是否属于固定格式的IP段10.10.1.00-10.10.255.255
    
    	        String ip="10.8.12.255";
    
    	        String ipSection="10.1.1.00-10.10.10.250";
    
    	       boolean exists=ipExistsInRange(ip,ipSection);
    
    	        System.out.println(exists);
    
    	        System.out.println(getIp2long(ip));
    
    	        System.out.println(getIp2long2(ip));
    
    	    }
        
        
        
        
    
    
    }
    

      

    自己的业务是支持多个单个ip和多个ip段  例如 192.168.0.1-192.168.0.100,10.10.10.1-10.10.10.100;170.20.20.100

    public static void main(String[] args) {
    		String ip = "192.168.10.10,255.168.100.255;192.168.0.1-192.168.3.100,192.168.5.1-192.168.5.100";
    		String remoteHost = "192.168.255.88,192.168.10.11";
    		checkIp(ip, remoteHost);
    }
    	
    	/**
    	 * 
    	 * @param ip  用户填写可以通过的ip或ip段(可能存在多个或两种都存在)
    	 * @param remoteHost 这个是抓取到的ip (可能存在多个)
    	 * @return
    	 */
    	public static boolean checkIp(String ip, String remoteHost) {
    		// IP地址校验
    		boolean flag = false;
    		if (!StringUtil.isEmpty(ip)) {
    			String[] remoteHostList = remoteHost.split(",");
    			// 新增短信ip段的效验
    			String[] ipArry = ip.split(";");
    			for (int i = 0; i < remoteHostList.length; i++) {
    				if (DateConfig.server_ip.indexOf(remoteHostList[i].trim()) > -1) {
    					flag = true;
    					break;
    				} else {
    					if (ip.indexOf(remoteHostList[i].trim()) > -1) {
    						flag = true;
    						break;
    					}
    
    					// 判断ip段
    					for (String ipStr : ipArry) {
    						if (ipStr.indexOf("-") != -1) { // 包含ip段
    							String[] ipSegent = ipStr.split(",");
    							for (String strs : ipSegent) { // str 192.168.0.1 -192.168.0.100
    								boolean ipfalg = IpUtil.ipExistsInRange(remoteHostList[i].trim(), strs.trim());
    								if (ipfalg) {
    									flag = true;
    									break;
    								}
    							}
    						}
    					}
    
    				}
    			}
    
    			if (!flag) {
    				System.out.println("ip效验不通过");
    			}else{
    				System.out.println("ip效验通过");
    			}
    
    		}
    		return flag;
    	}
    

      

    上面效验ip是否在ip段的短发里面转载自:https://blog.csdn.net/qq_14918243/article/details/51452489

  • 相关阅读:
    CSS经典实用技巧10招
    分析与设计数据库模型的简单过程
    MS SQL数据类型及长度限制
    firefox与IE的nextSibling
    DIV+CSS布局总结
    Sql Server查找临时表,并删除
    powerDesigner15.1破解
    分别使用函数及游标实现SQL多行转一列
    生命的帐单
    北京有380万“北漂族” 没有归属感但不离不弃!
  • 原文地址:https://www.cnblogs.com/yangyang2018/p/11089715.html
Copyright © 2020-2023  润新知