第一步:先在項目src文件下創建一個ipConfig.properties文件,加入黑白名單的ipweb
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.*;app
二,建IpFilter,過濾器文件
/**jsp
@version 1.0 */
public class IpFilter implements Filter{
//用來存放初始化後的IP白名單列表對應的正則表達式
private List<String> allowList = new ArrayList<String>();ide
@Override
public void init(FilterConfig arg0) throws ServletException {
try {
System.out.println("過濾器IpFilter開始初始化,功能:IP訪問限制");
initConfig();//在過濾器初始化的時候初始化白名單列表
} catch (IOException e) {
e.printStackTrace();
}
}this
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
//獲取訪問的IP地址
String remoteAddr = request.getRemoteAddr();url
//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);
} spa
} }
@Override
public void destroy() {
System.out.println("過濾器IpFilter結束。");
} code
/* 對配置文件進行初始化並校驗 * xml
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><!-- 結束過濾器 -->