2311061111/src/main/java/com/vibevault/security/JwtService.java

62 lines
1.6 KiB
Java
Raw Normal View History

2025-12-14 00:24:42 +08:00
package com.vibevault.security;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;
/**
* JWT 服务
*
* 需要实现
* - 生成 JWT token包含用户名
* - token 中提取用户名
* - 验证 token 是否有效未过期签名正确
*/
@Service
public class JwtService {
@Value("${jwt.secret:your-secret-key-here-should-be-at-least-256-bits-long-for-hs256}")
private String secret;
@Value("${jwt.expiration:86400000}")
private long expiration;
/**
* 为用户生成 JWT token
*/
public String generateToken(String username) {
// TODO: 实现 token 生成
// 提示:使用 Jwts.builder()
throw new UnsupportedOperationException("待实现");
}
/**
* token 中提取用户名
*/
public String extractUsername(String token) {
// TODO: 实现用户名提取
// 提示:使用 Jwts.parser()
throw new UnsupportedOperationException("待实现");
}
/**
* 验证 token 是否有效
*/
public boolean isTokenValid(String token, String username) {
// TODO: 实现 token 验证
throw new UnsupportedOperationException("待实现");
}
/**
* 获取签名密钥
*/
private SecretKey getSigningKey() {
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
}
}