96 lines
2.5 KiB
Java
96 lines
2.5 KiB
Java
package com.denniseckerskorn.security;
|
|
|
|
import io.jsonwebtoken.*;
|
|
import io.jsonwebtoken.security.Keys;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.security.Key;
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* Utility class for generating and validating JWT tokens.
|
|
*/
|
|
@Component
|
|
public class JwtUtil {
|
|
|
|
@Value("${jwt.secret}")
|
|
private String secretKey;
|
|
|
|
@Value("${jwt.expiration}")
|
|
private long expirationTime;
|
|
|
|
/**
|
|
* Generates a signing key using the secret key.
|
|
*
|
|
* @return the signing key
|
|
*/
|
|
private Key getSigningKey() {
|
|
return Keys.hmacShaKeyFor(secretKey.getBytes());
|
|
}
|
|
|
|
/**
|
|
* Generates a JWT token for the given username.
|
|
*
|
|
* @param username the username to include in the token
|
|
* @return the generated JWT token
|
|
*/
|
|
public String generateToken(String username, String authority) {
|
|
Date now = new Date();
|
|
Date expiryDate = new Date(now.getTime() + expirationTime);
|
|
|
|
return Jwts.builder()
|
|
.setSubject(username)
|
|
.claim("role", authority)
|
|
.setIssuedAt(now)
|
|
.setExpiration(expiryDate)
|
|
.signWith(getSigningKey(), SignatureAlgorithm.HS256)
|
|
.compact();
|
|
}
|
|
|
|
/**
|
|
* Extracts the username from the given JWT token.
|
|
*
|
|
* @param token the JWT token
|
|
* @return the username extracted from the token
|
|
*/
|
|
public String extractUsername(String token) {
|
|
return Jwts.parserBuilder()
|
|
.setSigningKey(getSigningKey())
|
|
.build()
|
|
.parseClaimsJws(token)
|
|
.getBody()
|
|
.getSubject();
|
|
}
|
|
|
|
/**
|
|
* Validates the given JWT token.
|
|
*
|
|
* @param token the JWT token to validate
|
|
* @return true if the token is valid, false otherwise
|
|
*/
|
|
public boolean validateToken(String token) {
|
|
try {
|
|
extractUsername(token);
|
|
return true;
|
|
} catch (JwtException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the given JWT token is expired.
|
|
*
|
|
* @param token the JWT token to check
|
|
* @return true if the token is expired, false otherwise
|
|
*/
|
|
public Claims getAllClaimsFromToken(String token) throws ExpiredJwtException {
|
|
return Jwts.parserBuilder()
|
|
.setSigningKey(getSigningKey())
|
|
.build()
|
|
.parseClaimsJws(token)
|
|
.getBody();
|
|
}
|
|
|
|
}
|