security(P0①): 安全链改 fail-safe 默认 + prod 配置 + JWT 弱密钥守卫
宽松 permitAll 链由 @Profile("!prod") 改为显式 opt-in @Profile(dev|local|ci|parity);严格认证链覆盖默认(含裸 java -jar)与 prod。新增 SecurityProfiles 作旁路名单唯一真相、JwtSecretSafetyCheck(严格 profile 下拒开发默认密钥启动)、application-prod.yml(密钥/DB 无默认强制环境变量)。修 2026-06-24 架构评审 P0:未设 profile 即全放行的 fail-open 默认。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
54
platform-app/src/main/resources/application-prod.yml
Normal file
54
platform-app/src/main/resources/application-prod.yml
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
# ============================================================================
|
||||||
|
# 生产 profile(P0 修复,2026-06-24 架构评审:补齐缺失的 prod 配置)。
|
||||||
|
#
|
||||||
|
# 启用方式:-Dspring.profiles.active=prod(或 SPRING_PROFILES_ACTIVE=prod)。
|
||||||
|
# 设计原则 fail-safe:所有敏感项<strong>无内置默认</strong>,必须经环境变量显式提供,
|
||||||
|
# 缺失即启动失败——杜绝继承 application.yml 的开发默认值(共享测试库 / 弱 JWT 密钥)裸奔进生产。
|
||||||
|
#
|
||||||
|
# 安全链:prod 不在 SecurityProfiles 旁路名单内 → 自动走严格链(其余 /api/** 强制认证),
|
||||||
|
# 且 JwtSecretSafetyCheck 在此 profile 下拒绝开发默认 JWT 密钥。
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
# 生产库连接必须经环境变量提供,无默认值(缺失 → 占位符解析失败 → 启动拒绝)。
|
||||||
|
url: ${EIP4_DB_URL}
|
||||||
|
username: ${EIP4_DB_USERNAME}
|
||||||
|
password: ${EIP4_DB_PASSWORD}
|
||||||
|
hikari:
|
||||||
|
maximum-pool-size: ${EIP4_DB_POOL_MAX:20}
|
||||||
|
read-only: false
|
||||||
|
connection-init-sql: "SET statement_timeout = '${EIP4_DB_STATEMENT_TIMEOUT:120s}'"
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: ${SERVER_PORT:8084}
|
||||||
|
|
||||||
|
eip4:
|
||||||
|
auth:
|
||||||
|
jwt:
|
||||||
|
# 生产 JWT 密钥必须经环境变量提供,无默认值(故意去掉 application.yml 的 dev 默认回退)。
|
||||||
|
# 缺失 → 占位符解析失败启动拒绝;若误填为开发默认值 → JwtSecretSafetyCheck 拒绝启动。
|
||||||
|
secret: ${EIP4_AUTH_JWT_SECRET}
|
||||||
|
expire-seconds: ${EIP4_AUTH_JWT_EXPIRE_SECONDS:28800}
|
||||||
|
issuer: eip4-platform
|
||||||
|
file:
|
||||||
|
# 生产附件根必须为绝对路径,经环境变量提供。
|
||||||
|
root: ${EIP4_FILE_ROOT}
|
||||||
|
|
||||||
|
# 可观测性:生产仅暴露健康探针,详情仅对已认证者可见(匿名只见 UP/DOWN)。
|
||||||
|
management:
|
||||||
|
endpoints:
|
||||||
|
web:
|
||||||
|
exposure:
|
||||||
|
include: health
|
||||||
|
endpoint:
|
||||||
|
health:
|
||||||
|
probes:
|
||||||
|
enabled: true
|
||||||
|
show-details: when-authorized
|
||||||
|
show-components: when-authorized
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
com.dsasp: INFO
|
||||||
|
org.springframework.jdbc.core: INFO
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.eip4.platform.auth.security;
|
||||||
|
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT 弱密钥启动守卫(P0,2026-06-24 架构评审)。
|
||||||
|
*
|
||||||
|
* <p>{@code application.yml} 给出「非生产默认密钥」
|
||||||
|
* ({@value #DEV_DEFAULT_SECRET})供 dev/local/ci 开箱即用。风险:若生产(或任何
|
||||||
|
* 非旁路 profile)忘记经 {@code EIP4_AUTH_JWT_SECRET} 覆盖,该默认密钥会静默签发可伪造令牌。
|
||||||
|
*
|
||||||
|
* <p>本守卫在启动期判定:激活的是<strong>严格 profile</strong>
|
||||||
|
* ({@link SecurityProfiles#isBypassActive(Environment)} 为 false,含 prod 与未设 profile)
|
||||||
|
* 却仍使用开发默认密钥 → <strong>启动即失败</strong>,杜绝弱密钥裸奔进生产。
|
||||||
|
*
|
||||||
|
* <p>旁路 profile(dev/local/ci/parity)下跳过本检查,保留默认密钥开箱即用。
|
||||||
|
* {@link JwtService} 仍独立校验密钥存在且 ≥256bit;本守卫只补「默认值不得进严格环境」一条。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class JwtSecretSafetyCheck {
|
||||||
|
|
||||||
|
/** application.yml 内置的非生产默认密钥;严格 profile 下出现即拒启动。 */
|
||||||
|
static final String DEV_DEFAULT_SECRET =
|
||||||
|
"dev-only-eip4-jwt-secret-change-me-in-production-0123456789";
|
||||||
|
|
||||||
|
public JwtSecretSafetyCheck(JwtProperties props, Environment env) {
|
||||||
|
if (!SecurityProfiles.isBypassActive(env)
|
||||||
|
&& DEV_DEFAULT_SECRET.equals(props.getSecret())) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"严格安全 profile(prod 或未设旁路 profile)下仍使用 application.yml 的开发默认 JWT 密钥;"
|
||||||
|
+ "生产部署必须经环境变量 EIP4_AUTH_JWT_SECRET 设置强随机密钥(≥32 字节),"
|
||||||
|
+ "或仅在 dev/local/ci/parity profile 下使用默认密钥。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,15 +15,20 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|||||||
/**
|
/**
|
||||||
* Spring Security 配置(无状态,STATELESS:不建 HttpSession,免 session 共享)。
|
* Spring Security 配置(无状态,STATELESS:不建 HttpSession,免 session 共享)。
|
||||||
*
|
*
|
||||||
* <p><strong>双链按 profile 装配</strong>:
|
* <p><strong>双链按 profile 装配(fail-safe 默认)</strong>:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><b>prod</b>(生产):严格链——{@code /api/auth/**}、swagger/openapi 放行,
|
* <li><b>严格链(默认)</b>:旁路 profile({@link SecurityProfiles#BYPASS_NAMES})<em>全不激活</em>时
|
||||||
* 其余 {@code /api/**} 必须认证;<em>无任何测试旁路</em>。</li>
|
* 装配——含生产 {@code prod} <strong>以及未设任何 profile 的裸启动</strong>。
|
||||||
* <li><b>非 prod</b>(default/local/ci 等):宽松链——保留 {@code permitAll} 测试通道,
|
* {@code /api/auth/**}、swagger/openapi、健康探针放行,其余 {@code /api/**} 必须认证;
|
||||||
* 使对拍框架的免令牌 + {@code asUser/roles} 旁路可用(CI 依赖)。JWT 过滤器仍挂载,
|
* <em>无任何测试旁路</em>。</li>
|
||||||
* 带令牌照常填充引擎上下文。</li>
|
* <li><b>宽松链(显式 opt-in)</b>:仅 {@code dev/local/ci/parity} 之一激活时装配——保留
|
||||||
|
* {@code permitAll} 测试通道,使对拍框架的免令牌 + {@code asUser/roles} 旁路可用(CI 依赖)。
|
||||||
|
* JWT 过滤器仍挂载,带令牌照常填充引擎上下文。</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* 「生产绝不放行」由 {@code @Profile} 条件装配硬保证,并由 {@code SecurityProfileTest} 断言。
|
* P0 修复(2026-06-24 架构评审):旧实现宽松链为 {@code @Profile("!prod")},未设 profile 即 fail-open
|
||||||
|
* 全放行。现改为旁路<strong>显式 opt-in</strong>,默认 fail-safe(忘设 profile = 强制认证)。
|
||||||
|
* 旁路 profile 名单集中在 {@link SecurityProfiles},「生产/默认绝不放行」由 {@code @Profile} 条件装配
|
||||||
|
* 硬保证,并由 {@code SecurityProfileTest} 断言。
|
||||||
*
|
*
|
||||||
* <p>两链共享的放行清单与无状态/无表单/无 CSRF 设定抽到 {@link #applyCommon}。
|
* <p>两链共享的放行清单与无状态/无表单/无 CSRF 设定抽到 {@link #applyCommon}。
|
||||||
*/
|
*/
|
||||||
@@ -46,9 +51,9 @@ public class SecurityConfig {
|
|||||||
this.roleResolver = roleResolver;
|
this.roleResolver = roleResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 生产链:其余 /api/** 一律需认证,无测试旁路。 */
|
/** 严格链(默认 + prod):旁路 profile 全不激活时装配,其余 /api/** 一律需认证,无测试旁路。 */
|
||||||
@Bean
|
@Bean
|
||||||
@Profile("prod")
|
@Profile(SecurityProfiles.STRICT)
|
||||||
SecurityFilterChain prodFilterChain(HttpSecurity http) throws Exception {
|
SecurityFilterChain prodFilterChain(HttpSecurity http) throws Exception {
|
||||||
applyCommon(http);
|
applyCommon(http);
|
||||||
http.authorizeHttpRequests(reg -> reg
|
http.authorizeHttpRequests(reg -> reg
|
||||||
@@ -62,11 +67,11 @@ public class SecurityConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 非生产链(default/local/ci):保留 permitAll 测试通道。
|
* 宽松链(显式 opt-in:dev/local/ci/parity):保留 permitAll 测试通道。
|
||||||
* JWT 过滤器照常挂载——带令牌走 JWT 上下文,不带令牌走 asUser/roles 旁路(对拍 CI 依赖)。
|
* JWT 过滤器照常挂载——带令牌走 JWT 上下文,不带令牌走 asUser/roles 旁路(对拍 CI 依赖)。
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@Profile("!prod")
|
@Profile(SecurityProfiles.BYPASS)
|
||||||
SecurityFilterChain devFilterChain(HttpSecurity http) throws Exception {
|
SecurityFilterChain devFilterChain(HttpSecurity http) throws Exception {
|
||||||
applyCommon(http);
|
applyCommon(http);
|
||||||
http.authorizeHttpRequests(reg -> reg.anyRequest().permitAll());
|
http.authorizeHttpRequests(reg -> reg.anyRequest().permitAll());
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.eip4.platform.auth.security;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全旁路 profile 的<strong>唯一真相</strong>。
|
||||||
|
*
|
||||||
|
* <p>历史问题(P0,2026-06-24 架构评审):宽松安全链曾用 {@code @Profile("!prod")},即
|
||||||
|
* <em>未显式设 prod 的一切场景</em>(含「裸 {@code java -jar} 未设任何 profile」)都开
|
||||||
|
* {@code permitAll} + {@code asUser/roles} 旁路 → fail-open 默认。忘记设 profile = 全放行。
|
||||||
|
*
|
||||||
|
* <p>修复:把旁路改为<strong>显式 opt-in</strong>。仅 {@link #BYPASS_NAMES} 里的 profile
|
||||||
|
* (开发/对拍/CI)开启免令牌旁路;其余一切(prod、<em>以及未设任何 profile</em>)走严格链。
|
||||||
|
* 即默认 fail-safe——忘记设 profile = 强制认证,而非全放行。
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code dev} —— 本机开发(run-dev.sh 默认)</li>
|
||||||
|
* <li>{@code local} —— 本机集成测试(@ActiveProfiles("local"))</li>
|
||||||
|
* <li>{@code ci} —— CI 流水线对拍</li>
|
||||||
|
* <li>{@code parity}—— 对拍框架免令牌重放</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>{@link SecurityConfig} 的 {@code @Profile} 表达式与本类的名单<strong>必须一致</strong>,
|
||||||
|
* 故 {@link #BYPASS} / {@link #STRICT} 提供编译期常量给注解复用,
|
||||||
|
* {@link #isBypassActive(Environment)} 给运行期判定(JWT 弱密钥守卫等)复用。
|
||||||
|
*/
|
||||||
|
public final class SecurityProfiles {
|
||||||
|
|
||||||
|
private SecurityProfiles() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 开启免令牌旁路的 profile 名(运行期判定用)。 */
|
||||||
|
static final Set<String> BYPASS_NAMES = Set.of("dev", "local", "ci", "parity");
|
||||||
|
|
||||||
|
/** 宽松(旁路)链的 {@code @Profile} 表达式——与 {@link #BYPASS_NAMES} 一致。 */
|
||||||
|
public static final String BYPASS = "dev | local | ci | parity";
|
||||||
|
|
||||||
|
/** 严格链的 {@code @Profile} 表达式:旁路 profile 全不激活(含 prod 与未设 profile)。 */
|
||||||
|
public static final String STRICT = "!dev & !local & !ci & !parity";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前是否激活了任一旁路 profile。
|
||||||
|
* 用于运行期 fail-safe 判定(如 prod/严格场景下拒绝弱 JWT 密钥)。
|
||||||
|
*/
|
||||||
|
public static boolean isBypassActive(Environment env) {
|
||||||
|
if (env == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String p : env.getActiveProfiles()) {
|
||||||
|
if (BYPASS_NAMES.contains(p)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.eip4.platform.auth.security;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT 弱密钥启动守卫断言:严格 profile 下用开发默认密钥必须拒启动;旁路 profile 或强密钥放行。
|
||||||
|
*/
|
||||||
|
class JwtSecretSafetyCheckTest {
|
||||||
|
|
||||||
|
private JwtProperties props(String secret) {
|
||||||
|
JwtProperties p = new JwtProperties();
|
||||||
|
p.setSecret(secret);
|
||||||
|
p.setIssuer("eip4-platform");
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 严格profile下开发默认密钥_拒启动() {
|
||||||
|
MockEnvironment env = new MockEnvironment().withProperty("x", "y"); // 无旁路 profile = 严格
|
||||||
|
assertThatThrownBy(() -> new JwtSecretSafetyCheck(
|
||||||
|
props(JwtSecretSafetyCheck.DEV_DEFAULT_SECRET), env))
|
||||||
|
.isInstanceOf(IllegalStateException.class)
|
||||||
|
.hasMessageContaining("EIP4_AUTH_JWT_SECRET");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void prod_profile下开发默认密钥_拒启动() {
|
||||||
|
MockEnvironment env = new MockEnvironment();
|
||||||
|
env.setActiveProfiles("prod");
|
||||||
|
assertThatThrownBy(() -> new JwtSecretSafetyCheck(
|
||||||
|
props(JwtSecretSafetyCheck.DEV_DEFAULT_SECRET), env))
|
||||||
|
.isInstanceOf(IllegalStateException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 旁路profile下开发默认密钥_放行() {
|
||||||
|
for (String profile : new String[] {"dev", "local", "ci", "parity"}) {
|
||||||
|
MockEnvironment env = new MockEnvironment();
|
||||||
|
env.setActiveProfiles(profile);
|
||||||
|
assertThatCode(() -> new JwtSecretSafetyCheck(
|
||||||
|
props(JwtSecretSafetyCheck.DEV_DEFAULT_SECRET), env))
|
||||||
|
.as("旁路 profile " + profile + " 下默认密钥应放行")
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 严格profile下强密钥_放行() {
|
||||||
|
MockEnvironment env = new MockEnvironment();
|
||||||
|
env.setActiveProfiles("prod");
|
||||||
|
assertThatCode(() -> new JwtSecretSafetyCheck(
|
||||||
|
props("a-strong-random-production-secret-key-0123456789-abcdef"), env))
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,11 +18,13 @@ import org.springframework.security.web.FilterChainProxy;
|
|||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 安全链按 profile 装配的断言:
|
* 安全链按 profile 装配的断言(P0 fail-safe 修复后):
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><b>生产链(prod)</b>:测试旁路<strong>关闭</strong>——无令牌访问 /api/entity/** 被拒(401/403,非 2xx);
|
* <li><b>严格链(prod)</b>:测试旁路<strong>关闭</strong>——无令牌访问 /api/entity/** 被拒(401);
|
||||||
* 放行清单(/api/auth/**、/v3/api-docs/**)仍开。</li>
|
* 放行清单(/api/auth/**、/v3/api-docs/**)仍开。</li>
|
||||||
* <li><b>非生产链(无 profile)</b>:permitAll 测试通道开启——无令牌访问 /api/entity/** 放行(对拍 CI 依赖)。</li>
|
* <li><b>严格链(未设任何 profile)</b>:fail-safe 默认——无令牌访问受保护路径同样被拒(401)。
|
||||||
|
* 这是修复点:旧实现未设 profile 即 permitAll 全放行(fail-open),现默认强制认证。</li>
|
||||||
|
* <li><b>宽松链(显式 dev/local/ci/parity)</b>:permitAll 测试通道开启——无令牌访问放行(对拍 CI 依赖)。</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* 用 WebApplicationContextRunner 直接装载 SecurityConfig + 真实 Security 自动配置,
|
* 用 WebApplicationContextRunner 直接装载 SecurityConfig + 真实 Security 自动配置,
|
||||||
* 取出 springSecurityFilterChain 跑 Mock 请求观察拦截结果,不需起整 Boot 应用。
|
* 取出 springSecurityFilterChain 跑 Mock 请求观察拦截结果,不需起整 Boot 应用。
|
||||||
@@ -83,13 +85,29 @@ class SecurityProfileTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void 非生产链开启测试旁路_无令牌放行() {
|
void 未设profile_fail_safe默认严格_受保护路径无令牌被拒() {
|
||||||
// 无 active profile → devFilterChain(@Profile("!prod"))
|
// P0 修复:无 active profile → 严格链(@Profile(SecurityProfiles.STRICT)),不再 fail-open 全放行
|
||||||
runner().run(ctx -> {
|
runner().run(ctx -> {
|
||||||
assertThat(ctx).hasNotFailed();
|
assertThat(ctx).hasNotFailed();
|
||||||
assertThat(statusThroughChain(ctx, "GET", "/api/entity/EIP4PT_TASK/list"))
|
assertThat(statusThroughChain(ctx, "GET", "/api/entity/EIP4PT_TASK/list"))
|
||||||
.as("非生产 profile:permitAll 测试通道开启,无令牌应放行(对拍 CI 依赖)")
|
.as("未设 profile:fail-safe 默认必须强制认证,无令牌被拒为 401(旧实现此处 fail-open 放行)")
|
||||||
.isEqualTo(200);
|
.isEqualTo(401);
|
||||||
|
// 放行清单在严格默认链下仍开
|
||||||
|
assertThat(statusThroughChain(ctx, "POST", "/api/auth/login"))
|
||||||
|
.as("登录端点须放行").isEqualTo(200);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 显式旁路profile开启测试通道_无令牌放行() {
|
||||||
|
// dev/local/ci/parity 任一激活 → devFilterChain(@Profile(SecurityProfiles.BYPASS))
|
||||||
|
for (String profile : new String[] {"dev", "local", "ci", "parity"}) {
|
||||||
|
runner().withPropertyValues("spring.profiles.active=" + profile).run(ctx -> {
|
||||||
|
assertThat(ctx).hasNotFailed();
|
||||||
|
assertThat(statusThroughChain(ctx, "GET", "/api/entity/EIP4PT_TASK/list"))
|
||||||
|
.as("旁路 profile(" + profile + "):permitAll 测试通道开启,无令牌应放行(对拍 CI 依赖)")
|
||||||
|
.isEqualTo(200);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user