• JavaWeb 之 Filter 敏感词汇过滤案例


    需求:

      1、 对day17_case案例录入的数据进行敏感词汇过滤
      2、 敏感词汇参考 src路径下的《敏感词汇.txt》
      3、 如果是敏感词汇,替换为 ***

    分析:

      1、 对request对象进行增强。增强获取参数相关方法
      2、 放行。传递代理对象

    代码实现:

     1 import org.springframework.cglib.proxy.InvocationHandler;
     2 import org.springframework.cglib.proxy.Proxy;
     3 
     4 import javax.servlet.*;
     5 import javax.servlet.annotation.WebFilter;
     6 import java.io.BufferedReader;
     7 import java.io.FileReader;
     8 import java.io.IOException;
     9 import java.lang.reflect.Method;
    10 import java.util.ArrayList;
    11 import java.util.List;
    12 
    13 /**
    14  * 敏感词汇过滤器
    15  */
    16 @WebFilter("/*")
    17 public class SensitiveWordsFilter implements Filter {
    18 
    19 
    20     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    21         // 1 创建代理对象,增强 getparameter 方法
    22         ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
    23             @Override
    24             public Object invoke(Object o, Method method, Object[] args) throws Throwable {
    25 
    26                 // 增强 getparameter 方法
    27                 // 判断是否是该方法
    28                 if(method.getName().equals("getParameter")) {
    29                     // 增强返回值
    30                     // 获取返回值
    31                     String value = (String) method.invoke(req,args);
    32 
    33                     if(value != null) {
    34                         for (String str : list) {
    35                             if(value.contains(str)){
    36                                 value = value.replaceAll(str,"***");
    37                             }
    38                         }
    39                     }
    40 
    41                     return  value;
    42                 }
    43 
    44                 return method.invoke(req,args);
    45             }
    46         });
    47         // 2 放行,传递增强的代理对象
    48 
    49         chain.doFilter(proxy_req, resp);
    50     }
    51 
    52 
    53     private List<String> list = new ArrayList<String>();  // 敏感词汇集合
    54     public void init(FilterConfig config) throws ServletException {
    55 
    56 
    57         try {
    58             // 1 加载文件
    59             // 获取文件的真实路径
    60             ServletContext servletContext = config.getServletContext();
    61             String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");
    62             // 2 读取文件
    63 
    64             BufferedReader br = new BufferedReader(new FileReader(realPath));
    65 
    66             // 3 将文件的每一行添加到 list 中
    67 
    68             String line = null;
    69             while((line =  br.readLine()) != null) {
    70                 list.add(line);
    71             }
    72 
    73             br.close();
    74 
    75             System.out.println(list);
    76         } catch (Exception e) {
    77             e.printStackTrace();
    78         }
    79 
    80     }
    81 
    82 
    83     public void destroy() {
    84     }
    85 
    86 }
  • 相关阅读:
    String substring(int start,int end)截取当前字符串中指定范围内的字符串
    String字符串是不变对象,内容一旦创建不可改变,若改变一定会创建新对象
    StringBuilder修改字符串内容,增,删,改,插
    startsWith(),endsWith()判断当前字符串是否是以给定字符串开始或结尾的
    length()返回当前字符串的字符个数
    indexOf()字符位置
    charAt()检测回文
    手把手教你猜字小游戏
    release 和 drain
    c专家编程 ,c语言运行时环境
  • 原文地址:https://www.cnblogs.com/niujifei/p/11628109.html
Copyright © 2020-2023  润新知