From 3a154bdb0a5aaa2c0ac3eac95a6ba747068bd454 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 13 一月 2026 10:00:37 +0800
Subject: [PATCH] 优化
---
server/system_service/src/main/java/com/doumee/core/utils/PasswordGenerator.java | 85 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 85 insertions(+), 0 deletions(-)
diff --git a/server/system_service/src/main/java/com/doumee/core/utils/PasswordGenerator.java b/server/system_service/src/main/java/com/doumee/core/utils/PasswordGenerator.java
new file mode 100644
index 0000000..8afedfd
--- /dev/null
+++ b/server/system_service/src/main/java/com/doumee/core/utils/PasswordGenerator.java
@@ -0,0 +1,85 @@
+package com.doumee.core.utils;
+
+import com.doumee.core.constants.ResponseStatus;
+import com.doumee.core.exception.BusinessException;
+
+import java.security.SecureRandom;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @Author : Rk
+ * @create 2025/10/27 16:23
+ */
+public class PasswordGenerator {
+
+ private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
+ private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ private static final String DIGITS = "0123456789";
+ private static final String SYMBOLS = "!@#$%^&*()_+-=[]{};':|,.<>/?";
+ private static final String PASSWORD_CHARS = LOWER + UPPER + DIGITS + SYMBOLS;
+ private static final int PASSWORD_LENGTH = 8; // 浣犲彲浠ユ牴鎹渶瑕佽皟鏁村瘑鐮侀暱搴�
+
+ public static void getPasswordTypeCount(String password){
+ Set<Integer> typeCount = new HashSet<>(4);
+ for (int i = 0; i < password.length(); i++) {
+ String key = password.substring(i,i+1);
+ if(LOWER.indexOf(key)>=Constants.ZERO){
+ typeCount.add(Constants.ZERO);
+ }else if(UPPER.indexOf(key)>=Constants.ZERO){
+ typeCount.add(Constants.ONE);
+ }else if(DIGITS.indexOf(key)>=Constants.ZERO){
+ typeCount.add(Constants.TWO);
+ }else if(SYMBOLS.indexOf(key)>=Constants.ZERO){
+ typeCount.add(Constants.THREE);
+ }else{
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"璇蜂娇鐢ㄥ悎娉曠殑瀵嗙爜鍐呭");
+ }
+ }
+ if(typeCount.size()<Constants.THREE||password.length()<8){
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵嗙爜鑷冲皯鍖呮嫭鏁板瓧銆佸ぇ鍐欏瓧姣嶃�佸皬鍐欏瓧姣嶃�佺壒娈婂瓧绗︿腑鐨勪笁绉嶅瓧绗︼紝涓旇嚦灏�8浣嶆暟");
+ }
+ }
+
+
+
+ public static String generatePassword() {
+ SecureRandom random = new SecureRandom();
+ StringBuilder password = new StringBuilder(PASSWORD_LENGTH);
+ Set<Character> usedChars = new HashSet<>();
+
+// while (password.length() < PASSWORD_LENGTH) {
+// int index = random.nextInt(PASSWORD_CHARS.length());
+// char randomChar = PASSWORD_CHARS.charAt(index);
+// // 纭繚姣忕绫诲瀷鐨勫瓧绗﹁嚦灏戝嚭鐜颁竴娆�
+// if (usedChars.add(randomChar)) {
+// password.append(randomChar);
+// }
+// }
+ // 濡傛灉闇�瑕佺‘淇濇瘡绉嶇被鍨嬮兘鑷冲皯鍑虹幇涓�娆★紝鍙互娉ㄩ噴鎺変笂闈㈢殑寰幆锛屼娇鐢ㄤ笅闈㈢殑浠g爜锛�
+ for (int i = 0; i < PASSWORD_LENGTH; i++) {
+ int type = random.nextInt(4); // 0=lower, 1=upper, 2=digit, 3=symbol
+ String charSet;
+ switch (type) {
+ case 0: charSet = LOWER; break;
+ case 1: charSet = UPPER; break;
+ case 2: charSet = DIGITS; break;
+ default: charSet = SYMBOLS; break;
+ }
+ int index = random.nextInt(charSet.length());
+ password.append(charSet.charAt(index));
+ }
+ // Shuffle the string to avoid predictable patterns (optional)
+ password.toString().chars()
+ .mapToObj(c -> (char) c)
+ .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
+ .toString(); // This line is a no-op, but it's here to show the method call for completeness.
+ return password.toString();
+ }
+
+ public static void main(String[] args) {
+ System.out.println("Generated Password: " + generatePassword());
+ }
+}
--
Gitblit v1.9.3