Class TokenServiceImpl
- All Implemented Interfaces:
TokenService
This service is responsible for generating access and refresh tokens, validating existing tokens, and refreshing tokens when necessary. It uses JWT (JSON Web Token) to create signed tokens for user authentication and authorization. The service uses user details from the database to generate tokens.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerateToken
(org.springframework.security.core.Authentication authentication) Generates access and refresh tokens for the authenticated user.Extracts the user ID from the token.org.springframework.security.authentication.UsernamePasswordAuthenticationToken
parseAccessToken
(String header) Parses the access token from the authorization header and retrieves the associated authentication.refreshToken
(RefreshTokenDto refreshTokenDto) Refreshes the access token using the provided refresh token.void
-
Constructor Details
-
TokenServiceImpl
public TokenServiceImpl()
-
-
Method Details
-
generateToken
Generates access and refresh tokens for the authenticated user.This method creates an access token and a refresh token for the user based on the provided authentication object. It uses the user's ID, the current time, and expiration times for the tokens to generate JWT tokens.
- Specified by:
generateToken
in interfaceTokenService
- Parameters:
authentication
- the authentication object containing the user's credentials- Returns:
- the generated
TokensDto
containing the access and refresh tokens
-
parseAccessToken
public org.springframework.security.authentication.UsernamePasswordAuthenticationToken parseAccessToken(String header) Parses the access token from the authorization header and retrieves the associated authentication.This method extracts the token from the authorization header, verifies its validity, and retrieves the user details associated with the token. It then creates and returns a
UsernamePasswordAuthenticationToken
.- Specified by:
parseAccessToken
in interfaceTokenService
- Parameters:
header
- the authorization header containing the access token- Returns:
- the
UsernamePasswordAuthenticationToken
representing the authenticated user - Throws:
IllegalArgumentException
- if the authorization header is incorrect or the token is invalid
-
refreshToken
Refreshes the access token using the provided refresh token.This method validates the refresh token, checks if the associated access token is still valid, and generates a new set of tokens if possible.
- Specified by:
refreshToken
in interfaceTokenService
- Parameters:
refreshTokenDto
- the DTO containing the refresh token- Returns:
- the new
TokensDto
containing the new access and refresh tokens - Throws:
IllegalArgumentException
- if the refresh token is invalid or nullIllegalStateException
- if the old access token has expired
-
id
Extracts the user ID from the token.- Specified by:
id
in interfaceTokenService
- Parameters:
token
- the JWT token- Returns:
- the user ID contained in the token
-
setCookie
- Specified by:
setCookie
in interfaceTokenService
-