有很多的数据,更新的频率很低,比如中国行政区划,为避免每次都要从数据库查询,因此可把这些数据放在中,提高系统响应速度。
这种功能可以在很多的地方实现,也可以在过滤器中完成。
我们在上一节说了敏感词过滤器,每一次数据都要读取文件,好像效率稍微低了点,我们把这个加到缓存中吧。
1、JAVA实现利用过滤器实现敏感信息过滤
过滤器的JAVA代码:
package com.filter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletRequestWrapper;
importjavax.servlet.http.HttpServletResponse;
/**
*一个简单的敏感词过滤器,这里针对从GET的方式做了转码,如果用POST方式,将ISO8859-1字符转换为UTF-8下面一段要注释掉
*将敏感词表加载到内存中,提高过滤的效率
*
*@author 范芳铭
*/
public class EasyDirtyFilter2 implementsFilter {
List<String>dirtyWords = null;
privateFilterConfig config = null;
@Override
publicvoid init(FilterConfig filterConfig) throws ServletException {
System.out.println("----过滤器初始化----");
this.config= filterConfig;
//在初始化就进行加载到内存
dirtyWords= getDirtyWords();
//一些常用的不变的参数都可以在这里进行初始化,但是加载太多内容会影响启动速度
}
//过滤器功能在这里实现
@Override
publicvoid doFilter(ServletRequest req, ServletResponse resp,
FilterChainchain) throws IOException, ServletException {
HttpServletRequestrequest = (HttpServletRequest) req;
HttpServletResponseresponse = (HttpServletResponse) resp;
Stringcharset = "UTF-8";
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+ charset);
DirtyRequestdirtyreq = new DirtyRequest(request);
chain.doFilter(dirtyreq,response);
}
@Override
publicvoid destroy() {
System.out.println("----过滤器销毁----");
}
privateList<String> getDirtyWords() {
List<String>dirtyWords = new ArrayList<String>();
StringdirtyWordPath = config.getInitParameter("dirtyword");
InputStreaminputStream = config.getServletContext()
.getResourceAsStream(dirtyWordPath);
InputStreamReaderis = null;
try{
is= new InputStreamReader(inputStream, "UTF-8");
}catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
BufferedReaderreader = new BufferedReader(is);
Stringline;
try{
while((line = reader.readLine()) != null) {// 如果 line为空说明读完了
dirtyWords.add(line);
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("敏感词文件加载完成");
returndirtyWords;
}
//使用Decorator模式包装request对象,实现敏感字符过滤功能
classDirtyRequest extends HttpServletRequestWrapper {
privateHttpServletRequest request;
publicDirtyRequest(HttpServletRequest request) {
super(request);
this.request= request;
}
//重写getParameter方法,实现对敏感字符的过滤
@Override
publicString getParameter(String name) {
Stringvalue = this.request.getParameter(name);
//如果get的方式提交表单,通过request.setCharacterEncoding("UTF-8");这种方式是解决不了中文乱码问题
//参考:http://blog.csdn.net/ffm83/article/details/43229819
if(value == null) {
returnnull;
}
//将ISO8859-1字符转换为UTF-8
try{
value= new String(value.getBytes("ISO8859-1"), "UTF-8");
}catch (UnsupportedEncodingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
if(dirtyWords == null) {
dirtyWords= getDirtyWords(); //如果为空,重新加载
}
for(String dirtyWord : dirtyWords) {
if(value.contains(dirtyWord)) {
System.out.println("内容中包含敏感词:" + dirtyWord + ",将会被替换成****");
//替换敏感字符
value= value.replace(dirtyWord, "****");
}
}
returnvalue;
}
}
}
2、将过滤器添加到Web.xml
<!--配置过滤器 -->
<filter>
<filter-name>easyFilter</filter-name>
<filter-class>com.filter.EasyDirtyFilter2</filter-class>
<!-- 配置要过滤的敏感字符文件 -->
<init-param>
<param-name>dirtyword</param-name>
<param-value>/WEB-INF/dirtyword.txt</param-value>
</init-param>
</filter>
<!--映射过滤器 -->
<filter-mapping>
<filter-name>easyFilter</filter-name>
<!--“/*”表示拦截所有的请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dirty</servlet-name>
<servlet-class>com.servlet.RequestDirty</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dirty</servlet-name>
<url-pattern>/dirty</url-pattern>
</servlet-mapping>
其他和敏感词过滤一章一样。
原文地址:http://blog.csdn.net/ffm83/article/details/43530381