您的位置:首页 > 编程语言

输入框输入特殊字符导致页面代码暴露的解决方案

2017-12-26 15:51 549 查看
使用的页面是 velocity页面,解决问题的思路是:在页面显示的时候用特殊字符对用的unicode编码转换下

1.编写一个处理乱码的工具类

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class VelocityEscapeUtil {
/**
* 时间: 2017年7月17日 上午10:25:19</br>
* 版本: v1.0.0</br>
* 描述: json形式的字符串转义</br>
* 方法名: jsonEncode</br>
*/
private static String jsonEncode(String s) {
if (s == null || s.isEmpty()) {
return s;
}

s = s.replace(">", ">");
s = s.replace("<", "<");
s = s.replace("\'", "'");
s = s.replace("\\\\", "\");
s = s.replace("\\\"", """);
s = s.replace("/", "/");
s = s.replace("(", "(");
s = s.replace(")", ")");

return s;
}

/**
*
* 时间: 2017年7月17日 上午10:25:48</br>
* 版本: v1.0.0</br>
* 描述: 字符串转义</br>
* 方法名: strEncode</br>
*/
private static String strEncode(String s) {
if (s == null || s.isEmpty()) {
return s;
}
s = s.replace(">", ">");
s = s.replace("<", "<");
s = s.replace("\'", "'");
s = s.replace("\"", """);
s = s.replace("/", "/");
s = s.replace("(", "(");
s = s.replace(")", ")");
return s;
}

/**
*
* 时间: 2017年7月17日 上午10:26:04</br>
* 版本: v1.0.0</br>
* 描述: 字符转义</br>
* 方法名: charEncode</br>
*/
private static String charEncode(char c) {
String s = String.valueOf(c);
switch (c) {
case '>':
s = ">";// 实体编号大于号
break;
case '<':
s = "<";// 实体编号小于号
break;
case '\'':
s = "'";// 实体编号单引号
break;
case '\"':
s = """;// 实体编号双引号
break;
case '\\':
s = "\";// 实体编号斜线
break;
case '/':
s = "/";// 实体编号斜线
break;
case '(':
s = "(";// 实体编号(号
break;
case ')':
s = ")";// 实体编号)号
break;
default:
break;
}
return s;
}

/**
*
* 时间: 2017年7月17日 上午10:26:17</br>
* 版本: v1.0.0</br>
* 描述: 对jsonObject 进行转义</br>
* 方法名: JSON2Escape</br>
*/
private static void JSON2Escape(String key, Object value, Map<String, Object> model) {
JSONObject jsonObject = (JSONObject) value;
String temp = jsonEncode(jsonObject.toJSONString());
model.put(key, JSONObject.parse(temp));
}

/**
*
* 时间: 2017年7月17日 上午10:26:17</br>
* 版本: v1.0.0</br>
* 描述: 对jsonArray 进行转义</br>
* 方法名: JSONArray2Escape</br>
*/
private static void JSONArray2Escape(String key, Object value, Map<String, Object> model) {
JSONArray jsonArray = (JSONArray) value;
String temp = jsonEncode(jsonArray.toJSONString());
model.put(key, JSONArray.parse(temp));
}

/**
*
* 时间: 2017年7月17日 上午10:26:17</br>
* 版本: v1.0.0</br>
* 描述: 对list 进行转义</br>
* 方法名: list2Escape</br>
*/
private static void list2Escape(String key, Object value, Map<String, Object> model) {
if (key != null) {
JSONArray json = (JSONArray) JSONArray.toJSON(value);
JSONArray2Escape(key, json, model);
}
}

/**
*
* 时间: 2017年7月17日 上午10:26:19</br>
* 版本: v1.0.0</br>
* 描述: 对字符串进行转义</br>
* 方法名: str2Escape</br>
*/
private static void str2Escape(String key, Object value, Map<String, Object> model) {
if (key != null) {
String str = (String) value;
model.put(key, strEncode(str));
}
}

/**
*
* 时间: 2017年7月17日 上午10:26:21</br>
* 版本: v1.0.0</br>
* 描述: 对字符串进行转义</br>
* 方法名: strArr2Escape</br>
*/
private static void strArr2Escape(String key, Object value, Map<String, Object> model) {
if (key != null) {
for (String str : (String[]) value) {
str = strEncode(str);
}
model.put(key, value);
}
}

/**
*
* 时间: 2017年7月17日 上午10:26:23</br>
* 版本: v1.0.0</br>
* 描述: 对字符转义</br>
* 方法名: char2Escape</br>
*/
private static void char2Escape(String key, Object value, Map<String, Object> model) {
if (key != null) {
String str = charEncode((char) value);
model.put(key, str);
}
}

/**
*
* 时间: 2017年7月17日 上午10:26:26</br>
* 版本: v1.0.0</br>
* 描述: 对object转义</br>
* 方法名: object2Escape</br>
*/
private static void object2Escape(String key, Object value, Map<String, Object> model) {
if (key != null) {
JSONObject json = (JSONObject) JSONObject.toJSON(value);
JSON2Escape(key, json, model);
}
}

/**
*
* 时间: 2017年7月17日 上午10:26:31</br>
* 版本: v1.0.0</br>
* 描述: 转义对象</br>
* 方法名: escapeTask</br>
*/
public static void escapeTask(Map<String, Object> model) {
if (model == null || model.isEmpty()) {
return;
}
for (Map.Entry<String, Object> entry : model.entrySet()) {
if (entry.getKey() != null) {
if (entry.getKey().equals("rc") || entry.getKey().equals("springMacroRequestContext")
|| entry.getKey().contains("org.springframework.validation.BindingResult")
|| entry.getKey().equals("APIJson")) {
continue;
}
}
if (entry.getValue() == null) {
continue;
}
// 遇到类型为 Boolean、Long、Short、Integer、Double、Float、Float、Date自动跳过
if (entry.getValue() instanceof Boolean) {
continue;
}
if (entry.getValue() instanceof Long) {
continue;
}
if (entry.getValue() instanceof Short) {
continue;
}
if (entry.getValue() instanceof Byte) {
continue;
}
if (entry.getValue() instanceof Integer) {
continue;
}
if (entry.getValue() instanceof Double) {
continue;
}
if (entry.getValue() instanceof Float) {
continue;
}
if (entry.getValue() instanceof Date) {
continue;
}
if (entry.getValue() instanceof java.sql.Date) {
continue;
}
// 转义字符
if (entry.getValue() instanceof Char) {
char2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对string类型的值进行转义
if (entry.getValue() instanceof String) {
str2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对string[] 类型进行转义
if (entry.getValue() instanceof String[]) {
strArr2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对jsonObject 类型进行转义
if (entry.getValue() instanceof JSONObject) {
JSON2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对JSONArray 类型进行转义
if (entry.getValue() instanceof JSONArray) {
JSONArray2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对 list 进行转义
if (entry.getValue() instanceof List) {
list2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
// 对 object 进行转义
if (entry.getValue() instanceof Object) {
object2Escape(entry.getKey(), entry.getValue(), model);
continue;
}
}
}
}


2.方法类的调用

package com.hand.util.velocity;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;

import com.hand.util.VelocityEscapeUtil;

public class VelocityToolbox2View extends VelocityToolboxView {
@Override
protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ViewToolContext ctx;
**VelocityEscapeUtil.escapeTask(model);**
ctx = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());
ctx.putAll(model);
if (this.getToolboxConfigLocation() != null) {
ToolManager tm = new ToolManager();
tm.setVelocityEngine(getVelocityEngine());
tm.configure(getServletContext().getRealPath(getToolboxConfigLocation()));
if (tm.getToolboxFactory().hasTools(Scope.REQUEST)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.REQUEST));
}
if (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.APPLICATION));
}
if (tm.getToolboxFactory().hasTools(Scope.SESSION)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.SESSION));
}
}
return ctx;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: