Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions base/src/main/java/com/tinyengine/it/login/utils/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.Keys;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Jwt util
Expand All @@ -39,22 +40,38 @@
@Slf4j
public class JwtUtil {

@Autowired
private TokenBlacklistService tokenBlacklistService;

private static final long EXPIRATION_TIME = 21600000L; // 6小时 = 6 * 60 * 60 * 1000 = 21600000 毫秒
private static final String DEFAULT_SECRET = "tiny-engine-backend-secret-key-at-jwt-login";

// 避免启动时环境变量未加载的问题
private static String getSecretString() {
return Optional.ofNullable(System.getenv("SECRET_STRING"))
.orElse(DEFAULT_SECRET);
}

public static SecretKey getSecretKey() {

return Keys.hmacShaKeyFor(getSecretString().getBytes());
}
@Autowired
private TokenBlacklistService tokenBlacklistService;

private static final long EXPIRATION_TIME = 21600000L; // 6小时 = 6 * 60 * 60 * 1000 = 21600000 毫秒
private static final String SECRET_ENV_NAME = "SECRET_STRING";

@PostConstruct
public void validateSecretConfiguration() {
try {
getSecretKey();
} catch (Exception e) {
throw new IllegalStateException(
"JWT secret is not configured correctly. Set environment variable "
+ SECRET_ENV_NAME + " to a strong value before starting the service.",
e
);
}
}
Comment thread
hexqi marked this conversation as resolved.

private static String getSecretString() {
String secret = System.getenv(SECRET_ENV_NAME);
if (secret == null || secret.isBlank()) {
throw new IllegalStateException(
"Missing required environment variable " + SECRET_ENV_NAME + " for JWT signing."
);
}
return secret;
}

public static SecretKey getSecretKey() {
return Keys.hmacShaKeyFor(getSecretString().getBytes(StandardCharsets.UTF_8));
}
Comment thread
hexqi marked this conversation as resolved.

/**
* 生成包含完整用户信息的 JWT Token(支持 Tenant 对象和 Map 两种格式)
Expand Down
Loading