88 lines
3.3 KiB
Java
88 lines
3.3 KiB
Java
package com.yj.earth.business.controller;
|
|
|
|
import com.yj.earth.annotation.CheckAuth;
|
|
import com.yj.earth.auth.AuthInfo;
|
|
import com.yj.earth.auth.AuthValidator;
|
|
import com.yj.earth.common.util.ApiResponse;
|
|
import com.yj.earth.common.util.ServerUniqueIdUtil;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
|
|
@Tag(name = "系统设置管理")
|
|
@RestController
|
|
@RequestMapping("/auth")
|
|
public class AuthController {
|
|
|
|
// 授权文件存储路径、项目根目录下的 license 目录
|
|
private static final String AUTH_FILE_PATH = "license/yjearth.lic";
|
|
|
|
@GetMapping("/info")
|
|
@Operation(summary = "获取系统授权码")
|
|
public ApiResponse info() {
|
|
return ApiResponse.success(ServerUniqueIdUtil.getServerUniqueId());
|
|
}
|
|
|
|
@PostMapping("/import")
|
|
@Operation(summary = "导入授权信息")
|
|
public ApiResponse importAuth(@Parameter(description = "授权文件", required = true) @RequestParam("file") MultipartFile file) {
|
|
// 验证文件是否为空
|
|
if (file.isEmpty()) {
|
|
return ApiResponse.failure("请选择授权文件");
|
|
}
|
|
|
|
// 验证文件名是否为 yjearth.lic
|
|
String fileName = file.getOriginalFilename();
|
|
if (fileName == null || !fileName.equals("yjearth.lic")) {
|
|
return ApiResponse.failure("请上传 yjearth.lic");
|
|
}
|
|
|
|
try {
|
|
// 读取文件内容
|
|
String authContent = new String(file.getBytes(), StandardCharsets.UTF_8).trim();
|
|
// 验证授权内容有效性
|
|
String serverHardwareMd5 = ServerUniqueIdUtil.getServerUniqueId();
|
|
boolean isValid = AuthValidator.validateAuth(authContent, serverHardwareMd5);
|
|
if (!isValid) {
|
|
return ApiResponse.failure("授权文件无效或已过期");
|
|
}
|
|
// 创建目录(如果不存在)
|
|
Path path = Paths.get(AUTH_FILE_PATH);
|
|
if (!Files.exists(path.getParent())) {
|
|
Files.createDirectories(path.getParent());
|
|
}
|
|
// 保存授权文件
|
|
Files.write(path, authContent.getBytes(StandardCharsets.UTF_8));
|
|
return ApiResponse.success(null);
|
|
} catch (Exception e) {
|
|
return ApiResponse.failure("导入授权文件失败:" + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@GetMapping("/show")
|
|
@Operation(summary = "查看授权信息")
|
|
public ApiResponse showAuth() {
|
|
try {
|
|
// 检查授权文件是否存在
|
|
Path path = Paths.get(AUTH_FILE_PATH);
|
|
if (!Files.exists(path)) {
|
|
return ApiResponse.failure("请先导入授权");
|
|
}
|
|
// 读取授权文件内容
|
|
String authContent = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
|
// 获取授权详情
|
|
AuthInfo authInfo = AuthValidator.getAuthInfo(authContent);
|
|
return ApiResponse.success(authInfo);
|
|
} catch (Exception e) {
|
|
return ApiResponse.failure("获取授权信息失败:" + e.getMessage());
|
|
}
|
|
}
|
|
}
|