|
|
@@ -25,7 +25,22 @@ public class JwtConfig {
|
|
|
private Long expiration;
|
|
|
|
|
|
private SecretKey getSigningKey() {
|
|
|
- return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
|
|
+ if (keyBytes.length < 32) {
|
|
|
+ // 密钥太短(如 dev 用的短密钥),用 SHA-256 扩展
|
|
|
+ java.security.MessageDigest md;
|
|
|
+ try {
|
|
|
+ md = java.security.MessageDigest.getInstance("SHA-256");
|
|
|
+ byte[] expanded = md.digest(secret.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return Keys.hmacShaKeyFor(expanded);
|
|
|
+ } catch (java.security.NoSuchAlgorithmException e) {
|
|
|
+ // 回退:重复填充到32字节
|
|
|
+ byte[] padded = new byte[32];
|
|
|
+ System.arraycopy(keyBytes, 0, padded, 0, keyBytes.length);
|
|
|
+ return Keys.hmacShaKeyFor(padded);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Keys.hmacShaKeyFor(keyBytes);
|
|
|
}
|
|
|
|
|
|
public String generateToken(Long userId, String role) {
|