feat:授权码通过文件读取
This commit is contained in:
@@ -26,8 +26,9 @@ public class LicenseController {
|
||||
}
|
||||
// 校验激活码是否正确
|
||||
LicenseResult verify = verifier.verify(param.getCode());
|
||||
if (true == verify.getResult()) {
|
||||
if (verify.getResult()) {
|
||||
// 存到文件中
|
||||
verifier.install(param.getCode());
|
||||
}
|
||||
return new ResponseEntity<>(verify, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
@@ -21,9 +25,25 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String headerName = properties.getHeader();
|
||||
String authCode = request.getHeader(headerName);
|
||||
LicenseResult result = verifier.verify(authCode);
|
||||
String sdkLocation = properties.getSdkLocation();
|
||||
|
||||
LicenseResult result;
|
||||
try {
|
||||
if (sdkLocation == null || sdkLocation.trim().isEmpty()) {
|
||||
result = LicenseResult.requestResult(false, "授权码失败", null);
|
||||
} else {
|
||||
Path path = Paths.get(sdkLocation);
|
||||
if (!Files.exists(path)) {
|
||||
result = LicenseResult.requestResult(false, "授权码失败", null);
|
||||
} else {
|
||||
// 考虑存内存
|
||||
String authCode = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim();
|
||||
result = verifier.verify(authCode);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result = LicenseResult.requestResult(false, "授权码失败", null);
|
||||
}
|
||||
if (Boolean.TRUE.equals(result.getResult())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class LicenseAutoConfiguration implements WebMvcConfigurer {
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/auth/login", "/auth/code", "/auth/logout", "/auth/info");
|
||||
.excludePathPatterns("/auth/login", "/auth/code", "/auth/logout", "/auth/info", "/api/verify/activity");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class LicenseProperties {
|
||||
private String header = "cdk";
|
||||
/** 私钥资源位置,默认 classpath:private_key.txt */
|
||||
private String privateKeyLocation = "classpath:private_key.txt";
|
||||
private String sdkLocation = "classpath:sdk.txt";
|
||||
private String sdkLocation = "C:/Code/sdk.txt";
|
||||
|
||||
public String getHeader() {
|
||||
return header;
|
||||
|
||||
@@ -2,6 +2,10 @@ package org.nl.core;
|
||||
|
||||
import org.nl.util.RSAUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@@ -41,9 +45,23 @@ public class LicenseVerifier {
|
||||
public void install(String authCode) {
|
||||
// 存储SDK到文件中(没有文件则创建)
|
||||
String sdkLocation = properties.getSdkLocation();
|
||||
|
||||
}
|
||||
public void unInstall(LicenseResult result) {
|
||||
|
||||
if (sdkLocation == null || sdkLocation.trim().isEmpty()) {
|
||||
throw new IllegalStateException("sdkLocation 未配置");
|
||||
}
|
||||
try {
|
||||
Path targetPath = Paths.get(sdkLocation);
|
||||
Path parent = targetPath.getParent();
|
||||
if (parent != null && !Files.exists(parent)) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
Files.write(
|
||||
targetPath,
|
||||
(authCode == null ? "" : authCode).getBytes(java.nio.charset.StandardCharsets.UTF_8),
|
||||
java.nio.file.StandardOpenOption.CREATE,
|
||||
java.nio.file.StandardOpenOption.TRUNCATE_EXISTING
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("写入授权码失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.Base64;
|
||||
public class GenerateAuthCode {
|
||||
public static void main(String[] args) throws Exception {
|
||||
String publicKeyBase64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgW1kN/JmXZjl9+/Zsl/zRZ/6hSHj95G6IA4lpRrXNSTbeng0rDhEoVDY96PCaOUGnqcqOo+LLT+FqZggFBbHcjqHc1NEetD+4BTkdTiHSsskNjkvSdTHTV0UbpLHw9VlRvms6AfobkKNg+dm3F0o57tVA0Q61zd112YIdS9lf2g4PhovUACLDAx7TetsyrB7OLZQpR7dCjkUnQMTfUYz81BTWF38Vt3TUzPc35baDNm4HlHxutReRDIqZABFYw2+Pj7tgbDVGpo8vOXig1gPDcQ1Orrg5MSzYi8eSoqS8TkfhDOP3DqkcB3RI/JmraRpagUjnzsV3ekoeV5lUrQY7QIDAQAB"; // 从生成器中获取
|
||||
String expirationDate = "2025-12-31"; // 到期时间原文
|
||||
String expirationDate = "2025-06-31"; // 到期时间原文
|
||||
|
||||
// 还原公钥
|
||||
byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyBase64);
|
||||
|
||||
Reference in New Issue
Block a user