JavaWeb中的filter過濾敏感詞匯案例詳解
需求:對(duì)含有bad.txt中的敏感詞匯的句子進(jìn)行替換,替換成星號(hào)
bad.txt放在src目錄下
笨蛋
壞蛋
FilterDemo
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;
//過濾敏感詞匯
@WebFilter("/*")
public class FilterDemo8 implements Filter {
//讀取txt文件時(shí)將詞匯存入列表
private List<String> list = new ArrayList<String>();
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//通過代理增強(qiáng)方法
ServletRequest proxy = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//判斷方法是否為getParameter
if (method.getName().equals("getParameter")){
String obj = (String) method.invoke(req, args);
if (obj != null){
//遍歷后替換詞匯
for (String s : list) {
if (obj.contains(s)){
obj = obj.replaceAll(s,"***");
}
}
}
return obj;
}
return method.invoke(req,args);
}
});
//放行
chain.doFilter(proxy,resp);
}
public void init(FilterConfig config) throws ServletException {
//讀取文件
ServletContext context = config.getServletContext();
//src下的文件真實(shí)位置是在WEN-INF/classes下的
String realPath = context.getRealPath("/WEB-INF/classes/bad.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(realPath));
String line = null;
while ((line = reader.readLine()) != null){
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}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("/servletDemo")
public class ServletDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String what = request.getParameter("what");
System.out.println(name + " : " + what);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}到此這篇關(guān)于JavaWeb中的filter過濾敏感詞匯案例的文章就介紹到這了,更多相關(guān)JavaWeb filter過濾敏感詞匯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot項(xiàng)目與vue項(xiàng)目整合打包的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot項(xiàng)目與vue項(xiàng)目整合打包的實(shí)現(xiàn)方式,本文通過兩種方式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Spring boot + mybatis + Vue.js 
這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(二),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
Java?C++題解leetcode769最多能完成排序的塊
這篇文章主要為大家介紹了Java?C++題解leetcode769最多能完成排序的塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java調(diào)用IK分詞器進(jìn)行分詞方式,封裝工具類
這篇文章主要介紹了Java調(diào)用IK分詞器進(jìn)行分詞方式,封裝工具類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請(qǐng)求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03

