标签:ini null ide weight add res 获取 file style
package com.hopetesting.web.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 newcityman
* @date 2019/9/13 - 19:41
*/
@WebFilter("/*")
public class SensitiveWordsFilter implements Filter {
public void destroy() {
}
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 str : list) {
if (value.contains(str)) {
value = value.replace(str, "******");
}
}
}
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 br = new BufferedReader(new FileReader(realPath));
//3、将文件的每一行读取到list中
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:ini null ide weight add res 获取 file style
原文地址:https://www.cnblogs.com/newcityboy/p/11517618.html