##Servlet中有獲取Request參數的方法,而Nutz中也有重寫類似的方法,我們只要知道它如何得到RequestMap就可以處理請求中的參數,進而對它進行處理。
- 在Nutz項目中的MainModule中配置你寫的類(如AnyMobileActionFilter.class);
- 把需要過濾的字符寫在配置文件中(如:SCFilter.properties).
- 編寫AnyMobileActionFilter 它是記住要實現ActionFilter,這個是Nutz中的對請求統一處理的過濾器;
一、MainModule中配置過濾器:
@Filters({@By(type=AnyMobileActionFilter.class)})
public class MainModule {
}
二、在配置文件中配置需要過濾的字符:
SCFilter.properties
#有多個參數請用"|"號隔開,可以有多個KEY=Value
#Key的名字隨意取
NAME=滾犢子
HELLO=go and fuck youself|fuck
SPEAK=不要說髒話
三、編寫AnyMobileActionFilter:
package com.carforu.web.filter;
import java.text.Normalizer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;
import org.nutz.mvc.view.ForwardView;
public class AnyMobileActionFilter implements ActionFilter {
/**
* 遍歷當前參數
*
* @param Map
* <String, String[]> 獲取的內容
* @return bool boolean
*/
public boolean SCFCheck(Map<String, String[]> originalQueryString) {
boolean bool = true;
if (originalQueryString != null) {
for (String key : (Set<String>) originalQueryString.keySet()) {
if (bool != false) {
String[] rawVals = originalQueryString.get(key);
for (int i = 0; i < rawVals.length; i++) {
bool = stripXSS(rawVals[i]);
if(bool == false) break;
}
bool = stripXSS(key);
if(bool == false) break;
} else {
break;
}
}
}
return bool;
}
/**
* 判斷是否匹配
*
* @param value
* 當前要匹配內容
* @return bool boolean
*/
private boolean stripXSS(String value) {
String cleanValue = null;
Boolean bool = true;
Ioc ioc = Mvcs.getIoc();
PropertiesProxy scfilter = ioc.get(PropertiesProxy.class, "scfilter");
if (value != null) {
cleanValue = Normalizer.normalize(value, Normalizer.Form.NFD);
List<String> fkeys = scfilter.getKeys();
for (String fk : fkeys) {
if (bool != false) {
String scfvalue = scfilter.get(fk);
String[] propertiesList = scfvalue.split("\\|");
for (int i = 0; i < propertiesList.length; i++) {
/*Pattern scfpattern = Pattern.compile(propertiesList[i],
Pattern.CASE_INSENSITIVE);
Matcher m = scfpattern.matcher(cleanValue);*/
int index = cleanValue.indexOf(propertiesList[i]);
if (index != -1) {
bool = false;
break;
}else{
bool = true;
}
}
}else{
break;
}
}
}
return bool;
}
@Override
public View match(ActionContext ac) {
Map<String, String[]> originalQueryString = ac.getRequest()
.getParameterMap();
boolean bool = this.SCFCheck(originalQueryString);
if (bool) {
return null;
} else {
return new ForwardView("/unsafeCode.html");
}
}
}