springboot过滤器禁止ip频繁访问
2021-02-20 15:20
标签:nbsp oca rate map not cat auto package move 1.编写一个过滤器: 2. 创建一个监听器:需要初始化俩个容器: 3.iputil 4配置 springboot启动类中添加过滤器和监听器的包扫描 spring web.xml 过滤器 监听器: springboot过滤器禁止ip频繁访问 标签:nbsp oca rate map not cat auto package move 原文地址:https://www.cnblogs.com/achengmu/p/12681190.htmlimport lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@WebFilter(urlPatterns="/dyflight/*")
public class IpFilter implements Filter{
/**
* 默认限制时间(单位:ms)3600000,3600(s),
*/
private static final long LIMITED_TIME_MILLIS = 10 * 1000;
/**
* 用户连续访问最高阀值,超过该值则认定为恶意操作的IP,进行限制
*/
private static final int LIMIT_NUMBER = 5;
/**
* 用户访问最小安全时间,在该时间内如果访问次数大于阀值,则记录为恶意IP,否则视为正常访问
*/
private static final int MIN_SAFE_TIME = 5000;
private FilterConfig config;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig; //设置属性filterConfig
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@SuppressWarnings("unchecked")
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
ServletContext context = config.getServletContext();
// 获取限制IP存储器:存储被限制的IP信息
//Map
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@WebListener
public class MyApplicationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
log.debug("liting: contextInitialized");
log.debug("MyApplicationListener初始化成功");
ServletContext context = sce.getServletContext();
// IP存储器
ConcurrentHashMap
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPUtil {
public static String getRemoteIpAddr(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.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.getRemoteAddr();
if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ip= inet.getHostAddress();
}
}
return ip;
}
}
@ServletComponentScan(basePackages="cn.xxx.common")
文章标题:springboot过滤器禁止ip频繁访问
文章链接:http://soscw.com/index.php/essay/58037.html