Package api.mpba.rastvdmy.service.impl
Class JwtServiceImpl
java.lang.Object
api.mpba.rastvdmy.service.impl.JwtServiceImpl
- All Implemented Interfaces:
JwtService
Implementation of the JwtService interface for managing JSON Web Tokens (JWT).
This service provides methods for token generation, extraction, validation,
and claim handling using the specified secret key and expiration time.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionextractSubject
(String token) Extracts the subject from the given JWT token.extractToken
(jakarta.servlet.http.HttpServletRequest request) Extracts the JWT token from the HTTP request's Authorization header.extractUsername
(String token) Extracts the username from the given JWT token.generateToken
(org.springframework.security.core.userdetails.UserDetails userDetails) Generates a JWT token for the given user details.long
Retrieves the expiration time for the JWT token.boolean
isTokenValid
(String token, org.springframework.security.core.userdetails.UserDetails userDetails) Validates the given JWT token against the provided user details.
-
Constructor Details
-
JwtServiceImpl
public JwtServiceImpl()
-
-
Method Details
-
extractUsername
Extracts the username from the given JWT token.- Specified by:
extractUsername
in interfaceJwtService
- Parameters:
token
- the JWT token- Returns:
- the username extracted from the token
-
extractToken
Extracts the JWT token from the HTTP request's Authorization header.- Specified by:
extractToken
in interfaceJwtService
- Parameters:
request
- the HTTP request containing the Authorization header- Returns:
- the extracted token, or null if no valid token is found
-
generateToken
Generates a JWT token for the given user details.- Specified by:
generateToken
in interfaceJwtService
- Parameters:
userDetails
- the user details for which the token is to be generated- Returns:
- the generated JWT token
-
getExpirationTime
public long getExpirationTime()Retrieves the expiration time for the JWT token.- Specified by:
getExpirationTime
in interfaceJwtService
- Returns:
- the expiration time in milliseconds
-
isTokenValid
public boolean isTokenValid(String token, org.springframework.security.core.userdetails.UserDetails userDetails) Validates the given JWT token against the provided user details.- Specified by:
isTokenValid
in interfaceJwtService
- Parameters:
token
- the JWT token to validateuserDetails
- the user details to compare against- Returns:
- true if the token is valid, false otherwise
-
extractSubject
Extracts the subject from the given JWT token.- Specified by:
extractSubject
in interfaceJwtService
- Parameters:
token
- the JWT token- Returns:
- the subject extracted from the token
-