CommonUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.java110.utils.util;
  2. import com.java110.utils.log.LoggerEngine;
  3. import org.apache.commons.lang3.math.NumberUtils;
  4. import org.springframework.util.StringUtils;
  5. import java.util.Random;
  6. /**
  7. * 公用工具类
  8. * Created by wuxw on 2017/3/10.
  9. */
  10. public class CommonUtil extends LoggerEngine {
  11. /**
  12. * 将 30*1000 转为 30000
  13. * 不能出现小数点等
  14. *
  15. * @param val
  16. * @return
  17. */
  18. public static int multiplicativeStringToInteger(String val) {
  19. try {
  20. if (StringUtils.isEmpty(val)) {
  21. return 0;
  22. }
  23. if (val.contains("*")) {
  24. String[] vals = val.split("\\*");
  25. int value = 1;
  26. for (int vIndex = 0; vIndex < vals.length; vIndex++) {
  27. if (!NumberUtils.isNumber(vals[vIndex])) {
  28. throw new ClassCastException("配置的数据有问题,必须配置为30*1000格式");
  29. }
  30. value *= Integer.parseInt(vals[vIndex]);
  31. }
  32. return value;
  33. }
  34. if (NumberUtils.isNumber(val)) {
  35. return Integer.parseInt(val);
  36. }
  37. } catch (Exception e) {
  38. logger.error("---------------[CommonUtil.multiplicativeStringToInteger]----------------类型转换失败", e);
  39. return 0;
  40. }
  41. return 0;
  42. }
  43. /**
  44. * 生成六位验证码
  45. *
  46. * @return
  47. */
  48. public static String generateVerificationCode() {
  49. Random random = new Random();
  50. String result = "";
  51. for (int i = 0; i < 6; i++) {
  52. result += random.nextInt(10);
  53. }
  54. return result;
  55. }
  56. // 手机号码前三后四脱敏
  57. public static String mobileEncrypt(String mobile) {
  58. if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
  59. return mobile;
  60. }
  61. return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
  62. }
  63. //身份证前三后四脱敏
  64. public static String idEncrypt(String id) {
  65. if (StringUtils.isEmpty(id) || (id.length() < 8)) {
  66. return id;
  67. }
  68. return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
  69. }
  70. //效验
  71. public static boolean sqlValidate(String str) {
  72. str = str.toLowerCase();//统一转为小写
  73. String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|" +
  74. "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|" +
  75. "table|from|grant|use|group_concat|column_name|" +
  76. "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|" +
  77. "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";//过滤掉的sql关键字,可以手动添加
  78. String[] badStrs = badStr.split("\\|");
  79. for (int i = 0; i < badStrs.length; i++) {
  80. if (str.indexOf(badStrs[i]) >= 0) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. }