EmojiHttpServletRequestWraper.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.hssx.cloudmodel.util;
  2. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletRequestWrapper;
  5. public class EmojiHttpServletRequestWraper extends HttpServletRequestWrapper {
  6. public EmojiHttpServletRequestWraper(HttpServletRequest request) {
  7. super(request);
  8. }
  9. @Override
  10. public String getParameter(String name) {
  11. return filterEmoji(super.getParameter(name));
  12. }
  13. @Override
  14. public String getHeader(String name) {
  15. return filterEmoji(super.getParameter(name));
  16. }
  17. @Override
  18. public String[] getParameterValues(String name) {
  19. System.out.println("getParameterValues----->转义处理:"+name);
  20. if(!StringUtils.isEmpty(name)){
  21. String[] values = super.getParameterValues(name);
  22. if(values != null ){
  23. if(values.length > 0) {
  24. String[] newValues = new String[values.length];
  25. for(int i =0; i< values.length; i++){
  26. if (containsEmoji(values[i])) {
  27. System.out.println("包含emoji:"+values[i]);
  28. newValues[i] = filterEmoji(values[i]);
  29. System.out.println("包含emoji,处理后:"+newValues[i]);
  30. } else {
  31. newValues[i] = values[i];
  32. }
  33. }
  34. return newValues;
  35. }
  36. }
  37. }
  38. return null;
  39. }
  40. public static boolean containsEmoji(String source) {
  41. if (StringUtils.isEmpty(source)) {
  42. return false;
  43. }
  44. int len = source.length();
  45. for (int i = 0; i < len; i++) {
  46. char codePoint = source.charAt(i);
  47. if (isEmojiCharacter(codePoint)) {
  48. //do nothing,判断到了这里表明,确认有表情字符
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. private static boolean isEmojiCharacter(char codePoint) {
  55. return (codePoint == 0x0) ||
  56. (codePoint == 0x9) ||
  57. (codePoint == 0xA) ||
  58. (codePoint == 0xD) ||
  59. ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
  60. ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
  61. ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
  62. }
  63. /**
  64. * 过滤emoji 或者 其他非文字类型的字符
  65. * @param source
  66. * @return
  67. */
  68. public static String filterEmoji(String source) {
  69. if (source == null || "".equals(source)) {
  70. return source;
  71. }
  72. System.out.println("过滤 = "+source);
  73. source = source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
  74. if (!containsEmoji(source)) {
  75. return source;//如果不包含,直接返回
  76. }
  77. //到这里铁定包含
  78. StringBuilder buf = null;
  79. int len = source.length();
  80. for (int i = 0; i < len; i++) {
  81. char codePoint = source.charAt(i);
  82. if (isEmojiCharacter(codePoint)) {
  83. if (buf == null) {
  84. buf = new StringBuilder(source.length());
  85. }
  86. buf.append(codePoint);
  87. } else {
  88. buf.append("*");
  89. }
  90. }
  91. if (buf == null) {
  92. return source;//如果没有找到 emoji表情,则返回源字符串
  93. } else {
  94. if (buf.length() == len) {//这里的意义在于尽可能少的toString,因为会重新生成字符串
  95. buf = null;
  96. return source;
  97. } else {
  98. return buf.toString();
  99. }
  100. }
  101. }
  102. }