Validate production JWT secret and isolate development fallback

This commit is contained in:
28Hus committed 2026-10-18 19:32:54 +08:00
1 parent 1939677e48
commit 053827412e
4 files changed
+38 -4

No files matched your search

+5 -2
View File
@@ -60,12 +60,15 @@ novel-plus -- 父工程
## 前台 JWT 签名密钥
启动 `novel-front` 前,通过 `JWT_SECRET` 提供 Base64 编码的随机密钥(至少 64 字节),不要将密钥提交到仓库:
生产安装包的启动脚本会启用 `prod` 环境。解压 `novel-front.zip` 后,在启动前配置独立的 JWT 签名密钥:
```bash
export JWT_SECRET="$(openssl rand -base64 64)"
export JWT_SECRET="$(openssl rand -base64 64 | tr -d '\n')"
bin/novel-front.sh start
```
建议生成至少 64 字节的随机密钥,只生成一次并安全保存,在重启和同一部署的多个实例间复用。`prod` 环境未设置或仅设置空白 `JWT_SECRET` 时,会记录明确错误并终止应用启动。默认 `dev` 环境保留原有开发配置,无需额外设置;不要将开发环境用于生产。更换密钥会使原 token 失效。
## 项目截图
### 绿色主题模版
@@ -0,0 +1,27 @@
package com.java2nb.novel.core.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
/**
* Requires an explicitly configured JWT signing secret in production.
*/
@Component
@Profile("prod")
@Slf4j
public class JwtSecretValidator implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
String jwtSecret = System.getenv("JWT_SECRET");
if (jwtSecret == null || jwtSecret.isBlank()) {
log.error("JWT_SECRET environment variable is not configured. "
+ "Please configure it before starting the application. "
+ "Linux/macOS: export JWT_SECRET=\"$(openssl rand -base64 64 | tr -d '\\n')\"");
throw new IllegalStateException("Missing required environment variable: JWT_SECRET");
}
}
}
@@ -2,6 +2,10 @@ spring:
config:
import: classpath:application-common-dev.yml
jwt:
# Development only; production requires an explicit JWT_SECRET.
secret: ${JWT_SECRET:novel!#20191230}
pic:
save:
@@ -21,4 +25,4 @@ thread:
#线程超时时间(秒)
keep-alive-time: 10
#队列大小
queue-size: 100
queue-size: 100
@@ -7,7 +7,7 @@ spring:
include: website,alipay,oss
jwt:
secret: ${JWT_SECRET}
secret: ${JWT_SECRET:}
expiration: 604800
#缓存管理密码