温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javaWeb项目加入IP黑白名单

发布时间:2020-07-16 02:33:27 来源:网络 阅读:1325 作者:HHT15927087748 栏目:编程语言

第一步:先在项目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配置文件中加入过滤配置

<!-- 过滤器 -->
<filter>
<filter-name>IPFilter</filter-name>
<filter-class>xxxx.IpFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>IPFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 结束过滤器 -->

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI