前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >博客系统添加IP黑名单拦截功能

博客系统添加IP黑名单拦截功能

作者头像
用户9131103
发布2023-07-17 19:19:17
2260
发布2023-07-17 19:19:17
举报
文章被收录于专栏:工作经验

为博客系统添加IP黑名单拦截功能 仅贴出部分代码

创建IP拦截器

代码语言:javascript
复制
package com.my.blog.website.interceptor;
import com.my.blog.website.service.ILogService;
import com.my.blog.website.utils.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jesse-liu
 * @description: Ip黑名单拦截器
 * @date 2020/5/13 11:12
 */
@Component
public class IpInterceptor implements HandlerInterceptor {

    @Resource
    private ILogService logService;

    private static final Logger LOGGE = LoggerFactory.getLogger(IpInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        LOGGE.info("进入IP黑名单拦截器:{}", IpUtils.getIpAddress(request));
        String ip = IpUtils.getIpAddress(request);
        if (IpUtils.chickIpBreak(ip)) {
            logService.insertLog("进入IP黑名单拦截器", IpUtils.getIpAddress(request), request.getRemoteAddr(), null);
            response.setHeader("Content-Type", "text/html; charset=UTF-8");
            response.getWriter().write("<p>欢迎使用.</p>");
            response.getWriter().flush();
            response.getWriter().close();
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}

往webMVC里添加拦截器组件

代码语言:javascript
复制
   仅贴出部分代码:
代码语言:javascript
复制
package com.my.blog.website.interceptor;
import com.my.blog.website.utils.TaleUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 向mvc中添加自定义组件
 * Created by BlueT on 2017/3/9.
 * 更换MVC配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private BaseInterceptor baseInterceptor;
    @Resource
    private IpInterceptor ipInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       //拦截器是按顺序执行的
        registry.addInterceptor(ipInterceptor);
        registry.addInterceptor(baseInterceptor);
    }
}

使用的工具类

代码语言:javascript
复制
package com.my.blog.website.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 * @author Jesse-liu
 * @description: ip操作工具类
 * @date 2020/5/13 11:29
 */
public class IpUtils {

    private static String longToIp(Long number) {
        String ip = "";
        for (int i = 3; i >= 0; i--) {
            long tmp = number >> (8 * i);
            ip += String.valueOf((tmp & 0xff));
            if (i != 0) {
                ip += ".";
            }
        }
        return ip;
    }

    private static Long ipToLong(String ip) {
        long ips = 0l;
        String[] ipArr = ip.split("\\.");
        for (int i = 0; i < 4; i++) {
            ips = ips << 8 | Integer.parseInt(ipArr[i]);
        }
        return ips;
    }

    private static String date;
    private static PropertiesUtil p = null;

    /***
     * 校验IP是否加入黑名单
     * @param ip
     * @return true 是在黑名单
     * @throws IOException
     */
    public static boolean chickIpBreak(String ip) throws IOException {
        if (p == null) {
            p = new PropertiesUtil("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("ip-black.properties");
            }
        }
        Enumeration en = p.getProps().propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            if (StringUtils.isNotBlank(key) && key.contains("~")) {
                String[] ipTemps = key.split("~");
                if (!isLocalIp(ip) && (Long.parseLong(ipTemps[0]) <= IpUtils.ipToLong(ip) &&
                        IpUtils.ipToLong(ip) <= Long.parseLong(ipTemps[1]))) {
                    return true;
                }

            } else if (!isLocalIp(ip) && key.equals(IpUtils.ipToLong(ip).toString())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     * @throws IOException
     */
    public final static String getIpAddress(HttpServletRequest request)
            throws IOException {
        // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");
        // if (logger.isInfoEnabled()) {
        // logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip="
        // + ip);
        // }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                // if (logger.isInfoEnabled()) {
                // logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip="
                // + ip);
                // }
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                // if (logger.isInfoEnabled()) {
                // logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip="
                // + ip);
                // }
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                // if (logger.isInfoEnabled()) {
                // logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip="
                // + ip);
                // }
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                // if (logger.isInfoEnabled()) {
                // logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip="
                // + ip);
                // }
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                // if (logger.isInfoEnabled()) {
                // logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip="
                // + ip);
                // }
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }

        return ip;
    }

    /***
     * 服务器是否是本机ip
     * @param ip
     * @return
     */
    public static boolean isLocalIp(String ip) throws IOException {
        return ip.equals("127.0.0.1") || ip.equals("localhost") || ip.equals("0:0:0:0:0:0:0:1");
    }
}
代码语言:javascript
复制
package com.my.blog.website.utils;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Jesse-liu
 * @description: 配置文件读取工具类
 * @date 2020/5/13 11:24
 */
public class PropertiesUtil {
    //配置文件的路径
    private String configPath = null;

