Java实现过滤敏感词汇:
使用动态代理模式,增强返回值类型,替换敏感词汇
代码如下:
1、敏感词汇过滤器
package com.it.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;
/**
* @简单描述:敏感词汇过滤器
* @Author:
* @Date: 2022年03月16日 19时42分39秒
*/
@WebFilter("/*")
public class SensitiveFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//1、创建代理对象,增强getParameter方法
ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//增强getParameter方法
//判断是否有getParameter方法
if (method.getName().equals("getParameter")){
//增强返回值
//获取返回值
String value = (String) method.invoke(req, args);
if (value!=null){
for (String s : list) {
if (value.contains(s)){
value = value.replaceAll(s,"***");
}
}
}
return value;
}
return method.invoke(req, args);
}
});
chain.doFilter(proxy_req, resp);
}
//敏感词汇集合
private List<String> list = new ArrayList<String>();
public void init(FilterConfig config) throws ServletException {
try {
//1、加载文件,获取文件真实路径
ServletContext servletContext = config.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");
//2、读取文件
BufferedReader bufferedReader = new BufferedReader(new FileReader(realPath));
//3、将文件中的每一行数据添加到list集合中
String line = null ;
while ((line = bufferedReader.readLine()) != null){
list.add(line);
}
bufferedReader.close();
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void destroy() {
}
}
2、测试所需servlet
package com.it.controller;
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;
/**
* @简单描述:测试所需servlet
* @Author:
* @Date: 2022年03月16日 20时03分21秒
*/
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String msg = request.getParameter("msg");
String info = name+":"+msg ;
System.out.println(info);
request.setAttribute("info",info);
request.getRequestDispatcher("he.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
3、所需的前端界面:he.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" >
<span>×</span></button>
<strong>${info}</strong>
</div>
</body>
</html>
4、直接在地址栏输入:
http://localhost:8080/userjdbc/testServlet?name=测试&msg=(你要输入的敏感词汇)
5、运行结果如下:
前端界面显示: