栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC通过拦截器实现IP黑名单

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC通过拦截器实现IP黑名单

本文实例为大家分享了SpringMVC通过拦截器实现IP黑名单的具体代码,供大家参考,具体内容如下

以前没有遇到这个需要,后面在网上找了很久,参考了很多文档给出的方案。

1.配置拦截器

这里使用全局拦截:


  
    getParameterMap(HttpServletRequest request) {
 Enumeration names = request.getParameterNames();
 String name;
 Map map = new HashMap();
 while (names.hasMoreElements()) {
 name = names.nextElement();
 map.put(name, request.getParameter(name).trim().replaceAll("'", ""));
 }
 return map;
}


String getRealUrl(String uri, Map params) {
 StringBuffer sb = new StringBuffer(uri);
 if (params != null) {
 int i = 0;
 for (String key : params.keySet()) {
 i++;
 if (i == 1) {
 sb.append("?" + key + "=" + params.get(key));
 } else {
 sb.append("&" + key + "=" + params.get(key));
 }
 }
 }
 return sb.toString();
}
}

2.校验IP工具

public class IpInterceptUtils {
private static String date ;
private static PropertiesUtil p=null;
 
 public static boolean chickIpBreak(String ip) throws IOException{
 if(p == null){
  p = new PropertiesUtil("conf/ip-black.properties");
 }else{
  String str = new SimpleDateFormat("MMddHHmmss").format(new Date()); 
  str=str.substring(0,9);
  if(date==null || !date.equals(str)){ 
  date = str; 
  p = new PropertiesUtil("conf/ip-black.properties");
  }
 }
   Enumeration en = p.getProps().propertyNames();
   while (en.hasMoreElements()) {
    String key = (String) en.nextElement();
    if(key.equals(ip)){
    return true;
    }
   }
 return false;
 }
}

3.配置文件读取类

PropertiesUtil

package com.nps.base.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Component;


@Component("PropertiesUtil")
public class PropertiesUtil {
 //配置文件的路径
 private String configPath=null;
 
 
 private Properties props=null;
 
 
 public PropertiesUtil() throws IOException{
  if(props==null){
  InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("conf/application.properties");
   props = new Properties();
   props.load(in);
    //关闭资源
   in.close();
  }
  
 }
 
 
 public PropertiesUtil(String path) throws IOException{
  if(props==null){
  InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
   props = new Properties();
   props.load(in);
    //关闭资源
   in.close();
  }
  
 }
 
 
 public String readValue(String key) throws IOException {
  return props.getProperty(key);
 }
 
 
 public Map readAllProperties() throws FileNotFoundException,IOException {
  //保存所有的键值
  Map map=new HashMap();
  Enumeration en = props.propertyNames();
  while (en.hasMoreElements()) {
   String key = (String) en.nextElement();
   String Property = props.getProperty(key);
   map.put(key, Property);
  }
  return map;
 }

 
 public void setValue(String key,String value) throws IOException {
  Properties prop = new Properties();
  InputStream fis = new FileInputStream(this.configPath);
  // 从输入流中读取属性列表(键和元素对)
  prop.load(fis);
  // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
  // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
  OutputStream fos = new FileOutputStream(this.configPath);
  prop.setProperty(key, value);
  // 以适合使用 load 方法加载到 Properties 表中的格式,
  // 将此 Properties 表中的属性列表(键和元素对)写入输出流
  prop.store(fos,"last update");
  //关闭文件
  fis.close();
  fos.close();
 }
 
 
 public Properties getProps() {
 return props;
 }

 public static void main(String[] args) {
  PropertiesUtil p;
  try {
   p = new PropertiesUtil("conf/ip-black.properties");
   Enumeration en = p.props.propertyNames();
   String str="";
   while (en.hasMoreElements()) {
    String key = (String) en.nextElement();
    System.out.println(key);
   }

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 
}

附上黑名单IP文件格式

ip-black.properties 配置文件

45.119.99.35
103.253.2.165
157.65.166.51
202.57.55.242
119.82.252.122
140.227.53.126
140.227.211.20
140.227.208.20
116.253.84.183

附加

之所以使用配置文件读取黑名单IP是为了加快数据读取速度,因为每次访问服务器都会被校验,使用数据库会加大数据库的压力,这个方法中每10秒就会重新读取配置文件缓存其实更好,不使用缓存主要是为了,因为放在缓存中还要写对应的操作方法,如果有什么更好的方法欢迎大家讨论。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/137429.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号