Java escape HTML

后端 未结 6 1014
故里飘歌
故里飘歌 2020-12-03 06:34

currently I use org.apache.commons.lang.StringEscapeUtils escapeHtml() to escape unwanted HTML tags in my Strings but then I realized it escapes characters with

6条回答
  •  天命终不由人
    2020-12-03 07:16

    Here's a version that replaces the six significant characters as recommended by OWASP. This is suitable for HTML content elements like , but not HTML attributes like because the latter are often left unquoted.

    StringUtils.replaceEach(text,
            new String[]{"&", "<", ">", "\"", "'", "/"},
            new String[]{"&", "<", ">", """, "'", "/"});
    

提交回复
热议问题