generated from Java-2025Fall/final-vibevault-template
更新 src/main/java/com/vibevault/security/JwtService.java
This commit is contained in:
parent
768876a4ce
commit
87546e6f19
@ -1,21 +1,25 @@
|
|||||||
package com.vibevault.security;
|
package com.vibevault.security;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.MalformedJwtException;
|
||||||
import io.jsonwebtoken.security.Keys;
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import io.jsonwebtoken.security.SecurityException;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.vibevault.model.User;
|
||||||
|
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JWT 服务
|
* JWT 服务
|
||||||
*
|
*
|
||||||
* 需要实现:
|
* 提供 JWT token 的生成、验证和解析功能
|
||||||
* - 生成 JWT token(包含用户名)
|
|
||||||
* - 从 token 中提取用户名
|
|
||||||
* - 验证 token 是否有效(未过期、签名正确)
|
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class JwtService {
|
public class JwtService {
|
||||||
@ -24,32 +28,80 @@ public class JwtService {
|
|||||||
private String secret;
|
private String secret;
|
||||||
|
|
||||||
@Value("${jwt.expiration:86400000}")
|
@Value("${jwt.expiration:86400000}")
|
||||||
private long expiration;
|
private long expiration; // 默认 24 小时
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为用户生成 JWT token
|
* 为用户生成 JWT token
|
||||||
*/
|
*/
|
||||||
public String generateToken(String username) {
|
public String generateToken(String username) {
|
||||||
// TODO: 实现 token 生成
|
Date now = new Date();
|
||||||
// 提示:使用 Jwts.builder()
|
Date expiryDate = new Date(now.getTime() + expiration);
|
||||||
throw new UnsupportedOperationException("待实现");
|
|
||||||
|
return Jwts.builder()
|
||||||
|
.setSubject(username)
|
||||||
|
.setIssuedAt(now)
|
||||||
|
.setExpiration(expiryDate)
|
||||||
|
.signWith(getSigningKey())
|
||||||
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从 token 中提取用户名
|
* 从 token 中提取用户名
|
||||||
*/
|
*/
|
||||||
public String extractUsername(String token) {
|
public String extractUsername(String token) {
|
||||||
// TODO: 实现用户名提取
|
Claims claims = extractAllClaims(token);
|
||||||
// 提示:使用 Jwts.parser()
|
return claims.getSubject();
|
||||||
throw new UnsupportedOperationException("待实现");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证 token 是否有效
|
* 验证 token 是否有效
|
||||||
*/
|
*/
|
||||||
public boolean isTokenValid(String token, String username) {
|
public boolean isTokenValid(String token, User user) {
|
||||||
// TODO: 实现 token 验证
|
try {
|
||||||
throw new UnsupportedOperationException("待实现");
|
String username = extractUsername(token);
|
||||||
|
return username.equals(user.getUsername()) && !isTokenExpired(token);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证 token 是否有效
|
||||||
|
*/
|
||||||
|
public boolean isTokenValid(String token) {
|
||||||
|
try {
|
||||||
|
extractAllClaims(token);
|
||||||
|
return !isTokenExpired(token);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 token 是否过期
|
||||||
|
*/
|
||||||
|
private boolean isTokenExpired(String token) {
|
||||||
|
Date expirationDate = extractExpiration(token);
|
||||||
|
return expirationDate.before(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提取 token 的过期时间
|
||||||
|
*/
|
||||||
|
private Date extractExpiration(String token) {
|
||||||
|
Claims claims = extractAllClaims(token);
|
||||||
|
return claims.getExpiration();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 token 中提取所有声明
|
||||||
|
*/
|
||||||
|
private Claims extractAllClaims(String token) {
|
||||||
|
return Jwts.parserBuilder()
|
||||||
|
.setSigningKey(getSigningKey())
|
||||||
|
.build()
|
||||||
|
.parseClaimsJws(token)
|
||||||
|
.getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,4 +110,26 @@ public class JwtService {
|
|||||||
private SecretKey getSigningKey() {
|
private SecretKey getSigningKey() {
|
||||||
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 验证 token 签名和格式
|
||||||
|
*/
|
||||||
|
public boolean validateToken(String token) {
|
||||||
|
try {
|
||||||
|
Jwts.parserBuilder()
|
||||||
|
.setSigningKey(getSigningKey())
|
||||||
|
.build()
|
||||||
|
.parseClaimsJws(token);
|
||||||
|
return true;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
System.out.println("无效的 JWT 签名");
|
||||||
|
} catch (MalformedJwtException e) {
|
||||||
|
System.out.println("无效的 JWT token");
|
||||||
|
} catch (ExpiredJwtException e) {
|
||||||
|
System.out.println("JWT token 已过期");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
System.out.println("JWT token 为空或格式错误");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user