|
@@ -5,6 +5,7 @@ import com.fs.course.config.RedPacketConfig;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
+import java.security.SecureRandom;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
@@ -15,17 +16,9 @@ import java.util.*;
|
|
|
public class HuaweiCloudTest {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- RedPacketConfig config = new RedPacketConfig();
|
|
|
- config.setIsNew(1);
|
|
|
- R result = new R();
|
|
|
- // 根据 isNew 判断使用哪种发红包方式
|
|
|
- if (config.getIsNew() != null && config.getIsNew() == 1) {
|
|
|
- result = test1();
|
|
|
- } else {
|
|
|
- result= test2();
|
|
|
- }
|
|
|
- result.put("isNew",config.getIsNew());
|
|
|
- System.out.println(result);
|
|
|
+ String randomString = generateRandomString();
|
|
|
+ System.out.println("生成的随机字符串: " + randomString);
|
|
|
+ System.out.println("长度验证: " + randomString.length());
|
|
|
}
|
|
|
|
|
|
public static R test1(){
|
|
@@ -77,4 +70,21 @@ public class HuaweiCloudTest {
|
|
|
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
}
|
|
|
|
|
|
+ public static String generateRandomString() {
|
|
|
+ // 包含所有允许的字符
|
|
|
+ String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
+
|
|
|
+ // 使用安全随机数生成器
|
|
|
+ SecureRandom random = new SecureRandom();
|
|
|
+ StringBuilder sb = new StringBuilder(32);
|
|
|
+
|
|
|
+ for (int i = 0; i < 32; i++) {
|
|
|
+ // 生成0到chars.length()-1之间的随机索引
|
|
|
+ int randomIndex = random.nextInt(chars.length());
|
|
|
+ sb.append(chars.charAt(randomIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
}
|