How hieu-pt/spring-boot-spring-security-jwt-authentication works
Project type and purpose
• This is a backend-only Spring Boot REST API implementing JWT-based authentication and authorization with Spring Security and Spring Data JPA.
• The README describes user registration, login, role-based authorization flows, and refresh token support.
• There is no frontend in this repo; clients (web/mobile) consume the exposed REST endpoints.
File structure
• Top-level:
• pom.xml, Maven wrapper files, README.md
• src/main/java/com/bezkoder/springjwt – application code
• src/main/resources/application.properties – datasource and JWT config
• src/test/java/com/bezkoder/springjwt – basic smoke tests
• Under src/main/java/com/bezkoder/springjwt:
• controllers – REST controllers (AuthController, TestController)
• models – JPA entities (User, Role, ERole enum)
• payload – DTOs for requests (LoginRequest, SignupRequest) and responses (JwtResponse, MessageResponse)
• repository – Spring Data JPA repositories (UserRepository, RoleRepository)
• security –
– WebSecurityConfig: HTTP security rules
– jwt sub‐package: JwtUtils, AuthTokenFilter, AuthEntryPointJwt
– services: UserDetailsServiceImpl, UserDetailsImpl
• No external config besides application.properties; no microservices or frontend modules.
README for additional insights
• Authentication flow (login/signup → JWT generation → protected endpoints)
• Overall server architecture (security layer wrapping controllers, services, repository, database)
a. Main components
• Client(s) (Web/Mobile)
• Spring Boot Application
• Controller Layer (AuthController, TestController)
• Security Layer
· WebSecurityConfig
· AuthTokenFilter
· AuthEntryPointJwt
· JwtUtils
• Service Layer (UserDetailsServiceImpl / UserDetailsImpl)
• Repository Layer (UserRepository, RoleRepository)
• Domain Models (User, Role)
• Relational Database (MySQL/PostgreSQL)
b. Relationships and interactions
1. Client → HTTP POST /api/auth/signup or /login → AuthController
2. AuthController uses UserRepository/RoleRepository to persist or fetch users
3. On login, AuthController calls AuthenticationManager (configured in WebSecurityConfig), which delegates to UserDetailsServiceImpl → loads user via UserRepository
4. If credentials valid, JwtUtils generates token → returned in JwtResponse
5. For protected endpoints (/api/test/**), every request passes through AuthTokenFilter (registered in WebSecurityConfig)
– AuthTokenFilter uses JwtUtils to validate token, set SecurityContext
6. Controller methods annotated with @PreAuthorize or standard role checks
7. Data persisted/retrieved via Spring Data JPA to the Database
c. Architectural patterns and design principles
• Layered architecture (Controller → Service → Repository)
• Spring Security filter chain for cross-cutting concerns (authentication, exception handling)
• Stateless authentication via JWT
• Dependency inversion: controllers depend on interfaces (UserDetailsService), Spring injects implementations
d. Key technologies
• Spring Boot
• Spring Security
• JSON Web Token (JWT)
• Spring Data JPA (Hibernate)
• PostgreSQL or MySQL (configurable)
• Maven build system
Essential elements to capture
• Authentication flow for login and token refresh
• Security filter chain before hitting business logic
• Role-based access decision after token validation
• Persistence layer interactions
• Configuration entry point (WebSecurityConfig) that ties filters into the HTTP request pipeline