资源相关
This commit is contained in:
99
src/main/java/com/yj/earth/aspect/AuthAspect.java
Normal file
99
src/main/java/com/yj/earth/aspect/AuthAspect.java
Normal file
@ -0,0 +1,99 @@
|
||||
package com.yj.earth.aspect;
|
||||
|
||||
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 org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 授权验证切面
|
||||
* 实现授权验证注解的功能
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class AuthAspect {
|
||||
|
||||
// 授权文件路径
|
||||
private static final String LICENSE_FILE_PATH = "license/yjearth.lic";
|
||||
|
||||
@Pointcut("@annotation(com.yj.earth.annotation.CheckAuth)")
|
||||
public void authPointCut() {
|
||||
}
|
||||
|
||||
@Around("authPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
// 读取授权文件内容
|
||||
String authString = readLicenseFile();
|
||||
|
||||
// 检查授权文件是否存在
|
||||
if (authString == null || authString.trim().isEmpty()) {
|
||||
return ApiResponse.failure("未导入授权");
|
||||
}
|
||||
|
||||
// 获取当前服务器硬件信息
|
||||
String currentServerHardwareMd5 = ServerUniqueIdUtil.getServerUniqueId();
|
||||
|
||||
// 验证授权是否有效
|
||||
boolean isValid = AuthValidator.validateAuth(authString, currentServerHardwareMd5);
|
||||
if (!isValid) {
|
||||
// 进一步判断是未授权还是已过期
|
||||
try {
|
||||
AuthInfo authInfo = AuthValidator.getAuthInfo(authString);
|
||||
// 检查是否过期
|
||||
if (new java.util.Date().after(authInfo.getExpireTime())) {
|
||||
return ApiResponse.failure("授权已过期");
|
||||
} else {
|
||||
return ApiResponse.failure("未授权");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ApiResponse.failure("未授权");
|
||||
}
|
||||
}
|
||||
|
||||
// 授权有效,继续执行原方法
|
||||
return point.proceed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取授权文件内容
|
||||
* @return 授权文件内容,如果文件不存在或读取失败则返回null
|
||||
*/
|
||||
private String readLicenseFile() {
|
||||
try {
|
||||
// 尝试从类路径下读取
|
||||
ClassPathResource resource = new ClassPathResource(LICENSE_FILE_PATH);
|
||||
if (resource.exists()) {
|
||||
try (FileInputStream inputStream = new FileInputStream(resource.getFile())) {
|
||||
byte[] bytes = new byte[inputStream.available()];
|
||||
inputStream.read(bytes);
|
||||
return new String(bytes, StandardCharsets.UTF_8).trim();
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从文件系统读取
|
||||
File file = new File(LICENSE_FILE_PATH);
|
||||
if (file.exists() && file.isFile()) {
|
||||
try (FileInputStream inputStream = new FileInputStream(file)) {
|
||||
byte[] bytes = new byte[inputStream.available()];
|
||||
inputStream.read(bytes);
|
||||
return new String(bytes, StandardCharsets.UTF_8).trim();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user