非法字符过滤 HttpServletResponse
程序员文章站
2024-02-08 18:14:04
...
在页面显示前对将页面非法符过滤掉
IllegalCharacterFilter.java
IllegalWordUtil.java
web.xml
IllegalCharacterFilter.java
package *;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import *.IllegalWordUtil;
public class IllegalCharacterFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest servletrequest = (HttpServletRequest) request;
HttpServletResponse servletresponse = (HttpServletResponse) response;
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
WrapperImpl wrapper = new WrapperImpl(servletresponse);
chain.doFilter(request, wrapper);
PrintWriter out = response.getWriter();
out.println(IllegalWordUtil.filterIllegalWords(wrapper.toString()));
}
@Override
public void init(FilterConfig config) throws ServletException {
}
private class WrapperImpl extends HttpServletResponseWrapper {
private CharArrayWriter output;
public WrapperImpl(HttpServletResponse response) {
super(response);
output = new CharArrayWriter();
}
public String toString() {
return output.toString();
}
public PrintWriter getWriter() {
return new PrintWriter(output);
}
}
}
IllegalWordUtil.java
package *;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IllegalWordUtil{
protected static final Logger logger = LoggerFactory.getLogger(IllegalWordUtil.class);
private final static String pattern;
private final static String replacement;
private final static String illegalWords;
private final static String illegalWordRegx;
private static Pattern patterns = null;
static{
Properties properties = new Properties();
try{
properties.load(IllegalWordUtil.class.getResourceAsStream("/illegal.keywords.properties"));//加载非法关键字资源文件
}catch(IOException ioe){
ioe.printStackTrace();
logger.error("Cound not load illegal.keywords.properties");
throw new RuntimeException("Cound not load illegal.keywords.properties");
}
//取出所有非法关键字
illegalWords = properties.getProperty("words", "");
//以,为分格符取出每个非法关键字
pattern = properties.getProperty("pattern", ",");
//将非法字符替换成***
replacement = properties.getProperty("replacerex", "***");;
illegalWordRegx = "(" + illegalWords.trim().replaceAll(pattern, "|") + ")";
patterns = Pattern.compile(new String(illegalWordRegx.toString()));
}
public static String filterIllegalWords(String sentence) {
Matcher m = patterns.matcher(sentence);
return m.replaceAll(replacement);
}
public static boolean isIllegalWord(String word) {
Matcher m = patterns.matcher(word);
return m.find(0);
}
}
web.xml
<filter>
<filter-name>IllegalCharacterFilter</filter-name>
<filter-class>desirelist.web.servlet.IllegalCharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
上一篇: 非法字符过滤 HttpServletRequest
下一篇: JAVA 过滤非法字符(积累)