SqlUtil.java 819 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.ruoyi.common.utils.sql;
  2. import com.ruoyi.common.utils.StringUtils;
  3. /**
  4. * sql操作工具类
  5. *
  6. * @author ruoyi
  7. */
  8. public class SqlUtil
  9. {
  10. /**
  11. * 仅支持字母、数字、下划线、空格、逗号(支持多个字段排序)
  12. */
  13. public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,]+";
  14. /**
  15. * 检查字符,防止注入绕过
  16. */
  17. public static String escapeOrderBySql(String value)
  18. {
  19. if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value))
  20. {
  21. return StringUtils.EMPTY;
  22. }
  23. return value;
  24. }
  25. /**
  26. * 验证 order by 语法是否符合规范
  27. */
  28. public static boolean isValidOrderBySql(String value)
  29. {
  30. return value.matches(SQL_PATTERN);
  31. }
  32. }