init: 简单的验证授权

This commit is contained in:
2025-08-26 17:38:24 +08:00
commit 4350a3894d
13 changed files with 369 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nl</groupId>
<artifactId>nl-verify-check</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>nl-verify-check-sdk</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,51 @@
package org.nl.core;
import com.alibaba.fastjson.JSONObject;
import org.nl.util.RSAUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* @Author: lyd
* @Date: 2025/8/26
*/
public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String authCode = request.getHeader("cdk"); // 假设从 Header 获取
if (authCode == null || authCode.isEmpty()) {
response.setContentType("text/html;charset=UTF-8");
response.setStatus(402);
response.getWriter().write(JSONObject.toJSONString(LicenseResult.requestResult(false, "请收入授权码", null)));
return false;
}
String expirationStr ="";
try {
// 解密获取到期时间
expirationStr = RSAUtil.decrypt(authCode);
if ("-1".equals(expirationStr)) {
return true;
}
LocalDate expirationDate = LocalDate.parse(expirationStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
if (LocalDate.now().isAfter(expirationDate)) {
response.setContentType("text/html;charset=UTF-8");
response.setStatus(402);
response.getWriter().write(JSONObject.toJSONString(LicenseResult.requestResult(false, "授权码已到期", null)));
return false;
}
} catch (Exception e) {
// 解密失败或格式错误
response.setContentType("text/html;charset=UTF-8");
response.setStatus(402);
response.getWriter().write(JSONObject.toJSONString(LicenseResult.requestResult(false, "解密失败或格式错误", null)));
return false;
}
return true;
}
}

View File

@@ -0,0 +1,33 @@
package org.nl.core;
import lombok.Builder;
import lombok.Data;
/**
* @Author: lyd
* @Date: 2025/8/14
*/
@Data
@Builder
public class LicenseResult {
/** 检验结果 */
private Boolean result;
/** 附加信息 */
private String message;
/** 证书key */
private String content;
public static LicenseResult requestResult(Boolean result, String message, String content) {
return LicenseResult.builder()
.message(message)
.result(result)
.content(content)
.build();
}
public static LicenseResult requestOk(String message, String content) {
return LicenseResult.builder()
.message(message)
.result(true)
.content(content)
.build();
}
}

View File

@@ -0,0 +1,17 @@
package org.nl.core;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author: lyd
* @Date: 2025/8/26
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/**");
}
}

View File

@@ -0,0 +1,47 @@
package org.nl.util;
import org.springframework.util.ResourceUtils;
import javax.crypto.Cipher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
/**
* @Author: lyd
* @Date: 2025/8/13
*/
public class RSAUtil {
private static PrivateKey privateKey;
public static synchronized PrivateKey getPrivateKey() throws Exception {
if (privateKey == null) {
// 从 resources/private_key.txt 读取 Base64
String filePath = ResourceUtils.getFile("classpath:private_key.txt").getPath();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
byte[] privateKeyBytes = Base64.getDecoder().decode(sb.toString());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(keySpec);
}
return privateKey;
}
public static String decrypt(String authCode) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(authCode);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
byte[] decrypted = cipher.doFinal(encryptedBytes);
return new String(decrypted, "UTF-8");
}
}

View File

@@ -0,0 +1 @@
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCBbWQ38mZdmOX379myX/NFn/qFIeP3kbogDiWlGtc1JNt6eDSsOEShUNj3o8Jo5Qaepyo6j4stP4WpmCAUFsdyOodzU0R60P7gFOR1OIdKyyQ2OS9J1MdNXRRuksfD1WVG+azoB+huQo2D52bcXSjnu1UDRDrXN3XXZgh1L2V/aDg+Gi9QAIsMDHtN62zKsHs4tlClHt0KORSdAxN9RjPzUFNYXfxW3dNTM9zfltoM2bgeUfG61F5EMipkAEVjDb4+Pu2BsNUamjy85eKDWA8NxDU6uuDkxLNiLx5KipLxOR+EM4/cOqRwHdEj8matpGlqBSOfOxXd6Sh5XmVStBjtAgMBAAECggEAQCbcme6IVrRGqJI2MXfluQkGv56AxGFzBBh/CEs5iJnwP8/9K6/oNJ1CLdz5q8x5b4IkKEqmDZOCyQEiRVLVIQVpxfvr4YReEOvKIWAXjzcJh+boTYwuDWapjfUrFyJaxMdUsN3ak2xhgJPe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