Commit d5aa4b48 by 叶立庆

增加公共配置,路径前缀匹配

1 parent 3e425ab1
...@@ -27,6 +27,9 @@ COPY youjia-server/target/youjia-server.jar /app/app.jar ...@@ -27,6 +27,9 @@ COPY youjia-server/target/youjia-server.jar /app/app.jar
ENV YOUJIA_LOG_HOME=/app/logs \ ENV YOUJIA_LOG_HOME=/app/logs \
# JVM 参数(含 -Dfile.encoding=UTF-8,避免中文 Nacos 配置解析异常) # JVM 参数(含 -Dfile.encoding=UTF-8,避免中文 Nacos 配置解析异常)
JAVA_TOOL_OPTIONS="-server -Xms512m -Xmx1024m -XX:+UseG1GC -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai" \ JAVA_TOOL_OPTIONS="-server -Xms512m -Xmx1024m -XX:+UseG1GC -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai" \
# 请求前缀:需与 Nacos youjia-{env}.yaml 的 server.servlet.context-path 保持一致(默认 /youjia);
# 置空即表示不加前缀。同一个变量同时供 Spring(${SERVER_CONTEXT_PATH:/youjia})与健康检查使用
SERVER_CONTEXT_PATH=/youjia \
# 激活环境:dev / test / beta / release # 激活环境:dev / test / beta / release
SPRING_PROFILES_ACTIVE=release \ SPRING_PROFILES_ACTIVE=release \
# Nacos 连接(共享前缀写法,config 与 discovery 均继承) # Nacos 连接(共享前缀写法,config 与 discovery 均继承)
...@@ -39,7 +42,9 @@ ENV YOUJIA_LOG_HOME=/app/logs \ ...@@ -39,7 +42,9 @@ ENV YOUJIA_LOG_HOME=/app/logs \
EXPOSE 8080 EXPOSE 8080
# 免签 GET 接口,用于健康探测(无需鉴权 / 签名) # 免签 GET 接口,用于健康探测(无需鉴权 / 签名)
# 路径前缀取自容器环境变量 SERVER_CONTEXT_PATH(\$ 转义,避免构建期被 Docker 替换);
# 默认 /youjia,与 Nacos 的 server.servlet.context-path 保持一致,改前缀时无需改本文件
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
CMD curl -fsS http://localhost:8080/system/sign/public-key || exit 1 CMD curl -fsS "http://localhost:8080\${SERVER_CONTEXT_PATH-/youjia}/system/sign/public-key" || exit 1
ENTRYPOINT ["java", "-jar", "/app/app.jar"] ENTRYPOINT ["java", "-jar", "/app/app.jar"]
...@@ -159,8 +159,12 @@ java -jar youjia-server/target/youjia-server.jar ...@@ -159,8 +159,12 @@ java -jar youjia-server/target/youjia-server.jar
或直接在 IDE 中运行 `com.youjia.YouJiaApplication`,VM 参数填 `-Dspring.profiles.active=dev`。 或直接在 IDE 中运行 `com.youjia.YouJiaApplication`,VM 参数填 `-Dspring.profiles.active=dev`。
- 应用地址:http://localhost:8080 - 应用地址:http://localhost:8080/youjia
- 接口文档:http://localhost:8080/doc.html - 接口文档:http://localhost:8080/youjia/doc.html
> 前缀 `/youjia` 来自 `server.servlet.context-path`(Nacos `youjia-{env}.yaml` 中默认取值 `${SERVER_CONTEXT_PATH:/youjia}`,
> 可用环境变量 `SERVER_CONTEXT_PATH` 覆盖;置空即无前缀)。Docker 部署见 `docker-compose.yaml`,同一变量同时供 Spring 与健康检查使用。
> ⚠️ 前端 axios 的 `baseURL`、参与加签的 path 都必须带上该前缀,否则 STRICT 模式验签失败。
### 4.5 配置来源 ### 4.5 配置来源
......
...@@ -36,13 +36,20 @@ services: ...@@ -36,13 +36,20 @@ services:
- NACOS_PASSWORD=nacos - NACOS_PASSWORD=nacos
- NACOS_NAMESPACE= - NACOS_NAMESPACE=
- NACOS_GROUP=DEFAULT_GROUP - NACOS_GROUP=DEFAULT_GROUP
# 请求前缀:需与 Nacos youjia-{env}.yaml 的 server.servlet.context-path 保持一致(默认 /youjia);
# 用 ${VAR-default}(而非 :-)以便「显式置空」也能生效:
# SERVER_CONTEXT_PATH= docker compose up -d 即可去掉前缀
- SERVER_CONTEXT_PATH=${SERVER_CONTEXT_PATH-/youjia}
# 日志落盘目录 # 日志落盘目录
- YOUJIA_LOG_HOME=/app/logs - YOUJIA_LOG_HOME=/app/logs
volumes: volumes:
# 持久化应用日志 # 持久化应用日志
- youjia-logs:/app/logs - youjia-logs:/app/logs
# 免签 GET 接口,用于健康探测(无需鉴权 / 签名)。
# 路径前缀由容器环境变量 SERVER_CONTEXT_PATH 运行期展开:compose 中 $$ 会转义为字面 $,
# 再由容器 shell 取值,从而与 server.servlet.context-path 始终保持一致
healthcheck: healthcheck:
test: ["CMD", "curl", "-fsS", "http://localhost:8080/system/sign/public-key"] test: ["CMD-SHELL", "curl -fsS http://localhost:8080$${SERVER_CONTEXT_PATH-/youjia}/system/sign/public-key || exit 1"]
interval: 30s interval: 30s
timeout: 5s timeout: 5s
retries: 3 retries: 3
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
# 说明:带 TODO 的位置必须替换为预发布环境真实值后再导入 # 说明:带 TODO 的位置必须替换为预发布环境真实值后再导入
# ============================================================ # ============================================================
spring: spring:
servlet:
# 请求统一前缀:所有接口路径带 /youjia;部署侧用环境变量 SERVER_CONTEXT_PATH 覆盖,置空即无前缀
context-path: ${SERVER_CONTEXT_PATH:/youjia}
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
# TODO: 替换为预发布库地址(建议与生产同版本、同参数) # TODO: 替换为预发布库地址(建议与生产同版本、同参数)
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
# 说明:带 TODO 的位置必须替换为生产真实值后再导入;密钥请勿写在配置里,走挂载文件/环境变量 # 说明:带 TODO 的位置必须替换为生产真实值后再导入;密钥请勿写在配置里,走挂载文件/环境变量
# ============================================================ # ============================================================
spring: spring:
servlet:
# 请求统一前缀:所有接口路径带 /youjia;部署侧用环境变量 SERVER_CONTEXT_PATH 覆盖,置空即无前缀
context-path: ${SERVER_CONTEXT_PATH:/youjia}
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
# TODO: 替换为生产库地址(建议主从/高可用地址) # TODO: 替换为生产库地址(建议主从/高可用地址)
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
# 说明:带 TODO 的位置必须替换为测试环境真实值后再导入 # 说明:带 TODO 的位置必须替换为测试环境真实值后再导入
# ============================================================ # ============================================================
spring: spring:
servlet:
# 请求统一前缀:所有接口路径带 /youjia;部署侧用环境变量 SERVER_CONTEXT_PATH 覆盖,置空即无前缀
context-path: ${SERVER_CONTEXT_PATH:/youjia}
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
# TODO: 替换为测试库地址 # TODO: 替换为测试库地址
......
...@@ -167,12 +167,14 @@ COPY --from=build /workspace/youjia-server/target/youjia-server.jar /app/app.jar ...@@ -167,12 +167,14 @@ COPY --from=build /workspace/youjia-server/target/youjia-server.jar /app/app.jar
docker ps docker ps
# 手动探测免签接口(返回公钥 JSON 即正常) # 手动探测免签接口(返回公钥 JSON 即正常)
curl -fsS http://localhost:8080/system/sign/public-key # 前缀 /youjia 取自 server.servlet.context-path(容器环境变量 SERVER_CONTEXT_PATH,默认 /youjia);
# 若该环境未配置前缀,去掉 /youjia 即可
curl -fsS http://localhost:8080/youjia/system/sign/public-key
``` ```
### 6.2 接口文档 ### 6.2 接口文档
浏览器访问 `http://<服务器IP>:8080/doc.html`,在右上角分组下拉切换: 浏览器访问 `http://<服务器IP>:8080/youjia/doc.html`,在右上角分组下拉切换:
- **门店上架** → `com.youjia.store` - **门店上架** → `com.youjia.store`
- **系统设置** → `com.youjia.system` - **系统设置** → `com.youjia.system`
- **基础框架** → `com.youjia.framework`(含 OSS、签名接口) - **基础框架** → `com.youjia.framework`(含 OSS、签名接口)
......
...@@ -49,8 +49,10 @@ public class AuthInterceptor implements HandlerInterceptor { ...@@ -49,8 +49,10 @@ public class AuthInterceptor implements HandlerInterceptor {
if (handlerMethod != null && hasAnnotation(handlerMethod, AuthIgnore.class)) { if (handlerMethod != null && hasAnnotation(handlerMethod, AuthIgnore.class)) {
return true; return true;
} }
// ⚠️ requestURI 含 context-path(如 /youjia/doc.html),而白名单通常按接口路径书写(/doc.html),
// 故同时用「完整 URI」与「剥离 context-path 后的 URI」两路匹配,配置带不带前缀都能命中
String uri = request.getRequestURI(); String uri = request.getRequestURI();
if (matchIgnoreUrls(uri)) { if (matchIgnoreUrls(uri, stripContextPath(uri, request.getContextPath()))) {
return true; return true;
} }
...@@ -124,19 +126,43 @@ public class AuthInterceptor implements HandlerInterceptor { ...@@ -124,19 +126,43 @@ public class AuthInterceptor implements HandlerInterceptor {
return StringUtils.hasText(token) ? token : null; return StringUtils.hasText(token) ? token : null;
} }
private boolean matchIgnoreUrls(String uri) { /**
* 免鉴权白名单匹配:pattern 与「完整 URI」「剥离 context-path 的 URI」任一命中即放行。
* 这样配置了 context-path(如 /youjia)后,白名单无需同步加前缀。
*/
private boolean matchIgnoreUrls(String uri, String uriWithoutContextPath) {
List<String> patterns = authProperties.getIgnoreUrls(); List<String> patterns = authProperties.getIgnoreUrls();
if (CollectionUtils.isEmpty(patterns)) { if (CollectionUtils.isEmpty(patterns)) {
return false; return false;
} }
for (String pattern : patterns) { for (String pattern : patterns) {
if (PATH_MATCHER.match(pattern, uri)) { if (pattern == null || pattern.isBlank()) {
continue;
}
String target = pattern.trim();
if (PATH_MATCHER.match(target, uri) || PATH_MATCHER.match(target, uriWithoutContextPath)) {
return true; return true;
} }
} }
return false; return false;
} }
/** 去掉 context-path;context-path 为空或「/」时原样返回 */
private static String stripContextPath(String uri, String contextPath) {
if (uri == null) {
return "";
}
if (contextPath == null || contextPath.isBlank() || "/".equals(contextPath)) {
return uri;
}
String prefix = contextPath.endsWith("/") ? contextPath.substring(0, contextPath.length() - 1) : contextPath;
if (uri.startsWith(prefix) && uri.length() >= prefix.length()) {
String rest = uri.substring(prefix.length());
return rest.startsWith("/") ? rest : "/" + rest;
}
return uri;
}
private static <A extends java.lang.annotation.Annotation> boolean hasAnnotation(HandlerMethod hm, Class<A> type) { private static <A extends java.lang.annotation.Annotation> boolean hasAnnotation(HandlerMethod hm, Class<A> type) {
return hm.getMethodAnnotation(type) != null || hm.getBeanType().getAnnotation(type) != null; return hm.getMethodAnnotation(type) != null || hm.getBeanType().getAnnotation(type) != null;
} }
......
...@@ -51,10 +51,17 @@ public class AuthProperties { ...@@ -51,10 +51,17 @@ public class AuthProperties {
/** 角色权限变更时是否直接踢下线;默认 false —— 只失效权限缓存,保留登录态 */ /** 角色权限变更时是否直接踢下线;默认 false —— 只失效权限缓存,保留登录态 */
private boolean kickOnRoleChange = false; private boolean kickOnRoleChange = false;
/** 免鉴权白名单(支持 /** 与 /* 通配) */ /**
* 免鉴权白名单(支持 /** 与 /* 通配)。
*
* <p>写法兼容两种形式,任选其一即可:带 context-path 的完整路径 {@code /youjia/doc.html},
* 或不带前缀的接口路径 {@code /doc.html} —— 匹配时会同时用「完整 URI」与
* 「剥离 context-path 后的 URI」比对,因此配置 context-path 后无需同步改白名单。</p>
*/
private List<String> ignoreUrls = new ArrayList<>(List.of( private List<String> ignoreUrls = new ArrayList<>(List.of(
"/system/auth/login", "/system/auth/login",
"/doc.html", "/doc.html",
"/swagger-ui.html",
"/webjars/**", "/webjars/**",
"/v3/api-docs/**", "/v3/api-docs/**",
"/swagger-ui/**", "/swagger-ui/**",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!