java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap
在拦截器中,拦截请求参数,对参数进行处理,避免跨域注入漏洞等问题;
将request中的参数 进行替换时出现的错误
Map<String, String[]> params = request.getParameterMap();
if (params.size() > 0) {
for (String key : params.keySet()) {
String[] vlaue = params.get(key);
vlaue[0] = HTMLEncode(vlaue[0]);
params.put(key,vlaue);
}
}
修改如下:
Map<String, String[]> params = new HashMap(request.getParameterMap());
if (params.size() > 0) {
for (String key : params.keySet()) {
String[] vlaue = params.get(key);
vlaue[0] = HTMLEncode(vlaue[0]);
params.put(key,vlaue);
}
}
public static String HTMLEncode(String aText){
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(aText);
char character = iterator.current();
while (character != CharacterIterator.DONE ){
if (character == '<') {
result.append("<");
}
else if (character == '>') {
result.append(">");
}
else if (character == '&') {
result.append("&");
}
else if (character == '\"') {
result.append(""");
}
else {
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
总结:在java中,对象直接进行等于操作的时候会关联原对象 原对象不可以进行修改 新参数也无法进行修改