Initial commit: mail-server with WebFlux backend and Angular v22 Synthwave frontend
Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.mailserver.util;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
|
||||
public class JwtUtils {
|
||||
|
||||
private static final long EXPIRATION_MS = 1000L * 60 * 60 * 24; // 24 часа
|
||||
|
||||
private final SecretKey signingKey;
|
||||
|
||||
public JwtUtils(String secret) {
|
||||
this.signingKey = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public String generateToken(String email) {
|
||||
Date now = new Date();
|
||||
Date expiry = new Date(now.getTime() + EXPIRATION_MS);
|
||||
|
||||
return Jwts.builder()
|
||||
.subject(email)
|
||||
.issuedAt(now)
|
||||
.expiration(expiry)
|
||||
.signWith(signingKey)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public String getEmailFromToken(String token) {
|
||||
Claims claims = Jwts.parser()
|
||||
.verifyWith(signingKey)
|
||||
.build()
|
||||
.parseSignedClaims(token)
|
||||
.getPayload();
|
||||
return claims.getSubject();
|
||||
}
|
||||
|
||||
public boolean isValidToken(String token) {
|
||||
try {
|
||||
Jwts.parser()
|
||||
.verifyWith(signingKey)
|
||||
.build()
|
||||
.parseSignedClaims(token);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user