过滤器

在项目开发中需要对string 类型的输入进行非法代码过滤于是有了 这段 字符串的过滤
public class StringFilter {

/**
* 过滤<, >,\n 字符的方法。
* @param input 需要过滤的字符
* @return 完成过滤以后的字符串
*/
public static String filterHtml(String input) {

input = input.replaceAll("&", "&");
input = input.replaceAll("<", "<");
input = input.replaceAll(">", ">");
input = input.replaceAll(" ", " ");
input = input.replaceAll("'", "'");
input = input.replaceAll("\"", """);

return input.replaceAll("\n", "<br>");
}
}



或许对 struts 不够了解. 不知道其中有更好的方法
但是 对于风装好的 form 以及 pojo 层 对象的过滤, 上面 的还是无能为力
于是乎 有了下面对 对象的过滤,
public class SuperFilter {
private Object obj; // 传入的对象
private List<String> Methodname = new ArrayList<String>(); // 方法名 与get 方法对象
private Map<String, Mobj> map = new HashMap<String, Mobj>(); // key 为
// 方法名列表的
// 每个值 value
// 为封装了与方法名对应的get
// set
// 方法的对象
private List<Method> getList = new ArrayList<Method>(); // get 方法列表
private List<Method> setList = new ArrayList<Method>();// set 方法列表

public SuperFilter(Object obj, Class<?> cla) {
this.obj = obj;
String style = cla.getName();
MethodSorter(obj, style);
MethodFilter();
}

public SuperFilter(Object obj, String style) {
this.obj = obj;
MethodSorter(obj, style);
MethodFilter();
}

private void MethodSorter(Object o, String name) {
Method methods[] = null;
try {
methods = Class.forName(name).getDeclaredMethods();
} catch (SecurityException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < methods.length; i++) {
Method m = methods[i];
if (m.getName().startsWith("get")) {
getList.add(m);
String tempname = m.getName();
Methodname.add(tempname.substring(3));
}
if (m.getName().startsWith("set")) {
setList.add(m);
}

}

for (int i = 0; i < Methodname.size(); i++) {
String temp = Methodname.get(i);
for (int m = 0; m < setList.size(); m++) {
Method met = setList.get(m);
if (met.getName().endsWith(temp)) {

map.put(temp, new Mobj(getList.get(i), met));

}
}
}

}

public void MethodFilter() {
for (int i = 0; i < Methodname.size(); i++) {
Mobj mobj = map.get(Methodname.get(i));
Method methodget = mobj.getGetMethod();
Method methodset = mobj.getSetMethod();

try {
if (String.class.isInstance(methodget.invoke(obj, null))) {

String temp = (String) methodget.invoke(obj, null);
// System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println(temp);
// System.out.println("~~~~~~~~~~~~~~~~~~");
String tempAfterFilter = StringFilter.filterHtml(temp);
methodset.invoke(obj, tempAfterFilter);

}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

}

}

protected class Mobj {
Method getMethod;
Method setMethod;

public Mobj(Method getMethod, Method setMethod) {
this.getMethod = getMethod;
this.setMethod = setMethod;
}

public Method getSetMethod() {
return setMethod;
}

public void setSetMethod(Method setMethod) {
this.setMethod = setMethod;
}

public Method getGetMethod() {
return getMethod;
}

public void setGetMethod(Method getMethod) {
this.getMethod = getMethod;
}

}

}

思路:
1 通过传入 对象 ,对象类型名, 获取出 对象自己的 getter and setter ,
并将其对应到 map 中
2 判断 其get 方法 的返回值类型 若是 string 类型 则调用 对应属性的getter
3 将得到的 字符串 通过StringFilter 进行过滤后 ,调用对象的setter 将其保存到对象中 完成过滤
使用方法:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 传入的form 表单 转换为对应类型
Login loginForm = (Login) form;

// 进行过滤
new SuperFilter(loginForm,loginForm.getClass());
................... return mapping.getInputForward();



}
}
该过滤操作完全是针对风装好的对象 (例如 form bean) 进行字符过滤 可以通过对 stringFilter 的 相应配置 实现 特定字符的 转换任务

(*^__^*) 一个过滤 就这样完成了.. 其中不足 还希望大家给予指正,感激不尽
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值