feat:暂存
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
package org.nl.api;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 激活参数
|
||||||
|
* @Author: lyd
|
||||||
|
* @Date: 2025/9/23
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class LicenseActivityParam implements Serializable {
|
||||||
|
private String code;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.nl.api;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.nl.core.LicenseResult;
|
||||||
|
import org.nl.core.LicenseVerifier;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主要做的是激活的功能
|
||||||
|
* @Author: lyd
|
||||||
|
* @Date: 2025/9/22
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/verify")
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class LicenseController {
|
||||||
|
|
||||||
|
private final LicenseVerifier verifier;
|
||||||
|
|
||||||
|
@PostMapping("/activity")
|
||||||
|
public ResponseEntity<Object> activity(@RequestBody LicenseActivityParam param) {
|
||||||
|
if (null == param.getCode()) {
|
||||||
|
throw new RuntimeException("激活码不能为空!");
|
||||||
|
}
|
||||||
|
// 校验激活码是否正确
|
||||||
|
LicenseResult verify = verifier.verify(param.getCode());
|
||||||
|
if (true == verify.getResult()) {
|
||||||
|
// 存到文件中
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(verify, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,8 +28,8 @@ public class LicenseAutoConfiguration implements WebMvcConfigurer {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public LicenseVerifier licenseVerifier(RSAUtil rsaUtil) {
|
public LicenseVerifier licenseVerifier(RSAUtil rsaUtil, LicenseProperties properties) {
|
||||||
return new LicenseVerifier(rsaUtil);
|
return new LicenseVerifier(rsaUtil, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public class LicenseProperties {
|
|||||||
private String header = "cdk";
|
private String header = "cdk";
|
||||||
/** 私钥资源位置,默认 classpath:private_key.txt */
|
/** 私钥资源位置,默认 classpath:private_key.txt */
|
||||||
private String privateKeyLocation = "classpath:private_key.txt";
|
private String privateKeyLocation = "classpath:private_key.txt";
|
||||||
|
private String sdkLocation = "classpath:sdk.txt";
|
||||||
|
|
||||||
public String getHeader() {
|
public String getHeader() {
|
||||||
return header;
|
return header;
|
||||||
@@ -29,4 +30,12 @@ public class LicenseProperties {
|
|||||||
public void setPrivateKeyLocation(String privateKeyLocation) {
|
public void setPrivateKeyLocation(String privateKeyLocation) {
|
||||||
this.privateKeyLocation = privateKeyLocation;
|
this.privateKeyLocation = privateKeyLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSdkLocation() {
|
||||||
|
return sdkLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSdkLocation(String sdkLocation) {
|
||||||
|
this.sdkLocation = sdkLocation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,11 @@ import java.time.format.DateTimeFormatter;
|
|||||||
public class LicenseVerifier {
|
public class LicenseVerifier {
|
||||||
private final RSAUtil rsaUtil;
|
private final RSAUtil rsaUtil;
|
||||||
|
|
||||||
public LicenseVerifier(RSAUtil rsaUtil) {
|
private final LicenseProperties properties;
|
||||||
|
|
||||||
|
public LicenseVerifier(RSAUtil rsaUtil, LicenseProperties properties) {
|
||||||
this.rsaUtil = rsaUtil;
|
this.rsaUtil = rsaUtil;
|
||||||
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LicenseResult verify(String authCode) {
|
public LicenseResult verify(String authCode) {
|
||||||
@@ -23,15 +26,24 @@ public class LicenseVerifier {
|
|||||||
try {
|
try {
|
||||||
String expirationStr = rsaUtil.decrypt(authCode);
|
String expirationStr = rsaUtil.decrypt(authCode);
|
||||||
if ("-1".equals(expirationStr)) {
|
if ("-1".equals(expirationStr)) {
|
||||||
return LicenseResult.requestOk("永久授权", expirationStr);
|
return LicenseResult.requestOk("永久授权", authCode);
|
||||||
}
|
}
|
||||||
LocalDate expirationDate = LocalDate.parse(expirationStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
LocalDate expirationDate = LocalDate.parse(expirationStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
if (LocalDate.now().isAfter(expirationDate)) {
|
if (LocalDate.now().isAfter(expirationDate)) {
|
||||||
return LicenseResult.requestResult(false, "授权码已到期", expirationStr);
|
return LicenseResult.requestResult(false, "授权码已到期", authCode);
|
||||||
}
|
}
|
||||||
return LicenseResult.requestOk("授权码有效", expirationStr);
|
return LicenseResult.requestOk("授权码有效", authCode);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return LicenseResult.requestResult(false, "解密失败或格式错误", null);
|
return LicenseResult.requestResult(false, "解密失败或格式错误", authCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void install(String authCode) {
|
||||||
|
// 存储SDK到文件中(没有文件则创建)
|
||||||
|
String sdkLocation = properties.getSdkLocation();
|
||||||
|
|
||||||
|
}
|
||||||
|
public void unInstall(LicenseResult result) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,9 @@ package org.nl.util;
|
|||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.util.ResourceUtils;
|
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -37,8 +35,8 @@ public class RSAUtil {
|
|||||||
if (privateKey == null) {
|
if (privateKey == null) {
|
||||||
Resource resource = resourceLoader.getResource(privateKeyLocation);
|
Resource resource = resourceLoader.getResource(privateKeyLocation);
|
||||||
try (InputStream is = resource.getInputStream();
|
try (InputStream is = resource.getInputStream();
|
||||||
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
||||||
BufferedReader reader = new BufferedReader(isr)) {
|
BufferedReader reader = new BufferedReader(isr)) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user