java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.security.JWTService

@Service public class JWTService extends Object
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 Link icon

    Constructors
    Constructor
    Description
    Constructs an instance of the class that creates the key for signing tokens
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    <T> T
    extractClaim(String token, Function<io.jsonwebtoken.Claims,T> claimsTFunction)
    Applies input function on the claims.
    Extracts the subject (username) from JWT token
    generateToken(Map<String,Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes)
    Generates JWT token
    generateToken(org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes)
    Wrapper method for generating JWT token
    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

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details Link icon

    • JWTService Link icon

      public JWTService()
      Constructs an instance of the class that creates the key for signing tokens
  • Method Details Link icon

    • generateToken Link icon

      public String generateToken(org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes)
      Wrapper method for generating JWT token
      Parameters:
      userDetails - User information
      expirationTimeMinutes - Expiration time of token
      Returns:
      Generated token
    • generateToken Link icon

      public String generateToken(Map<String,Object> extraClaims, org.springframework.security.core.userdetails.UserDetails userDetails, long expirationTimeMinutes)
      Generates JWT token
      Parameters:
      extraClaims - Map of extra claims
      userDetails - User info
      expirationTimeMinutes - Expiration time for token
      Returns:
      JWT token
    • extractUsername Link icon

      public String extractUsername(String token)
      Extracts the subject (username) from JWT token
      Parameters:
      token - JWT token
      Returns:
      The username
    • extractClaim Link icon

      public <T> T extractClaim(String token, Function<io.jsonwebtoken.Claims,T> claimsTFunction)
      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 - Token
      claimsTFunction - The function applied to claims
      Returns:
      Type of input function
    • isTokenValid Link icon

      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 - Token
      userDetails - Details of the user trying to retrieve session
      Returns:
      True, if username matches and token has not expired