    /**
     * 配置文件对象
     */
    private Properties props = null;

    public PropertiesUtil() throws IOException {
        if (props == null) {
            InputStreamReader in = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream("ip-black.properties"), "utf-8");
            props = new Properties();
            props.load(in);
            //关闭资源
            in.close();
        }
    }

    /**
     * @param path 配置文件路径
     * @return : null
     * @author Jesse-liu
     * @date 2020/5/13
     * @description: 有参构造函数,用于sh运行,自动找到classpath下的path路径。
     **/
    public PropertiesUtil(String path) throws IOException {
        if (props == null) {
            InputStreamReader in = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(path), "utf-8");
            configPath = path;
            props = new Properties();
            props.load(in);
            //关闭资源
            in.close();
        }
    }

    /**
     * 根据key值读取配置的值
     * Jun 26, 2010 9:15:43 PM
     *
     * @param key key值
     * @return key 键对应的值
     * @throws IOException
     * @author 朱志杰
     */
    public String readValue(String key) throws IOException {
        return props.getProperty(key);
    }

    /**
     * 读取properties的全部信息
     *
     * @throws FileNotFoundException 配置文件没有找到
     * @throws IOException           关闭资源文件,或者加载配置文件错误
     */
    public Map<String, String> readAllProperties() throws FileNotFoundException, IOException {
        //保存所有的键值
        Map<String, String> map = new HashMap<String, String>();
        Enumeration en = props.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = props.getProperty(key);
            map.put(key, value);
        }
        return map;
    }

    private static String longToIp(Long number) {
        String ip = "";
        for (int i = 3; i >= 0; i--) {
            long tmp = number >> (8 * i);
            ip += String.valueOf((tmp & 0xff));
            if (i != 0) {
                ip += ".";
            }
        }
        return ip;
    }

    /**
     * 设置某个key的值,并保存至文件。
     * by https://blog.csdn.net/qq_40147276/article/details/91976753
     *
     * @param key key值
     * @return key 键对应的值
     * @throws IOException
     */
    public void setValue(String key, String value) throws IOException {
        Properties prop = new Properties();
        InputStreamReader fis = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(this.configPath), "utf-8");
        // 从输入流中读取属性列表(键和元素对)
        prop.load(fis);
        // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
        // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("ip-black.properties"), "utf-8");
        prop.setProperty(key, value);
        // 以适合使用 load 方法加载到 Properties 表中的格式,
        // 将此 Properties 表中的属性列表(键和元素对)写入输出流
        prop.store(writer, "last update");
        //关闭文件
        fis.close();
        writer.close();
    }

    /**
     * @return the props
     */
    public Properties getProps() {
        return props;
    }
}

导语

创建一个 ip-black.properties 放在 resources 目录下

代码语言:javascript
复制
# ip黑名单设置  需要把IP转成Long类型
236000768~236001023
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年05月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建IP拦截器
  • 往webMVC里添加拦截器组件
  • 使用的工具类
  • 导语
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档