Filter过滤非法字符
程序员文章站
2022-03-10 13:44:43
...
package com.pcm.web.pubs.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
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;
/**
* @version 1.0
* @author
*/
public class CharFilter implements Filter {
private List names = new ArrayList();
private List values = new ArrayList();
/**
* @see javax.servlet.Filter#void ()
*/
public void destroy() {
}
/**
* @see javax.servlet.Filter#void (javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(
ServletRequest req,
ServletResponse res,
FilterChain chain)
throws ServletException, IOException {
int skipNum = 0;
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
Map map = request.getParameterMap();
Set keySet = map.keySet();
StringBuffer url = request.getRequestURL().append("?");
for (Iterator it = keySet.iterator(); it.hasNext();) {
String name = it.next().toString();
String value = request.getParameter(name);
//=======================开始过滤字符=====================================
if (value != null) {
System.out.println(
" ================== " + names + " " + values);
boolean bTrim = !value.equals(value.trim());
//两端空格
if (bTrim) {
value = value.trim();
skipNum++;
}
StringBuffer tmp = new StringBuffer(value);
boolean bSkip = false;
for (int i = 0; i < names.size(); i++) {
if (tmp.toString().indexOf((String) names.get(i)) != -1) {
bSkip = true;
break;
}
}
if (bSkip) {
//这里比较麻烦,是因为我用的jdk 1.3
//如果使用jdk 1.4以上版本,直接使用replaceAll()方法,即可替换所有的字符
//jdk 1.4 替换' 为 "" : value.replaceAll("\'","\"") ,够简单吧
for (int i = 0; i < tmp.length(); i++) {
char tmpChar = tmp.charAt(i);
for (int j = 0; j < names.size(); j++) {
if (String
.valueOf(tmpChar)
.equalsIgnoreCase(names.get(j).toString())) {
tmp.replace(i, i + 1, values.get(j).toString());
}
}
}
skipNum++;
value = tmp.toString();
}
//单引号
boolean bYinHao = tmp.toString().indexOf("\'") != -1;
if (bYinHao) {
for (int i = 0; i < url.length(); i++) {
if (tmp.charAt(i) == '\'') {
tmp.replace(i, i + 1, "\"");
}
}
skipNum++;
value = tmp.toString();
}
}
//=======================================================================
url.append(name).append("=").append(value).append("&");
}
if (skipNum > 0) {
System.out.println(url);
String urlStr = url.toString();
//注意,这里是要把编码再从gb2312倒转为ISO8859-1,因为表单参数传递使用的是ISO8859-1编码
urlStr = new String(urlStr.getBytes("gb2312"), "ISO8859-1");
response.sendRedirect(urlStr);
} else {
chain.doFilter(request, response);
}
}
/**
* Method init.
* @param config
* @throws javax.servlet.ServletException
*/
public void init(FilterConfig config) throws ServletException {
String charName = config.getInitParameter("charName");
String charValue = config.getInitParameter("charValue");
//这里比较麻烦,是因为我使用的是jdk1.3
//如果jdk 1.4以上, String[] names=charName.spilt(",");
//就可以把字符串按指定分割符分割为数组
StringTokenizer stkName = new StringTokenizer(charName, ",");
while (stkName.hasMoreTokens()) {
names.add(stkName.nextToken());
}
StringTokenizer stkValue = new StringTokenizer(charValue, ",");
while (stkValue.hasMoreTokens()) {
values.add(stkValue.nextToken());
}
}
}
2.web.xml配置filter
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>TclRender</display-name>
<filter>
<filter-name>CharFilter</filter-name>
<display-name>CharFilter</display-name>
<filter-class>com.pcm.web.pubs.servlet.CharFilter</filter-class>
<init-param>
<param-name>charName</param-name>
<param-value>',^</param-value> <!-- , 为分隔符 -->
</init-param>
<init-param>
<param-name>charValue</param-name>
<param-value>",~</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
下一篇: 小顶堆排序Java代码样例
推荐阅读
-
php过滤所有的空白字符(空格、全角空格、换行等),全角换行_PHP教程
-
php过滤所有恶意字符(批量过滤post,get敏感数据)_PHP教程
-
ant编译java报“非法字符: \65279 ”错误的解决方法 z
-
php stripslashes() 函数删除、过滤字符串中的反斜杠
-
PHP字符串特殊符号的过滤方法
-
php过滤html字符串,防止SQL注入的实例代码
-
python 正则表达式 过滤字符串两边的字符,类似strip功能 练习题
-
Android编程之TextView的字符过滤功能分析
-
Java过滤器filter_动力节点Java学院整理
-
基于tomcat8 编写字符编码Filter过滤器无效问题的解决方法