修复回归安全问题并补充验证
This commit is contained in:
+18
-4
@@ -4,17 +4,31 @@ import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Gateway JWT 校验配置。
|
||||
*/
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "yuminail.security.jwt")
|
||||
public class JwtProperties {
|
||||
|
||||
private String secret = "yuminail-secret-key-for-jwt-token-generation-2026-must-be-at-least-512-bits-long-for-hs512-algorithm-security-requirements";
|
||||
public static final String DEVELOPMENT_PLACEHOLDER_SECRET = "__CONFIGURE_YUMINAIL_JWT_SECRET_OUTSIDE_REPOSITORY__";
|
||||
private static final int HS512_MIN_SECRET_BYTES = 64;
|
||||
|
||||
private String secret = DEVELOPMENT_PLACEHOLDER_SECRET;
|
||||
|
||||
private String issuer = "yuminail";
|
||||
|
||||
private long clockSkewSeconds = 30;
|
||||
|
||||
public void requireValidSecret() {
|
||||
if (secret == null || secret.isBlank()) {
|
||||
throw new IllegalStateException("JWT 密钥不能为空,请通过外部配置提供 yuminail.security.jwt.secret");
|
||||
}
|
||||
if (DEVELOPMENT_PLACEHOLDER_SECRET.equals(secret)) {
|
||||
throw new IllegalStateException("JWT 密钥仍为仓库占位值,请通过外部配置覆盖后再启动服务");
|
||||
}
|
||||
if (secret.getBytes(StandardCharsets.UTF_8).length < HS512_MIN_SECRET_BYTES) {
|
||||
throw new IllegalStateException("JWT 密钥长度不足,HS512 至少需要 64 字节");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -7,6 +7,7 @@ import io.jsonwebtoken.security.Keys;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -19,6 +20,11 @@ public class GatewayJwtValidator {
|
||||
|
||||
private final JwtProperties jwtProperties;
|
||||
|
||||
@PostConstruct
|
||||
void validateJwtSecret() {
|
||||
jwtProperties.requireValidSecret();
|
||||
}
|
||||
|
||||
public boolean validateAccessToken(String token) {
|
||||
try {
|
||||
Claims claims = Jwts.parserBuilder()
|
||||
@@ -35,6 +41,7 @@ public class GatewayJwtValidator {
|
||||
}
|
||||
|
||||
private SecretKey getKey() {
|
||||
jwtProperties.requireValidSecret();
|
||||
return Keys.hmacShaKeyFor(jwtProperties.getSecret().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@ spring:
|
||||
namespace: YumiNail-Dev
|
||||
group: DEFAULT_GROUP
|
||||
service: ${spring.application.name}
|
||||
username: xiaowang
|
||||
password: 122503wcy....
|
||||
username: ${NACOS_USERNAME:xiaowang}
|
||||
password: ${NACOS_PASSWORD:}
|
||||
config:
|
||||
server-addr: home-nacos01:8848,home-nacos02:8848,home-nacos03:8848
|
||||
namespace: YumiNail-Dev
|
||||
group: DEFAULT_GROUP
|
||||
name: yuminail-gateway
|
||||
file-extension: yaml
|
||||
username: xiaowang
|
||||
password: 122503wcy....
|
||||
username: ${NACOS_USERNAME:xiaowang}
|
||||
password: ${NACOS_PASSWORD:}
|
||||
shared-configs:
|
||||
- data-id: logging.yaml
|
||||
group: DEFAULT_GROUP
|
||||
|
||||
Reference in New Issue
Block a user