Filter的敏感词汇过滤案例
生活随笔
收集整理的這篇文章主要介紹了
Filter的敏感词汇过滤案例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
需求:
1. 對(duì)day17_case案例錄入的數(shù)據(jù)進(jìn)行敏感詞匯過(guò)濾
2. 敏感詞匯參考《敏感詞匯.txt》
3. 如果是敏感詞匯,替換為 ***
SensitiveWordsFilter:
package cn.itcast.web.filter;import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List;/*** 敏感詞匯過(guò)濾器*/ @WebFilter("/*") public class SensitiveWordsFilter implements Filter {// 敏感詞匯集合private List<String> list = new ArrayList<String>();@Overridepublic void init(FilterConfig filterConfig) throws ServletException {BufferedReader br = null;try {// 1.加載文件真實(shí)路徑ServletContext servletContext = filterConfig.getServletContext();// 獲取src下的敏感詞匯// 該處realPath可以使用getResourcesAsStream獲取String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感詞匯.txt");// 2.讀取文件br = new BufferedReader(new FileReader(realPath));// 3.將文件的每一行數(shù)據(jù)添加到list中String line = null;while ((line = br.readLine()) != null) {list.add(line);}} catch (Exception e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println(list);}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {// 1.創(chuàng)建代理對(duì)象,ServletRequest proxy_request = (ServletRequest) Proxy.newProxyInstance(servletRequest.getClass().getClassLoader(),servletRequest.getClass().getInterfaces(),new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// 增強(qiáng)getParameter方法// 判斷是否是getParameter方法if ("getParameter".equals(method.getName())) {// 使用真實(shí)對(duì)象request,獲取前端請(qǐng)求中返回的參數(shù)并增強(qiáng)該參數(shù)String value = (String) method.invoke(servletRequest, args);if (value != null) {// 遍歷文件配置文件中獲取的敏感詞匯for (String str : list) {if (value.contains(str)) {value = value.replaceAll(str, "***");}}}// 返回增強(qiáng)后的返回值為替換了敏感詞匯的返回值return value;}// 判斷方法是否是getParameterMap// 需要實(shí)現(xiàn)// 判斷方法是否是getParameterValue// 需要實(shí)現(xiàn)// getParameter方法以外的方法正常執(zhí)行return method.invoke(servletRequest, args);}});// 2.放行filterChain.doFilter(proxy_request, servletResponse);}@Overridepublic void destroy() {} }TestServlet:
package cn.itcast.web.servlet;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet("/testServlet") public class TestServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 從前臺(tái)輸入tomcat中設(shè)定好的url以及name參數(shù)和msg參數(shù)// msg的值或name的值可以使用配置文件中的敏感詞匯String name = request.getParameter("name");String msg = request.getParameter("msg");// 敏感詞匯將替換成 張三:你個(gè)***你個(gè)大***System.out.println(name+":"+msg);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);} }敏感詞匯.txt 該文件在src下
笨蛋 壞蛋 其他敏感詞匯可以自行添加總結(jié)
以上是生活随笔為你收集整理的Filter的敏感词汇过滤案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysqlnavicat数据库备份与恢复
- 下一篇: 计算机加入域 不能访问网络位置 解决办法