RegisterUtil.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.zanxiang.sdk.util;
  2. import com.zanxiang.common.enums.HttpStatusEnum;
  3. import com.zanxiang.common.utils.MD5Util;
  4. import org.apache.logging.log4j.util.Strings;
  5. /**
  6. * @author : lingfeng
  7. * @time : 2022-06-22
  8. * @description : 用户注册工具类
  9. */
  10. public class RegisterUtil {
  11. /**
  12. * 密码加密盐值
  13. */
  14. private static final String CMF_PASSWORD_SALT = "ZX_PASSWORD_SALT";
  15. /**
  16. * 用户名最小长度
  17. */
  18. private static final int USER_NAME_LENGTH_MIN = 4;
  19. /**
  20. * 用户名最大长度
  21. */
  22. private static final int USER_NAME_LENGTH_MAX = 32;
  23. /**
  24. * 密码最小长度
  25. */
  26. private static final int PASSWORD_LENGTH_MIN = 6;
  27. /**
  28. * 密码最大长度
  29. */
  30. private static final int PASSWORD_LENGTH_MAX = 32;
  31. /**
  32. * 用户名合规检测
  33. *
  34. * @param userName : 用户名验证
  35. * @return : 返回验证结果
  36. */
  37. public static HttpStatusEnum checkUserName(String userName) {
  38. if (Strings.isBlank(userName)) {
  39. return HttpStatusEnum.USERNAME_EMPTY;
  40. }
  41. //用户名长度验证
  42. if (userName.length() < USER_NAME_LENGTH_MIN) {
  43. return HttpStatusEnum.USERNAME_TOO_SHORT;
  44. }
  45. if (userName.length() > USER_NAME_LENGTH_MAX) {
  46. return HttpStatusEnum.USERNAME_TOO_LONG;
  47. }
  48. //用户名只能是数字字母组合
  49. if (!RegexUtil.checkUserName(userName)) {
  50. return HttpStatusEnum.USERNAME_BAD_CHAR;
  51. }
  52. //密码验证
  53. return HttpStatusEnum.SUCCESS;
  54. }
  55. /**
  56. * 密码合规检测
  57. *
  58. * @param password : 密码验证
  59. * @return : 返回验证结果
  60. */
  61. public static HttpStatusEnum checkPassword(String password) {
  62. if (Strings.isBlank(password)) {
  63. return HttpStatusEnum.PASSWORD_EMPTY;
  64. }
  65. if (password.length() < PASSWORD_LENGTH_MIN) {
  66. return HttpStatusEnum.PASSWORD_TOO_SHORT;
  67. }
  68. if (password.length() > PASSWORD_LENGTH_MAX) {
  69. return HttpStatusEnum.PASSWORD_TOO_LONG;
  70. }
  71. //密码只能是数字字母组合
  72. if (!RegexUtil.checkPassword(password)) {
  73. return HttpStatusEnum.PASSWORD_BAD_CHAR;
  74. }
  75. return HttpStatusEnum.SUCCESS;
  76. }
  77. /**
  78. * 随机用户昵称
  79. *
  80. * @return : 返回用户昵称
  81. */
  82. public static String randomNickName(String userName) {
  83. return "smlgm" + Math.abs(userName.hashCode());
  84. }
  85. /**
  86. * 密码cfm加密
  87. *
  88. * @param password : 密码
  89. * @return : 返回加密完的密码
  90. */
  91. public static String cmfPassword(String password) {
  92. String passwordSalt = password + CMF_PASSWORD_SALT;
  93. String charsetName = "UTF-8";
  94. return "###" + MD5Util.MD5Encode(MD5Util.MD5Encode(passwordSalt, charsetName), charsetName);
  95. }
  96. }