登录token优化

This commit is contained in:
2026-02-27 11:11:02 +08:00
parent 3b228a82ba
commit c677027988
2 changed files with 50 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import com.mini.mybigscreen.Model.Result;
import com.mini.mybigscreen.biz.domain.HomeUser;
import com.mini.mybigscreen.biz.service.HomeUserService;
import com.mini.mybigscreen.utils.AesUtil;
import com.mini.mybigscreen.utils.KeyUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
@@ -31,7 +32,7 @@ public class userController {
.eq("password", AesUtil.encrypt(loginRequest.getPassword()));
HomeUser user = userService.getOne(queryWrapper, true);
if (user != null) {
String token = "admin-token-" + System.currentTimeMillis();
String token = KeyUtil.ObjKey(125,0);
HttpSession session = request.getSession(true);
session.setAttribute("userName", user.getUserName());
session.setAttribute("token", token);

View File

@@ -0,0 +1,48 @@
package com.mini.mybigscreen.utils;
import java.util.Random;
public class KeyUtil {
public static String ObjKey(int length, int type) {
Random random = new Random();
StringBuffer key = new StringBuffer();
if (type == 1) {
String str = "0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(10);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else if (type == 2) {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(36);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else if (type == 3) {
String str = "abcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(36);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(62);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
}
}
}