How narayangeed/spring-boot-jwt-demo works
Main Components
• Client/Application: This represents a client (for example, a browser or REST client) that makes requests to the API endpoints.
• REST Endpoints:
• /authenticate: A POST endpoint handled by the JwtAuthenticationController to collect the username and password. On successful authentication (via the Spring Authentication Manager and JwtUserDetailsService), a JWT token is generated using JwtTokenUtil.
• /hello: A GET endpoint served by HelloWorldController. Access to this endpoint is secured—only allowing requests with a valid JWT.
• Security Components:
• WebSecurityConfig: Configures Spring Security. It sets up the Authentication Manager, specifies which endpoints are secured, and wires in the JWT filter and entry point.
• JwtRequestFilter: A Spring Web Filter (extending OncePerRequestFilter) that intercepts incoming requests to check for a valid JWT token. If the token is valid, it sets the appropriate authentication in the security context.
• JwtAuthenticationEntryPoint: Handles unauthorized requests by returning a 401 error.
• JWT Utility:
• JwtTokenUtil: Responsible for the creation and validation of JWT tokens. It works with the io.jsonwebtoken.Jwts library.
• User Details Service:
• JwtUserDetailsService: Implements Spring Security’s UserDetailsService. It provides user details (in this demo hardcoded) required for authentication.
• Data Models:
• JwtRequest: Represents the username and password received in the authentication request.
• JwtResponse: Wraps the JWT token that is returned as a response.
• Build & Configuration:
• Maven configuration files (pom.xml and the Maven wrapper) indicate this is a Maven project with a typical Java structure.
• Application properties file includes configurations like jwt.secret.
Relationships and Interactions
• The client initiates a POST request to /authenticate with login credentials.
• The JwtAuthenticationController delegates authentication to Spring’s Authentication Manager, which invokes the JwtUserDetailsService.
• On success, JwtTokenUtil is called to generate a JWT token, and this token is returned by the JwtResponse.
• For subsequent requests (to /hello), the JwtRequestFilter intercepts the request, validates the token using JwtTokenUtil, and permits access if the token is valid.
• If a token is missing or invalid, JwtAuthenticationEntryPoint handles the error by rejecting the request (HTTP 401).
• The WebSecurityConfig class integrates all security components and configures which endpoints are open vs. secured.
Architectural Patterns & Design Principles
• Separation of Concerns: Clear modularization where controllers handle incoming HTTP requests, security is managed separately using filters, and token logic is encapsulated in a utility class.
• Stateless Authentication: JWT based authentication avoids server-side session maintenance.
• Use of Dependency Injection: Spring Boot features and Maven for managing dependencies and configuration.
• RESTful API Design: Simple and clear API endpoints for core functionalities.