How yogendra-27/spring-security-jwt works
This repository is a Spring Boot backend centered on JWT-based authentication and authorization.
High-level architecture
The app is organized into a few clear layers:
1. Bootstrap layer
- SecurityappApplication is the Spring Boot entry point.
2. HTTP/API layer
- Controllers expose the REST endpoints:
- HelloController
- StudentController
- UserController
3. Security layer
- SecurityConfig defines the Spring Security setup.
- JwtFilter intercepts requests to extract and validate JWTs before the request reaches controllers.
- JWTService is the token utility/service responsible for generating and validating JWTs.
4. User/authentication layer
- MyUserDeatilsService implements Spring Security’s UserDetailsService contract.
- UserPrincipal adapts the application’s user model into Spring Security’s UserDetails.
- UserService appears to contain user-related business logic, likely including registration and login support.
5. Persistence layer
- UserRepository provides database access for user records.
- Users is the persistent domain/entity model backing authentication data.
6. Configuration and runtime
- application.yml holds runtime configuration, likely including server, security, and datasource/JWT settings.
Main request flow
1. Application startup
Spring Boot starts from SecurityappApplication, loads the security configuration, and wires the controller, service, and repository beans.
2. Authentication flow
A client sends credentials to an authentication-related endpoint, most likely in UserController.
• UserController delegates to UserService.
• UserService likely validates the credentials using Spring Security components.
• MyUserDeatilsService loads the user from UserRepository.
• The returned Users record is wrapped by UserPrincipal so Spring Security can work with it.
• JWTService creates a JWT after successful authentication.
• The token is returned to the client.
3. Authorized request flow
For later requests, the client includes the JWT in the request header.
• JwtFilter runs before controller execution.
• It extracts the token, validates it via JWTService, and establishes the authenticated security context.
• If the token is valid, the request continues to the controller.
• If invalid or missing on a protected route, Spring Security denies access.
Important boundaries
Public vs protected API boundary
This project is built around the distinction between:
• endpoints that are open, likely for login or registration
• endpoints that require a valid JWT
SecurityConfig is the boundary that decides which routes are public and which require authentication.
Application user model vs Spring Security model
There is a clear adapter boundary between:
• Users as the application/database representation
• UserPrincipal as the Spring Security representation
That separation is important for diagramming because it shows that the database entity is not used directly by the security framework.
Token logic vs request filtering
JWT responsibilities are split:
• JWTService handles token creation and validation logic
• JwtFilter handles request-time extraction and authentication setup
That separation is central to the architecture.
Role of the controllers
• UserController is the security-related entry point and likely owns auth flows such as signup/login.
• HelloController is probably a basic test or public/protected sample endpoint.
• StudentController suggests an additional domain endpoint used to demonstrate authorization behavior, likely with restricted access.
Data model and persistence
The only explicit persistent domain model in the tree is Users, accessed through UserRepository. That means user authentication state is backed by a datastore through Spring Data repository access.
Student appears under the controller package rather than a model package, so it is likely a simple request/response DTO or sample payload rather than a persisted entity.
Infrastructure and tooling
• The project uses Maven (pom.xml, mvnw, .mvn/).
• It is a Spring Boot application with Spring Security and JWT.
• The test tree contains only a basic application test, so the repo appears to be a small example or starter implementation rather than a large multi-module system.
What an architecture diagram should show
A good diagram for this repo should include:
• Client → REST controllers
• Security filter chain in front of all requests
• JwtFilter calling JWTService
• UserController and UserService handling auth actions
• MyUserDeatilsService loading users from UserRepository
• UserPrincipal bridging Users into Spring Security
• SecurityConfig controlling which endpoints are open or protected
• application.yml as the configuration source
• SecurityappApplication as the bootstrap entry point
In short, this is a layered Spring Security JWT application where authentication is user-store-backed, JWTs are the stateless session mechanism, and the security filter chain mediates access to the REST controllers.
Main entry points: SecurityConfig.java Security config · SecurityappApplication.java Security app · JwtFilter.java JWT filter · UserService.java User service
Components 14
Bootstrap
Application entry point and startup wiring
- Security app · Application entry point
src/main/java/com/security/securityapp/SecurityappApplication.java
HTTP API
REST controllers exposed to clients
- Hello · Simple endpoint controller
src/main/java/com/security/securityapp/controller/HelloController.java - Student · Sample or protected endpoint controller
src/main/java/com/security/securityapp/controller/StudentController.java - User · Auth-related API entry point
src/main/java/com/security/securityapp/controller/UserController.java - Student · Controller-side student payload or DTO
src/main/java/com/security/securityapp/controller/Student.java
Security
Spring Security configuration and JWT request handling
- Security config · Defines protected and public routes
src/main/java/com/security/securityapp/controller/config/SecurityConfig.java - JWT filter · Extracts and validates bearer tokens
src/main/java/com/security/securityapp/controller/config/JwtFilter.java - JWT service · Creates and validates JWTs
src/main/java/com/security/securityapp/service/JWTService.java
Auth domain
User details, principals, and auth business logic
- User service · Coordinates auth and user logic
src/main/java/com/security/securityapp/service/UserService.java - User details · Loads users for Spring Security
src/main/java/com/security/securityapp/service/MyUserDeatilsService.java - User principal · Adapts Users to Spring Security principal
src/main/java/com/security/securityapp/model/UserPrincipal.java
Persistence
User entity and repository access
Runtime config
Application runtime settings
- App config · Runtime configuration source
src/main/resources/application.yml
Connections 21
- Security app → Security config: loads · Bootstraps the security setup
- Security app → Hello: wires · Registers REST controllers
- Security app → Student: wires · Registers REST controllers
- Security app → User: wires · Registers REST controllers
- Security config → JWT filter: adds · Places JWT filter in the security chain
- Security config → User: opens · Configures auth-related routes
- Security config → Hello: guards · Applies access rules to endpoints
- Security config → Student: guards · Applies access rules to endpoints
- JWT filter → JWT service: validates · Delegates token parsing and verification
- JWT filter → Security config: participates · Runs as part of the configured filter chain
- User → User service: delegates · Hands off login or registration logic
- User service → User details: loads · Looks up users through security-aware lookup
- User service → JWT service: issues token · Creates JWT after successful authentication
- User details → User repo: queries · Loads user records from persistence
- User details → User principal: wraps · Converts entity into security principal
- User principal → Users: adapts · Represents the application user as Spring Security user
- User repo → Users: stores · Persists user account records
- Security config → App config: reads · Uses runtime properties and secrets
- JWT service → App config: reads · Uses JWT-related configuration
- User repo → App config: uses · Relies on datasource/runtime settings
- Student → Student: uses · Consumes or returns student payloads