最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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)論 (共 條)

分享到微博請遵守國家法律
翼城县| 嫩江县| 华阴市| 彭山县| 江口县| 桃江县| 和平县| 天水市| 宜宾县| 张北县| 大安市| 桐庐县| 澜沧| 叙永县| 永康市| 阜平县| 西乌珠穆沁旗| 蓬安县| 中阳县| 霍邱县| 咸阳市| 青冈县| 隆安县| 屏南县| 武功县| 紫阳县| 双辽市| 图木舒克市| 措美县| 辽阳市| 行唐县| 类乌齐县| 四平市| 克什克腾旗| 阿拉尔市| 张家港市| 商水县| 黄大仙区| 东台市| 寻乌县| 三门峡市|