国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

過濾器過濾敏感詞匯的代碼

2020-05-18 10:14 作者:小垃圾kiki  | 我要投稿
package cn.itcast.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.BufferedReader;
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 SensitiveWordsFilter implements Filter {


??? public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
??????? //1.創(chuàng)建代理對(duì)象,增強(qiáng)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 {
??????????????? //增強(qiáng)getParameter方法
??????????????? //判斷是否是getParameter方法
??????????????? if(method.getName().equals("getParameter")){
??????????????????? //增強(qiáng)返回值
??????????????????? //獲取返回值
??????????????????? 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

??????????????? //判斷方法名是否是 getParameterValue

??????????????? return method.invoke(req,args);//如果不是的話傳遞真實(shí)對(duì)象傳遞args數(shù)組
??????????? }
??????? });

??????? //2.放行
??????? chain.doFilter(proxy_req, resp);
??? }
??? private List<String> list = new ArrayList<String>();//敏感詞匯集合
?? ?
?? ?
??? public void init(FilterConfig config) throws ServletException {

??????? try{
??????????? //1.獲取文件真實(shí)路徑
??????????? 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() {
??? }

}

過濾器過濾敏感詞匯的代碼的評(píng)論 (共 條)

分享到微博請遵守國家法律
聂拉木县| 沭阳县| 天水市| 揭阳市| 平山县| 偃师市| 绥阳县| 万宁市| 阿合奇县| 南陵县| 资中县| 阿巴嘎旗| 石狮市| 宁南县| 天津市| 漳浦县| 乐业县| 股票| 西盟| 张家港市| 东兴市| 汉阴县| 托里县| 肃南| 昭苏县| 行唐县| 庆元县| 诏安县| 望城县| 年辖:市辖区| 利辛县| 平顺县| 邯郸县| 永登县| 南涧| 海口市| 抚州市| 津南区| 维西| 武山县| 潞西市|