java敏感词汇过滤工具类
生活随笔
收集整理的這篇文章主要介紹了
java敏感词汇过滤工具类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** 敏感詞匯過濾器*/
@WebFilter("/*")
public class SensitiveWordsFilter implements Filter {public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {//1.創(chuàng)建代理對象,增強getParameter方法ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic 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 str : list) {if(value.contains(str)){value = value.replaceAll(str,"***");}}}return value;}//判斷方法名是否是 getParameterMap//判斷方法名是否是 getParameterValuereturn method.invoke(req,args);}});//2.放行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 br = new BufferedReader(new FileReader(realPath));//3.將文件的每一行數(shù)據(jù)添加到list中String line = null;while((line = br.readLine())!=null){list.add(line);}br.close();System.out.println(list);}catch (Exception e){e.printStackTrace();}}public void destroy() {}}
總結(jié)
以上是生活随笔為你收集整理的java敏感词汇过滤工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS:真机调试
- 下一篇: [设计模式]中介者模式之Events消息