XssUtil.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.yihu.jw.util;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. * Created by yeshijie on 2020/8/27.
  6. */
  7. public class XssUtil {
  8. private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  9. private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  10. private static final String REGEX_HTML = "<[^>]+>"; // 定义HTML标签的正则表达式
  11. // private static final String REGEX_SPACE = "\\s*|\t|\r|\n";// 定义空格回车换行符
  12. /**
  13. * 过滤掉script标签
  14. *
  15. * @param htmlStr
  16. * @return
  17. */
  18. public static String delScriptTag(String htmlStr) {
  19. if (htmlStr == null)
  20. return null;
  21. Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
  22. Matcher m_script = p_script.matcher(htmlStr);
  23. htmlStr = m_script.replaceAll(""); // 过滤script标签
  24. return htmlStr;
  25. }
  26. /**
  27. * 过滤掉style标签
  28. *
  29. * @param htmlStr
  30. * @return
  31. */
  32. public static String delStyleTag(String htmlStr) {
  33. if (htmlStr == null)
  34. return null;
  35. Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
  36. Matcher m_style = p_style.matcher(htmlStr);
  37. htmlStr = m_style.replaceAll(""); // 过滤style标签*/
  38. return htmlStr;
  39. }
  40. /**
  41. * @param htmlStr
  42. * @return 删除Html标签
  43. */
  44. public static String delHTMLTag(String htmlStr) {
  45. if (htmlStr == null)
  46. return null;
  47. Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
  48. Matcher m_html = p_html.matcher(htmlStr);
  49. htmlStr = m_html.replaceAll(""); // 过滤html标签
  50. return htmlStr; // 返回文本字符串
  51. }
  52. /**
  53. * 将html标签中的<>进行转义,如只有"<"或者">"则不进行转义
  54. */
  55. public static String escapeHtml(String htmlStr) {
  56. if (htmlStr == null)
  57. return null;
  58. Pattern p_script = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
  59. Matcher m_script = p_script.matcher(htmlStr);
  60. while (m_script.find()) {
  61. String str = m_script.group();
  62. String str_target = str.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
  63. htmlStr = htmlStr.replaceAll(str, str_target);
  64. }
  65. return htmlStr;
  66. }
  67. /**
  68. * 转义html标签
  69. *
  70. * @param input
  71. * @return
  72. */
  73. public static String cleanXss(String input) {
  74. if (input == null)
  75. return null;
  76. return escapeHtml(delStyleTag(delScriptTag(input)));
  77. }
  78. public static void main(String[] args) {
  79. System.out.println(cleanXss("<'script>"));
  80. }
  81. }