HttpCharacterResponseWrapper.Java
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class HttpCharacterResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter charArrayWriter = new CharArrayWriter();
public HttpCharacterResponseWrapper(HttpServletResponse response) {
super(response);
// TODO Auto-generated constructor stub
}
@Override
public PrintWriter getWriter() throws IOException {
// TODO Auto-generated method stub
return new PrintWriter(charArrayWriter);
}
public CharArrayWriter getCharArrayWriter() {
return charArrayWriter;
}
}
OutoutReplaceFilter.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class OutoutReplaceFilter implements Filter {
private Properties pp = new Properties();
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");
// OutputStream out = null;
HttpCharacterResponseWrapper response = new HttpCharacterResponseWrapper(
(HttpServletResponse) res);
response.setCharacterEncoding("utf-8");
PrintWriter out = res.getWriter();
chain.doFilter((ServletRequest) req, response);
String output = response.getCharArrayWriter().toString();
for (Object obj : pp.keySet()) {
String key = (String) obj;
output = output.replace(key, pp.getProperty(key));
}
out.write(output);
// out = res.getOutputStream();
// out.write(output.getBytes());
// out.println("<!-- Generated at " + new java.util.Date() + " -->");
// out.close();
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
String file = config.getInitParameter("file");
String realPath = config.getServletContext().getRealPath(file);
try {
pp.load(new FileInputStream(realPath));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sensitive.properties
# 自动更正
Chna = China
www.linfenliang.cn = www.linfenliang.cn.vc
#自动替换
\色\情 = **
\情\色=**
\赌\博=**
TMD=***
web.xml
<!-- 敏感字替换器 -->
<filter>
<filter-name>OutoutReplaceFilter</filter-name>
<filter-class>org.com.util.OutoutReplaceFilter</filter-class>
<init-param>
<param-name>file</param-name>
<param-value>/WEB-INF/sensitive.properties</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OutoutReplaceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>