1. 程式人生 > >javaWeb項目加入IP黑白名單

javaWeb項目加入IP黑白名單

則表達式 direct param init web.xml rec lag null ||

第一步:先在項目src文件下建立一個ipConfig.properties文件,加入黑白名單的ip

1)ipConfig.properties:

#單個IP地址的配置,多個之間用逗號或分好隔開
allowIP=192.168.1.15;127.0.0.1;
#IP地址區間方式的配置,多個區間用逗號或分好隔開
allowIPRange=172.20.32.10-172.20.32.11;172.20.32.88-172.20.32.89;
#通配符,多個用逗號或分好隔開
allowIPWildcard=192.168.1.*;

二,建IpFilter,過濾器文件
/**

  • 過濾器 功能:對訪問者IP進行限制訪問
  • @author hht *
  • @serialData 2018-09-28 *
  • @version 1.0 */
    public class IpFilter implements Filter{
    //用來存放初始化後的IP白名單列表對應的正則表達式
    private List<String> allowList = new ArrayList<String>();

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    try {
    System.out.println("過濾器IpFilter開始初始化,功能:IP訪問限制");

    initConfig();//在過濾器初始化的時候初始化白名單列表
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    //獲取訪問的IP地址
    String remoteAddr = request.getRemoteAddr();

    //System.out.println("===============" + remoteAddr); 
    //如果allowList為空,則認為沒做限制,不為空則檢查是否限制
    if(allowList.size() == 0 || allowList == null) { 
        filterChain.doFilter(request, response);
        } else { 
        Boolean flag = false;
        //訪問標誌,默認為false,限制訪問 //進行逐個檢查 
        for(String regex : allowList){
        if(remoteAddr.matches(regex)){
        //ip沒被限制,正常訪問 
        filterChain.doFilter(request, response); 
        flag = true; 
       //置為true,表示不限制訪問 break; 
        } 
        } if(!flag) {
        //ip被限制,跳到指定頁面 

    // resp.sendRedirect("../noPrivilege.jsp");
    request.getRequestDispatcher("/WEB-INF/page/noPrivilege.jsp").forward(request, response);
    }

        }
    }

    @Override
    public void destroy() {
    System.out.println("過濾器IpFilter結束。");
    }

    /* 對配置文件進行初始化並校驗 *

    • @author hht *
      • @serialData 2018-09-28 *
      • @throws IOException */

    public void initConfig() throws IOException {
    //將文件轉化成流
    /InputStream inputStream = IpFilter.class.getResourceAsStream("./config/ipConfig.properties");
    Properties properties = new Properties(); //通過Properties對象實例加載流
    properties.load(inputStream);
    /
    PropertiesUtil.readProperties("ipConfig.properties");
    //獲取三種配置方式的值
    String allowIP = PropertiesUtil.getProperty("allowIP");
    String allowIPRange = PropertiesUtil.getProperty("allowIPRange");
    String allowIPWildcard = PropertiesUtil.getProperty("allowIPWildcard");

    //對用戶配置的三種方式的IP白名單進行格式校驗 
    if(!validate(allowIP, allowIPRange, allowIPWildcard)) { 
        throw new RuntimeException("配置文件有錯,請檢查!");
        } 
    /* * 將每一種配置方法放置到allowList中 */ 
    //將第一種配置方法放到allowList中 將第一種方式配置的ip地址解析出來,添加到存放IP白名單集合 
    if(null != allowIP && !"".equals(allowIP.trim())) {
        String[] allowIPs = allowIP.split(",|;"); 
        for(String ip : allowIPs) { 
        allowList.add(ip); 

    }
    }
    //將第二種配置方法放到allowList中將第二種方式配置的ip地址解析出來,添加到存放IP白名單集合
    if(null != allowIPRange && !"".equals(allowIPRange.trim())) {
    //先進行每一段的分割
    String[] allowIPRanges = allowIPRange.split(",|;");
    if(allowIPRanges.length > 0) {
    //對每一段進行遍歷
    for(String allowRanges : allowIPRanges) {
    if(allowRanges != null && !"".equals(allowRanges.trim())) {
    //對該段的ip進行解析
    String[] ips = allowRanges.split("-");
    if(ips.length > 0 && ips.length < 3) {
    String from = ips[0];//得到該段的起始ip
    String to = ips[1]; //得到該段的結束ip
    //獲取該ip段地址的前三段,因為起始和結束的ip的前三段一樣
    String share = from.substring(0, from.lastIndexOf(".")+1);
    //獲取該ip段的起始ip的最後一段
    int start = Integer.parseInt(from.substring(from.lastIndexOf(".")+1, from.length()));
    //獲取該ip段的結束ip的最後一段
    int end = Integer.parseInt(to.substring(to.lastIndexOf(".")+1, to.length()));
    for(int i=start; i<=end; i++) {
    String ip = share + String.valueOf(i); allowList.add(ip);
    }
    } else {
    throw new RuntimeException("配置文件有錯,請檢查!");

        } 
    } 

    }
    }
    }

    //將第三種配置方法放到allowList中 將第三種方式配置的ip地址解析為一條一條的正則表達式,添加到存放IP白名單集合,如對此處不明白可以先看後面的備註
    if(allowIPWildcard != null && !"".equals(allowIPWildcard)) {
    //獲取每個含通配符的ip地址
    String[] allowIPWildcards = allowIPWildcard.split(",|;");
    if(allowIPWildcards.length > 0) {
    for(String ip : allowIPWildcards) {
    if(ip.indexOf("") != -1) {
    //對
    進行替換
    ip = ip.replaceAll("\*", "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)");
    allowList.add(ip);
    } else {
    throw new RuntimeException("配置文件有錯,請檢查!");
    }
    }
    }

}
//打印輸出allowList
for(String str : allowList) {
System.out.println(str);
}
}

/** * 對配置文件進行校驗 
  * @author hht * 
 * @serialData 2018-09-28 *
 *  @param allowIP 
 *   @param allowIPRange 
 *   @param allowIPWildcard 
 * @return */ 
public Boolean validate(String allowIP, String allowIPRange, String allowIPWildcard) {
    Boolean result = false; 
    //IP地址每一段的正則 
    String regx = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; 
    //整個ip的正則
    String ipRegx = regx + "\\." + regx + "\\."+ regx + "\\." + regx; 
    //對第一種方式進行校驗 
    Pattern pattern = Pattern.compile("("+ipRegx+")|("+ipRegx+"(,|;))*"); 
    if(this.isNullorMatches(allowIP, pattern)){ 
        result = true; //匹配成功
        } else { 
        result = false; 

}
//對第二種方式進行校驗
pattern = Pattern.compile("("+ipRegx+")\-("+ipRegx+")|" + "(("+ipRegx+")\-("+ipRegx+")(,|;))");
if(this.isNullorMatches(allowIPRange, pattern)){
result = true; //匹配成功
} else {
result = false;
}
//對第三種方式進行校驗
pattern = Pattern.compile("("+regx+"\."+ regx+"\."+regx+"\."+ "\)|" + "("+regx+"\."+regx+"\."+regx+"\."+ "\(,|;))
");
if(this.isNullorMatches(allowIPWildcard, pattern)){
result = true; //匹配成功
} else {
result = false;
}
return result;
}

/** * 進行正則匹配 * 
 * @author hht * 
 * @serialData 2018-09-28 *
 *  @param allow * @return */ 
public Boolean isNullorMatches(String allow, Pattern pattern) {
    //如果為空,說明用戶沒添加該項,不做處理
    if(allow == null || "".equals(allow.trim())) { return true; } else {
    //在最後面沒有,或;的給添上 
    if(!allow.endsWith(";") && !allow.endsWith(",")) { 
    allow += ";";
    }
    //如果匹配,則返回true
  if(pattern.matcher(allow).matches()) { 
    return true; 
    } 
} 
return false; 

}

}

三,在項目中web.xml配置文件中加入過濾配置

IPFilter xxxx.IpFilter IPFilter /*

javaWeb項目加入IP黑白名單