init: rebuild myplayer-backend
This commit is contained in:
110
src/main/java/xin/merlin/myplayerbackend/utils/JwtUtil.java
Normal file
110
src/main/java/xin/merlin/myplayerbackend/utils/JwtUtil.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package xin.merlin.myplayerbackend.utils;
|
||||
|
||||
import io.jsonwebtoken.*;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xin.merlin.myplayerbackend.config.security.JwtProperties;
|
||||
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class JwtUtil {
|
||||
|
||||
private final JwtProperties jwtProperties;
|
||||
|
||||
private SecretKey key;
|
||||
private JwtParser jwtParser;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
// 从配置文件密钥生成 SecretKey
|
||||
this.key = Keys.hmacShaKeyFor(jwtProperties.getSecret().getBytes(StandardCharsets.UTF_8));
|
||||
this.jwtParser = Jwts.parser()
|
||||
.verifyWith(key)
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JWT 密钥初始化失败,请检查配置文件 jwt.secret", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 JWT Token
|
||||
*/
|
||||
public String generateToken(String uAccount, Integer uId) {
|
||||
Date now = new Date();
|
||||
Date expireDate = new Date(now.getTime() + jwtProperties.getExpire() * 1000L);
|
||||
|
||||
return Jwts.builder()
|
||||
.subject(uAccount)
|
||||
.claim("id", uId)
|
||||
.claim("account", uAccount)
|
||||
.id(UUID.randomUUID().toString())
|
||||
.issuedAt(now)
|
||||
.expiration(expireDate)
|
||||
.issuer(jwtProperties.getIssuer())
|
||||
.signWith(key, Jwts.SIG.HS256) // JJWT 0.12.6推荐的写法
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 Token 获取 Claims
|
||||
*/
|
||||
public Claims getClaims(String token) {
|
||||
try {
|
||||
Jws<Claims> jws = jwtParser.parseSignedClaims(token);
|
||||
System.out.println(jws.getPayload());
|
||||
return jws.getPayload();
|
||||
} catch (ExpiredJwtException e) {
|
||||
throw new TokenExpiredException("Token 已过期", e);
|
||||
} catch (JwtException e) {
|
||||
throw new InvalidTokenException("无效的 Token", e);
|
||||
} catch (Exception e) {
|
||||
throw new InvalidTokenException("解析 Token 失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 Token 是否过期
|
||||
*/
|
||||
public boolean isTokenExpired(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
return claims.getExpiration().before(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账号
|
||||
*/
|
||||
public String getUAccount(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
return claims.getSubject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户ID
|
||||
*/
|
||||
public String getUId(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
return claims.get("id", String.class);
|
||||
}
|
||||
|
||||
// 自定义异常类
|
||||
public static class TokenExpiredException extends RuntimeException {
|
||||
public TokenExpiredException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public static class InvalidTokenException extends RuntimeException {
|
||||
public InvalidTokenException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package xin.merlin.myplayerbackend.utils.result;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Response<T> {
|
||||
private int code;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
// 请求成功
|
||||
public static <T> Response<T> success(ResultCode code , T data){
|
||||
System.out.println( LocalDateTime.now()+" success : \n"+code);
|
||||
return new Response<T>(code.getCode(), code.getMessage(), data);
|
||||
}
|
||||
public static <T> Response<T> success(ResultCode code){
|
||||
System.out.println( LocalDateTime.now()+" success : \n"+code);
|
||||
return new Response<T>(code.getCode(), code.getMessage(),null);
|
||||
}
|
||||
|
||||
// 请求失败
|
||||
public static <T> Response<T> fail(ResultCode code){
|
||||
System.out.println( LocalDateTime.now()+" fail : \n"+code);
|
||||
return new Response<T>(code.getCode(),code.getMessage(), null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package xin.merlin.myplayerbackend.utils.result;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 通用状态码枚举类
|
||||
*/
|
||||
@Getter
|
||||
public enum ResultCode {
|
||||
|
||||
SUCCESS(200, "成功"),
|
||||
BAD_REQUEST(400, "请求参数错误"),
|
||||
UNAUTHORIZED(401, "未认证或登录已过期"),
|
||||
FORBIDDEN(403, "无权限访问"),
|
||||
NOT_FOUND(404, "资源不存在"),
|
||||
SERVER_ERROR(500, "服务器内部错误"),
|
||||
|
||||
// 自定义业务错误码
|
||||
USER_BANNED(1000,"用户被封禁"),
|
||||
USER_NOT_FOUND(1001, "用户不存在"),
|
||||
USER_EXIST(1003,"用户已存在"),
|
||||
USER_PASSWORD_ERROR(1004,"用户密码错误"),
|
||||
USER_VERIFICATION_ERROR(1005,"验证码不存在或错误"),
|
||||
USER_SEND_TOO_FAST(1006,"用户请求过快"),
|
||||
USER_SEND_TOO_OFTEN(1007,"请求次数过多,已被限制"),
|
||||
ORDER_NOT_FOUND(2000, "订单不存在");
|
||||
|
||||
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
ResultCode(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user