大屏项目初始化
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package com.mini.mybigscreen.Auth;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.mini.mybigscreen.Model.LoginRequest;
|
||||
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 jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -12,21 +17,26 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class userController {
|
||||
|
||||
|
||||
@Resource
|
||||
private HomeUserService userService;
|
||||
|
||||
|
||||
/**
|
||||
* 系统登录
|
||||
*/
|
||||
@PostMapping("/userLogin")
|
||||
public Result<?> getUserInfo(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {
|
||||
String username = loginRequest.getUsername();
|
||||
String password = loginRequest.getPassword();
|
||||
if ("admin".equals(username) && "123456".equals(password)) {
|
||||
QueryWrapper<HomeUser> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_name", loginRequest.getUsername())
|
||||
.eq("password", AesUtil.encrypt(loginRequest.getPassword()));
|
||||
HomeUser user = userService.getOne(queryWrapper, true);
|
||||
if (user != null) {
|
||||
String token = "admin-token-" + System.currentTimeMillis();
|
||||
HttpSession session = request.getSession(true);
|
||||
session.setAttribute("loginUser", username);
|
||||
session.setAttribute("userName", user.getUserName());
|
||||
session.setAttribute("token", token);
|
||||
return Result.success(token);
|
||||
} else {
|
||||
return Result.error("账号或密码错误");
|
||||
}
|
||||
return Result.error("账号或密码错误");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class demo {
|
||||
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
|
||||
})
|
||||
.strategyConfig(builder -> {
|
||||
builder.addInclude("biz_item_info")
|
||||
builder.addInclude("biz_home_user")
|
||||
.addTablePrefix("biz_")
|
||||
.entityBuilder()
|
||||
.enableLombok()
|
||||
|
||||
109
src/main/java/com/mini/mybigscreen/utils/AesUtil.java
Normal file
109
src/main/java/com/mini/mybigscreen/utils/AesUtil.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package com.mini.mybigscreen.utils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* AES加密解密工具类(CBC/PKCS5Padding)
|
||||
* 优化说明:修复密钥BUG+解决换行问题+性能优化+安全加固+零依赖
|
||||
*/
|
||||
public class AesUtil {
|
||||
|
||||
|
||||
private static final LoggerUtils logger = LoggerUtils.getInstance();
|
||||
private static final String AES_KEY_HEX = "AD42F6697B035B7580E4FEF93BE20BAD"; // 你的32位16进制密钥
|
||||
private static final String CHARSET = StandardCharsets.UTF_8.name();
|
||||
private static final int IV_LENGTH = 16; // AES CBC IV固定16字节
|
||||
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
|
||||
private static final String ALGORITHM = "AES";
|
||||
|
||||
private static final ThreadLocal<Cipher> ENCRYPT_CIPHER = ThreadLocal.withInitial(() -> {
|
||||
try {
|
||||
return Cipher.getInstance(TRANSFORMATION);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
private static final ThreadLocal<Cipher> DECRYPT_CIPHER = ThreadLocal.withInitial(() -> {
|
||||
try {
|
||||
return Cipher.getInstance(TRANSFORMATION);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*/
|
||||
public static String encrypt(String content) {
|
||||
return encrypt(content, hex2Bytes(AES_KEY_HEX));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*/
|
||||
public static String decrypt(String content) {
|
||||
return decrypt(content, hex2Bytes(AES_KEY_HEX));
|
||||
}
|
||||
|
||||
public static String encrypt(String content, byte[] key) {
|
||||
if (content == null || content.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
|
||||
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(key, IV_LENGTH));
|
||||
Cipher cipher = ENCRYPT_CIPHER.get();
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
|
||||
byte[] encryptBytes = cipher.doFinal(content.getBytes(CHARSET));
|
||||
return Base64.getEncoder().encodeToString(encryptBytes);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String decrypt(String content, byte[] key) {
|
||||
if (content == null || content.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
|
||||
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(key, IV_LENGTH));
|
||||
Cipher cipher = DECRYPT_CIPHER.get();
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
|
||||
byte[] decryptBytes = cipher.doFinal(Base64.getDecoder().decode(content));
|
||||
String result = new String(decryptBytes, CHARSET);
|
||||
return result.trim();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 32位16进制字符串转16字节数组(解决你的密钥长度BUG的核心方法)
|
||||
*/
|
||||
private static byte[] hex2Bytes(String hexStr) {
|
||||
if (hexStr == null || hexStr.length() % 2 != 0) {
|
||||
throw new IllegalArgumentException("密钥必须是偶数长度的16进制字符串");
|
||||
}
|
||||
byte[] bytes = new byte[hexStr.length() / 2];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机16字节IV向量(方案2:安全随机IV,新业务推荐使用)
|
||||
*/
|
||||
private static byte[] generateRandomIV() {
|
||||
byte[] iv = new byte[IV_LENGTH];
|
||||
ThreadLocalRandom.current().nextBytes(iv);
|
||||
return iv;
|
||||
}
|
||||
}
|
||||
187
src/main/java/com/mini/mybigscreen/utils/LoggerUtils.java
Normal file
187
src/main/java/com/mini/mybigscreen/utils/LoggerUtils.java
Normal file
@@ -0,0 +1,187 @@
|
||||
package com.mini.mybigscreen.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
|
||||
public class LoggerUtils {
|
||||
|
||||
// 日志级别
|
||||
public enum Level {
|
||||
DEBUG, INFO, WARN, ERROR
|
||||
}
|
||||
|
||||
// 单例实例
|
||||
private static volatile LoggerUtils instance;
|
||||
|
||||
private String baseLogPath;
|
||||
|
||||
|
||||
private final SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
// 线程安全锁
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
// 禁止外部实例化
|
||||
private LoggerUtils(String baseLogPath) {
|
||||
this.baseLogPath = baseLogPath;
|
||||
initLogDir(); // 初始化日志目录
|
||||
}
|
||||
|
||||
public static LoggerUtils getInstance() {
|
||||
return getInstance("/ogsapp/logs");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单例实例(自定义日志路径)
|
||||
*
|
||||
* @param baseLogPath 日志根目录(支持相对路径或绝对路径)
|
||||
*/
|
||||
public static LoggerUtils getInstance(String baseLogPath) {
|
||||
if (instance == null) {
|
||||
synchronized (LoggerUtils.class) {
|
||||
if (instance == null) {
|
||||
instance = new LoggerUtils(baseLogPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化日志目录(若不存在则创建多级目录)
|
||||
*/
|
||||
private void initLogDir() {
|
||||
try {
|
||||
Files.createDirectories(Paths.get(baseLogPath));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("初始化日志目录失败:" + baseLogPath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String getCurrentLogFilePath(Level level) {
|
||||
String fileName = level + "_APP" + ".log";
|
||||
return baseLogPath + File.separator + fileName;
|
||||
}
|
||||
|
||||
// ------------------------------ 日志方法(支持多类型可变参数) ------------------------------
|
||||
|
||||
/**
|
||||
* 记录DEBUG级别日志(支持1到多个任意类型参数)
|
||||
*/
|
||||
public void debug(Object... messages) {
|
||||
log(Level.DEBUG, joinMessages(messages), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录INFO级别日志(支持1到多个任意类型参数)
|
||||
*/
|
||||
public void info(Object... messages) {
|
||||
log(Level.INFO, joinMessages(messages), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录WARN级别日志(支持1到多个任意类型参数)
|
||||
*/
|
||||
public void warn(Object... messages) {
|
||||
log(Level.WARN, joinMessages(messages), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录WARN级别日志(支持1到多个任意类型参数+异常)
|
||||
*/
|
||||
public void warn(Object[] messages, Throwable throwable) {
|
||||
log(Level.WARN, joinMessages(messages), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录ERROR级别日志(支持1到多个任意类型参数)
|
||||
*/
|
||||
public void error(Object... messages) {
|
||||
log(Level.ERROR, joinMessages(messages), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录ERROR级别日志(支持1到多个任意类型参数+异常)
|
||||
*/
|
||||
public void error(Object[] messages, Throwable throwable) {
|
||||
log(Level.ERROR, joinMessages(messages), throwable);
|
||||
}
|
||||
|
||||
// ------------------------------ 核心方法 ------------------------------
|
||||
|
||||
/**
|
||||
* 核心日志写入逻辑
|
||||
*/
|
||||
private void log(Level level, String message, Throwable throwable) {
|
||||
// 构建日志内容
|
||||
StringBuilder logContent = new StringBuilder();
|
||||
logContent.append("[").append(logDateFormat.format(new Date())).append("] "); // 时间戳
|
||||
logContent.append("[").append(level.name()).append("] "); // 日志级别
|
||||
logContent.append("[Thread-").append(Thread.currentThread().getId()).append("] "); // 线程ID
|
||||
logContent.append(message); // 拼接后的消息
|
||||
|
||||
// 追加异常堆栈信息(如果有)
|
||||
if (throwable != null) {
|
||||
logContent.append("\n").append(getStackTrace(throwable));
|
||||
}
|
||||
logContent.append("\n"); // 每条日志换行
|
||||
|
||||
// 加锁写入文件(保证线程安全)
|
||||
lock.lock();
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(getCurrentLogFilePath(level), true), // 追加模式
|
||||
StandardCharsets.UTF_8 // 避免中文乱码
|
||||
)
|
||||
)) {
|
||||
System.out.print(logContent);
|
||||
writer.write(logContent.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
System.err.println("日志写入失败:" + e.getMessage());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多类型可变参数为单个字符串(自动转换任意类型为字符串,处理null)
|
||||
*
|
||||
* @param messages 1到多个任意类型参数(不可为空数组)
|
||||
* @return 拼接后的字符串
|
||||
*/
|
||||
private String joinMessages(Object... messages) {
|
||||
if (messages == null || messages.length == 0) {
|
||||
throw new IllegalArgumentException("日志消息至少需要1个参数");
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object msg : messages) {
|
||||
sb.append(msg);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常堆栈信息转为字符串
|
||||
*/
|
||||
private String getStackTrace(Throwable throwable) {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
throwable.printStackTrace(pw);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态修改日志路径
|
||||
*/
|
||||
public void setBaseLogPath(String baseLogPath) {
|
||||
this.baseLogPath = baseLogPath;
|
||||
initLogDir();
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ spring.servlet.multipart.max-file-size=200MB
|
||||
spring.servlet.multipart.max-request-size=1000MB
|
||||
spring.servlet.multipart.file-size-threshold=10MB
|
||||
## MySQL
|
||||
spring.datasource.url=jdbc:mysql://crontab.club:33069/work?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
|
||||
spring.datasource.url=jdbc:mysql://192.168.31.189:33069/work?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
|
||||
spring.datasource.username=dream
|
||||
spring.datasource.password=info_dream
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
Reference in New Issue
Block a user