|
|
@@ -3,11 +3,19 @@ package com.usky.license.client;
|
|
|
import com.usky.common.core.exception.BusinessException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.server.PathContainer;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+import org.springframework.web.util.pattern.PathPattern;
|
|
|
+import org.springframework.web.util.pattern.PathPatternParser;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
@@ -16,19 +24,60 @@ public class LicenseCheckInterceptor implements HandlerInterceptor {
|
|
|
@Autowired
|
|
|
private LicenseVerify licenseVerify;
|
|
|
|
|
|
+ @Value("${license.addpaths}")
|
|
|
+ private String addpaths;
|
|
|
+
|
|
|
+ @Value("${license.excludepaths}")
|
|
|
+ private String excludepaths;
|
|
|
+
|
|
|
+ private List<PathPattern> licensePaths;
|
|
|
+ private List<PathPattern> excludePaths;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ // 解析 addpaths 为 PathPattern 列表
|
|
|
+ PathPatternParser parser = new PathPatternParser();
|
|
|
+ licensePaths = Arrays.stream(addpaths.split(","))
|
|
|
+ .map(String::trim) // 去掉多余的空格
|
|
|
+ .map(path -> parser.parse(path))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 解析 excludepaths 为 PathPattern 列表
|
|
|
+ excludePaths = Arrays.stream(excludepaths.split(","))
|
|
|
+ .map(String::trim) // 去掉多余的空格
|
|
|
+ .map(path -> parser.parse(path))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
- // 临时添加:打印 licenseVerify 是否为 null
|
|
|
- if (licenseVerify == null) {
|
|
|
- log.error("LicenseVerify 注入失败,为 null!");
|
|
|
- throw new BusinessException("License 验证组件加载失败");
|
|
|
+ // 获取当前请求的 URI
|
|
|
+ String requestURI = request.getRequestURI();
|
|
|
+ log.info("当前请求 URI: {}", requestURI);
|
|
|
+
|
|
|
+ // 检查是否在排除路径中
|
|
|
+ PathContainer pathContainer = PathContainer.parsePath(requestURI);
|
|
|
+ for (PathPattern excludePath : excludePaths) {
|
|
|
+ if (excludePath.matches(pathContainer)) {
|
|
|
+ return true; // 不拦截
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- boolean verifyResult = licenseVerify.verify();
|
|
|
- if (!verifyResult) {
|
|
|
- log.warn("License 无效,拒绝服务");
|
|
|
- throw new BusinessException("License 无效,请检查证书是否授权或已过期");
|
|
|
+ // 检查是否在拦截路径中
|
|
|
+ boolean isMatched = licensePaths.stream()
|
|
|
+ .anyMatch(pathPattern -> pathPattern.matches(pathContainer));
|
|
|
+
|
|
|
+ if (isMatched) {
|
|
|
+ log.info("请求 URI 匹配拦截路径,开始验证 License");
|
|
|
+ boolean verifyResult = licenseVerify.verify();
|
|
|
+ if (!verifyResult) {
|
|
|
+ log.warn("License 无效,拒绝服务");
|
|
|
+ throw new BusinessException("License 无效,请检查证书是否授权或已过期");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("请求 URI 不在拦截路径中,直接放行");
|
|
|
}
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
-}
|
|
|
+}
|