Interface TokenService
- All Known Implementing Classes:
TokenServiceImpl
This interface defines the necessary methods for working with JSON Web Tokens (JWT) in an authentication system. Implementations of this interface are responsible for generating access and refresh tokens, parsing the access token from an HTTP request, and refreshing the tokens based on the refresh token.
-
Method Summary
Modifier and TypeMethodDescriptiongenerateToken
(org.springframework.security.core.Authentication authentication) Generates a new access and refresh token for the given authenticated user.Extracts the user ID from a JWT token.org.springframework.security.authentication.UsernamePasswordAuthenticationToken
parseAccessToken
(String token) Parses the access token from a provided token string and retrieves the associated authentication.refreshToken
(RefreshTokenDto refreshTokenDto) Refreshes the access token using the provided refresh token.void
-
Method Details
-
generateToken
Generates a new access and refresh token for the given authenticated user.This method creates an access token and a refresh token for a user based on the provided authentication object. The generated tokens are signed with a secret key and contain expiration times.
- Parameters:
authentication
- the authentication object containing the authenticated user's details- Returns:
- a
TokensDto
object containing the generated access and refresh tokens
-
parseAccessToken
org.springframework.security.authentication.UsernamePasswordAuthenticationToken parseAccessToken(String token) Parses the access token from a provided token string and retrieves the associated authentication.This method is responsible for extracting the JWT from the authorization header (or other source), verifying its validity, and using it to create a
UsernamePasswordAuthenticationToken
that represents the authenticated user.- Parameters:
token
- the JWT token in the authorization header or request- Returns:
- a
UsernamePasswordAuthenticationToken
containing the authenticated user's details - Throws:
IllegalArgumentException
- if the token is invalid or cannot be parsed
-
refreshToken
Refreshes the access token using the provided refresh token.This method validates the provided refresh token and uses it to generate a new set of tokens, including a fresh access token and a new refresh token. The method ensures that the refresh token is valid and has not expired before issuing the new tokens.
- Parameters:
refreshTokenDto
- aRefreshTokenDto
containing the refresh token to be used for refreshing the access token- Returns:
- a
TokensDto
containing the newly generated access and refresh tokens - Throws:
IllegalArgumentException
- if the refresh token is invalid or nullIllegalStateException
- if the refresh token is expired or otherwise cannot be used to generate a new access token
-
id
-
setCookie
-