最近有用到一个字符串处理类StringEscapeUtils,来自于apache工具包common-lang中,,这个类能很方便的进行html,xml,java等的转义与反转义;引入依赖:<dependency (html)); //转义 String uhtml = StringEscapeUtils.unescapeHtml(html); //反转义其他处理方式类似;需要注意的是,在进行反转义的时候, (html)); String uhtml = StringEscapeUtils.unescapeHtml(html); System.out.println(uhtml) ; String ujava = StringEscapeUtils.unescapeJava(uhtml); System.out.println(ujava); (" System.out.println(\"测试\")//这是注释")); System.out.println(StringEscapeUtils.escapeSql("select
(request.getParameter("param")); System.out.println("StringEscapeUtils " + secparam); String owaspparam param=value中文’;alert(‘x’)//<> system.out的输出为: original value中文';alert('x')//<> StringEscapeUtils value :value中文';alert('x')//<> OWASP: value中文';alert('x')//<> StringEscapeUtils.escapeJavaScript会对单引号’和双引号” StringEscapeUtils.escapeHtml和ESAPI.encoder().encodeForHTML可以帮助我们完成这个功能. . <% String doubleSecparam = StringEscapeUtils.escapeJavaScript( StringEscapeUtils.escapeHtml(request.getParameter
json字符串转义格式化后再转换处理demo StringEscapeUtils.unescapeJava 报错关键字: illegal identifier Expected BEGIN_OBJECT com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.apache.commons.lang3.StringEscapeUtils * 在读取到这个json字符串之后,发现该字符串中包含了\转义符号,程序自动把它变成了\\\ * 如果遇到类似的问题,可以直接使用org.apache.commons包下的StringEscapeUtils 1, column 2 */ // System.out.println("map="+map); //转换成功 // json = StringEscapeUtils.unescapeJava // json = StringEscapeUtils.unescapeJava(json); // // Map map = objectMapper.readValue
org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang.StringEscapeUtils Exception", e); } } /** * Html 转码. */ public static String htmlEscape(String html) { return StringEscapeUtils.escapeHtml html); } /** * Html 解码. */ public static String htmlUnescape(String htmlEscaped) { return StringEscapeUtils.unescapeHtml (htmlEscaped); } /** * Xml 转码. */ public static String xmlEscape(String xml) { return StringEscapeUtils.escapeXml (xml); } /** * Xml 解码. */ public static String xmlUnescape(String xmlEscaped) { return StringEscapeUtils.unescapeXml
false; } } 创建XssHttpServletRequestWrapper package com.dongao.filter; import org.apache.commons.lang3.StringEscapeUtils super.getHeader(name); if (value == null) { return null; } return StringEscapeUtils.escapeHtml4 super.getParameter(name); if (value == null) { return null; } return StringEscapeUtils.escapeHtml4 new String[length]; for(int i = 0; i < length; i++){ escapseValues[i] = StringEscapeUtils.escapeHtml4
忽略转义的工具类, org.apache.commons.lang.StringEscapeUtils,其中unescapeJava(String s)方法是来处理java转义字符的,可以将字符串中的 通过这个方法处理以上字符串 package day01; import org.apache.commons.lang.StringEscapeUtils; public class StringEscapeUtils_Test imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" + "}\n"; String s2 = StringEscapeUtils.unescapeJava
过滤Html标签 import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils String cleanHtml(String content) { if (StringUtils.isBlank(content)) { return ""; } content = StringEscapeUtils.unescapeHtml4 (content); content = Jsoup.clean(content, Whitelist.none()); content = StringEscapeUtils.unescapeHtml4 (content).replaceAll(" ", "").trim(); content = StringEscapeUtils.unescapeJava(content); return content
org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringEscapeUtils 五位的随机字母和数字 System.out.println("取得随机字母和数字: "+RandomStringUtils.randomAlphanumeric(15)); // 7.StringEscapeUtils System.out.println(StringEscapeUtils.unescapeHtml3("</html>")); // 输出结果为<html> System.out.println(StringEscapeUtils.escapeJava("String")); // 8.StringUtils,判断是否是空格字符
XssAndSqlHttpServletRequestWrapper.java import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils StringUtils.isEmpty(value)) { value = StringEscapeUtils.escapeHtml4(value); } return value; } @Override i = 0; i < parameterValues.length; i++) { String value = parameterValues[i]; parameterValues[i] = StringEscapeUtils.escapeHtml4 StringEscapeUtils.escapeHtml4这个方法来自Apache的工具类,maven坐标如下: <dependency> <groupId>org.apache.commons</groupId = null) { String encodedValue = StringEscapeUtils.escapeHtml4(value); jsonGenerator.writeString(encodedValue
我这边的报错就是“/”未转义; 2.解决办法: 2.1 工具类 package com.example.去转移; public class StringEscapeUtils { public str); System.out.println(unescapedStr); // 输出 hello\nworld 我这边直接换行了 因为\n 就是换行的意思 } 2.2 StringEscapeUtils.unescapeJava args) { String str = "hello\\nworld"; String unescapedStr = org.apache.commons.text.StringEscapeUtils.unescapeJava = (ObjectNode) node.getParent(); parent.set(node.fieldName(), objectMapper.valueToTree(StringEscapeUtils.unescapeJava 该方法会递归遍历 JSON 对象树,对所有字符串节点调用 StringEscapeUtils.unescapeJava() 方法去除转义字符,并将结果设置回到父节点中。
super(request); } @Override public String getHeader(String name) { return StringEscapeUtils.escapeHtml4 super.getHeader(name)); } @Override public String getQueryString() { return StringEscapeUtils.escapeHtml4 super.getQueryString()); } @Override public String getParameter(String name) { return StringEscapeUtils.escapeHtml4 String[length]; for(int i = 0; i < length; i++){ escapseValues[i] = StringEscapeUtils.escapeHtml4
javax.servlet.http.HttpServletRequestWrapper; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.text.StringEscapeUtils { super(request); } @Override public String getQueryString() { return StringEscapeUtils.escapeHtml4 super.getQueryString()); } @Override public String getParameter(String name) { return StringEscapeUtils.escapeHtml4 escapeValues = new String[length]; for (int i = 0; i < length; i++) { escapeValues[i] = StringEscapeUtils.escapeHtml4 = null) { return StringEscapeUtils.escapeHtml4(value.toString()); } return
最终实现 看一下最终的代码实现(有些导入的包被我删了) 重新包装Request的代码 import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger reqBody = getBodyString(); } @Override public String getQueryString() { return StringEscapeUtils.escapeHtml4 logger.info("----filter before--name:{}--value:{}----", name, value); // value = StringEscapeUtils.escapeHtml4 logger.info("----filter before--value:{}----", str, str); str = StringEscapeUtils.escapeHtml4 JSON*/ if (isJson(jp.getText())) { return jp.getText(); } return StringEscapeUtils.escapeHtml4
. */ public static String htmlEscape(String html) { return StringEscapeUtils.escapeHtml */ public static String htmlUnescape(String htmlEscaped) { return StringEscapeUtils.unescapeHtml * Xml 转码. */ public static String xmlEscape(String xml) { return StringEscapeUtils.escapeXml 解码. */ public static String xmlUnescape(String xmlEscaped) { return StringEscapeUtils.unescapeXml
html转txt import org.apache.commons.lang3.StringEscapeUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings); final String result = StringEscapeUtils.unescapeHtml4
( ) EMPTY_BYTE_ARRAY subarray ( ) indexOf ( ) isEquals ( ) toObject ( ) 9. org.apache.commons.lang.StringEscapeUtils getSimpleProperty ( ) isWriteable ( ) setSimpleProperty ( ) getPropertyType ( ) 15. org.apache.commons.lang3.StringEscapeUtils
javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.apache.commons.lang3.StringEscapeUtils private static String xssEncode(String s) { if (s == null || s.isEmpty()) { return s; } return StringEscapeUtils.escapeHtml4
() EMPTY_BYTE_ARRAY subarray() indexOf() isEquals() toObject() org.apache.commons.lang.StringEscapeUtils getSimpleProperty() isWriteable() setSimpleProperty() getPropertyType() org.apache.commons.lang3.StringEscapeUtils
reqBody = getBodyString(); } @Override public String getQueryString() { return StringEscapeUtils.escapeHtml4 logger.info("----filter before--name:{}--value:{}----", name, value); // value = StringEscapeUtils.escapeHtml4 logger.info("----filter before--value:{}----", str, str); str = StringEscapeUtils.escapeHtml4 value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(StringEscapeUtils.escapeHtml4 JSON*/ if (isJson(jp.getText())) { return jp.getText(); } return StringEscapeUtils.escapeHtml4
( ) EMPTY_BYTE_ARRAY subarray ( ) indexOf ( ) isEquals ( ) toObject ( ) 9. org.apache.commons.lang.StringEscapeUtils getSimpleProperty ( ) isWriteable ( ) setSimpleProperty ( ) getPropertyType ( ) 15. org.apache.commons.lang3.StringEscapeUtils