This Spring Boot application demonstrates JSON Web Token (JWT) authentication. It provides an API endpoint for authentication using JWT.
The HomeController
class is a Spring @RestController
that handles incoming requests related to user authentication using JWT. It contains two endpoints:
-
GET
/
: Returns a welcome message indicating the start of the JWT Tutorial. -
POST
/authenticate
: Accepts aJwtRequest
containing a username and password. It authenticates the user's credentials using Spring Security'sAuthenticationManager
. If the credentials are valid, it generates a JWT token using theJwtUtility
and returns it as aJwtResponse
.
- Access the root endpoint (
/
) to receive a welcome message indicating the start of the JWT tutorial.
- Use a POST request to
/authenticate
with a JSON body containingusername
andpassword
fields in aJwtRequest
. - If the credentials are valid, the endpoint generates a JWT token using the
JwtUtility
and returns it wrapped in aJwtResponse
.
- MyUserDetailsService: A service responsible for fetching user details from a data source.
- JwtUtility: A utility class to handle JWT token generation and parsing.
- AuthenticationManager: Provided by Spring Security for user authentication.
- Integration: Integrate this service into your application to enable JWT-based authentication.
- Authentication: Send a POST request to
/authenticate
with valid user credentials in theJwtRequest
body. - Token Handling: Upon successful authentication, retrieve the JWT token from the
JwtResponse
for subsequent authorized requests.
Contributions to enhance security, add more endpoints, or improve functionality are welcome. Feel free to fork this repository, make changes, and create a pull request.
This project is licensed under the MIT License.
making a request to genrate a token and using the token to access a protected route