Class JWTService
java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.security.JWTService
Serviced responsible for business logic related to JWT tokens
- Since:
- 17.4.24
- Version:
- 1.0
- Author:
- Harry L.X and Lars M.L.N
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance of the class that creates the key for signing tokens -
Method Summary
Modifier and TypeMethodDescription<T> T
extractClaim
(String token, Function<io.jsonwebtoken.Claims, T> claimsTFunction) Applies input function on the claims.extractUsername
(String token) Extracts the subject (username) from JWT tokengenerateToken
(Map<String, Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes) Generates JWT tokengenerateToken
(org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes) Wrapper method for generating JWT tokenboolean
isTokenValid
(String token, org.springframework.security.core.userdetails.UserDetails userDetails) Checks if username in token checks out with the current user and that the token is not expired
-
Constructor Details
-
Method Details
-
generateToken
public String generateToken(org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes) Wrapper method for generating JWT token- Parameters:
userDetails
- User informationexpirationTimeMinutes
- Expiration time of token- Returns:
- Generated token
-
generateToken
public String generateToken(Map<String, Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes) Generates JWT token- Parameters:
extraClaims
- Map of extra claimsuserDetails
- User infoexpirationTimeMinutes
- Expiration time for token- Returns:
- JWT token
-
extractUsername
Extracts the subject (username) from JWT token- Parameters:
token
- JWT token- Returns:
- The username
-
extractClaim
Applies input function on the claims. The returned type depends on the type T of the input function- Type Parameters:
T
- Type of input function- Parameters:
token
- TokenclaimsTFunction
- The function applied to claims- Returns:
- Type of input function
-
isTokenValid
public boolean isTokenValid(String token, org.springframework.security.core.userdetails.UserDetails userDetails) Checks if username in token checks out with the current user and that the token is not expired- Parameters:
token
- TokenuserDetails
- Details of the user trying to retrieve session- Returns:
- True, if username matches and token has not expired
-