How sanjay-namdeo/spring-security-with-jwt works
Project Type and Purpose
• This is a backend, monolithic Spring Boot application focusing on authentication and authorization via JWT.
• Its primary features (from README) are:
– Auto-creating a MySQL “user” table and pre-loading one user on startup
– Encrypting passwords with BCrypt
– Exposing a /login endpoint that returns a JWT
– Protecting a /hello endpoint via a Bearer token in the Authorization header
File Structure
• Top-level build and config:
– pom.xml, mvnw, .mvn/wrapper → Maven build and wrapper
– application-*.properties → profiles for MySQL, Oracle, and default
– keystore.jks → holds JWT signing key
• Code organized under src/main/java/com/learn/security/springsecuritywithjwt:
– SpringSecurityWithJwtApplication (main)
– config/SecurityConfig.java (defines SecurityFilterChain)
– filter/JwtFilter.java (intercepts inbound requests to validate JWT)
– service/JwtUtilService.java, UserDetailServiceImpl.java (token utilities, user lookup)
– repository/UserRepository.java (JPA interface to the user table)
– entity/UserEntity.java (JPA entity)
– controller/MainController.java (handles /login, /hello)
– advice/MainExceptionHandler.java (global error handling)
– exception and pojo packages (model error payloads, request/response DTOs)
README for Extra Insights
• Confirms Spring Boot 2.7.2, SecurityFilterChain, JWT flow
• No frontend or external microservices—purely a RESTful authentication service
Main Components
a. Client (Postman, browser, any HTTP client)
b. Spring Boot Application
– HTTP Entry Point / DispatcherServlet
– SecurityFilterChain
• JwtFilter
– Controllers (MainController)
– Services
• JwtUtilService
• UserDetailServiceImpl
– Repository
• UserRepository
– Database (MySQL)
– Keystore (for JWT signing)
c. Error-handling advice (MainExceptionHandler)
Relationships and Data Flow
– /login:
1. Client → HTTP request to /login
2. SecurityFilterChain (no token required for login) → MainController.login()
3. MainController → UserDetailServiceImpl (load user)
4. Authenticate password via BCryptPasswordEncoder
5. JwtUtilService generates JWT (uses keystore.jks)
6. Response → Client with token
– /hello (protected):
1. Client → HTTP GET /hello with Authorization: Bearer <token>
2. SecurityFilterChain → JwtFilter intercepts
3. JwtFilter uses JwtUtilService to validate and extract claims
4. SecurityContext populated → MainController.hello()
5. Response → Client
Architectural Patterns
• Layered architecture (Controller → Service → Repository → Database)
• Use of Spring Security’s SecurityFilterChain for filter-based auth
• JWT stateless authentication
• Exception-handling via @ControllerAdvice
Technologies & Frameworks
• Spring Boot 2.7.2, Spring Web, Spring Security, Spring Data JPA
• MySQL (with optional Oracle profile)
• JWT (sign/verify with keystore.jks)
• Maven for build
• BCryptPasswordEncoder for hashing
Diagramming Conventions
– Use rectangles for components (web server, filter chain, controllers, services, repository).
• Green for successful flows
• Red for error/exception paths
• Blue for data stores or external systems
Essential Detail Checklist
– Clearly show the SecurityFilterChain and the position of JwtFilter.
– Include error-handler component for exception responses.
– Annotate the keystore usage in token signing/verification.
– Distinguish between authenticated vs unauthenticated endpoints (/login vs /hello).