How prajapatiiranjeet/security_setup_with-jwt works
Purpose and Runtime
This is a Java 21 Spring Boot REST service demonstrating database-backed authentication and role-based authorization using stateless JWTs. It starts through src/main/java/com/chaiorcode/mycode/MycodeApplication.java; Maven coordinates dependencies/build via pom.xml. Runtime persistence is PostgreSQL, configured in src/main/resources/application.properties.
HTTP Entry Points
Authentication APIs are exposed by src/main/java/com/chaiorcode/mycode/Controller/AuthController.java: public /auth/** registration endpoints for admin, student, and faculty users, plus /auth/login. Protected route namespaces are represented by src/main/java/com/chaiorcode/mycode/Controller/StudentController.java and src/main/java/com/chaiorcode/mycode/Controller/FacultyController.java; admin support is incomplete in src/main/java/com/chaiorcode/mycode/Controller/AdminController.java.
Security Boundary and Authorization
src/main/java/com/chaiorcode/mycode/security/websecurityconfig.java defines the Spring Security filter chain, BCrypt password encoding, and authentication manager. It permits /auth/**, restricts /students/** to ADMIN/STUDENT, /faculty/** to ADMIN/FACULTY, and requires authentication elsewhere. This is the central HTTP trust boundary; controllers receive requests only after filter-chain enforcement.
A material inconsistency exists: src/main/java/com/chaiorcode/mycode/Entity/Role.java uses STUDENTS, while the security policy checks STUDENT, which can deny student access unless normalized.
Registration and Login Flow
src/main/java/com/chaiorcode/mycode/Service/AuthService.java owns registration and login orchestration. Registration accepts API DTOs, assigns role based on the selected registration endpoint rather than trusting the request role, hashes passwords through the configured encoder, and persists the user. Login delegates credential verification to Spring Security’s AuthenticationManager; on success it returns the user ID and signed JWT.
API contracts are separated from persistence in src/main/java/com/chaiorcode/mycode/DTO/CreateUserDto.java, src/main/java/com/chaiorcode/mycode/DTO/LoginDTO.java, and src/main/java/com/chaiorcode/mycode/DTO/LoginResponceDTO.java.
JWT Request Lifecycle
src/main/java/com/chaiorcode/mycode/security/JwtService.java generates JWTs with email as subject and validates signature/expiration using the configured secret; tokens expire after 15 minutes. src/main/java/com/chaiorcode/mycode/security/JwtFilter.java extracts Authorization: Bearer <token>, parses it, reloads the user and authorities, then populates Spring’s SecurityContext before dispatch continues. JWT validation is stateless, but each authenticated request still crosses the database-backed user lookup boundary.
Identity Persistence and Principal Mapping
src/main/java/com/chaiorcode/mycode/Entity/User.java is the primary persisted identity model, with roles represented by src/main/java/com/chaiorcode/mycode/Entity/Role.java. src/main/java/com/chaiorcode/mycode/Repo/UserRepo.java provides JPA access, notably lookup by email. src/main/java/com/chaiorcode/mycode/Service/CustomUserDetailsService.java converts the stored user and role into Spring Security UserDetails, bridging JPA identity data to authentication and authorization authorities.
src/main/java/com/chaiorcode/mycode/Entity/EntityStudent.java is presently outside the active request/service flow.
External Dependencies and Operational Boundaries
Spring WebMVC handles REST dispatch; Spring Security provides authentication, authorization, and context management; JPA/Hibernate maps identity data to PostgreSQL; JJWT signs/parses tokens. The datasource and JWT secret are externalized in src/main/resources/application.properties. Hibernate schema auto-update is configured, making schema management application-driven in the current local-oriented deployment model.
Components 17
HTTP API
- Auth endpoints · Public registration and login routes under /auth/**.
src/main/java/com/chaiorcode/mycode/Controller/AuthController.java - Student endpoints · Protected /students/** routes.
src/main/java/com/chaiorcode/mycode/Controller/StudentController.java - Faculty endpoints · Protected /faculty/** routes.
src/main/java/com/chaiorcode/mycode/Controller/FacultyController.java - Admin endpoints · Admin support is incomplete.
src/main/java/com/chaiorcode/mycode/Controller/AdminController.java - Auth API DTOs · Registration, login, and login-response payloads.
Security boundary
- Security filter chain · Permits /auth/** and enforces role policies for protected namespaces.
src/main/java/com/chaiorcode/mycode/security/websecurityconfig.java - JWT filter · Extracts Bearer tokens and establishes the SecurityContext.
src/main/java/com/chaiorcode/mycode/security/JwtFilter.java - JWT service · Signs and validates 15-minute JWTs.
src/main/java/com/chaiorcode/mycode/security/JwtService.java - Authentication service · Assigns registration roles, hashes passwords, and orchestrates login.
src/main/java/com/chaiorcode/mycode/Service/AuthService.java - Principal mapper · Maps persisted users and roles to Spring Security authorities.
src/main/java/com/chaiorcode/mycode/Service/CustomUserDetailsService.java
Identity persistence
- User repository · Provides email-based identity lookup.
src/main/java/com/chaiorcode/mycode/Repo/UserRepo.java - User identity · Primary persisted identity model.
src/main/java/com/chaiorcode/mycode/Entity/User.java - Role enum · Uses STUDENTS while policy checks STUDENT, risking denied student access.
src/main/java/com/chaiorcode/mycode/Entity/Role.java - PostgreSQL · Runtime identity persistence.
Other components
- Spring Boot application
src/main/java/com/chaiorcode/mycode/MycodeApplication.java - Runtime configuration · Externalizes datasource, JWT secret, and Hibernate schema auto-update.
src/main/resources/application.properties - Maven build · Defines Java 21 and application dependencies.
pom.xml
Connections 20
- Maven build → Spring Boot application: builds
- Spring Boot application → Security filter chain: boots
- Runtime configuration → Security filter chain: configures
- Runtime configuration → JWT service: JWT secret
- Runtime configuration → PostgreSQL: datasource
- Security filter chain → JWT filter: installs
- JWT filter → Principal mapper: reloads principal
- JWT filter → JWT service: validates token
- Security filter chain → Auth endpoints: permits /auth/**
- Security filter chain → Student endpoints: ADMIN or STUDENT
- Security filter chain → Faculty endpoints: ADMIN or FACULTY
- Auth endpoints → Auth API DTOs: uses
- Auth endpoints → Authentication service: registers and logs in
- Authentication service → Security filter chain: uses encoder and auth manager
- Authentication service → JWT service: issues JWT
- Authentication service → User repository: persists user
- Principal mapper → User repository: finds by email
- User repository → User identity: maps
- User identity → Role enum: has role
- User repository → PostgreSQL: JPA/Hibernate