# prajapatiiranjeet/security_setup_with-jwt architecture

> Architecture diagram of the GitHub repository prajapatiiranjeet/security_setup_with-jwt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prajapatiiranjeet/security_setup_with-jwt
- Repository: https://github.com/prajapatiiranjeet/security_setup_with-jwt

## Overview

#### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_http["HTTP API"]
  node_auth_controller["Auth endpoints<br/>REST controller"]
  node_student_controller["Student endpoints<br/>REST controller"]
  node_faculty_controller["Faculty endpoints<br/>REST controller"]
  node_admin_controller["Admin endpoints<br/>REST controller"]
  node_api_dtos["Auth API DTOs<br/>API contracts"]
end

subgraph group_security["Security boundary"]
  node_security_config["Security filter chain<br/>Spring Security config"]
  node_jwt_filter["JWT filter<br/>security filter<br/>[JwtFilter.java]"]
  node_jwt_service["JWT service<br/>token service<br/>[JwtService.java]"]
  node_auth_service["Authentication service<br/>application service<br/>[AuthService.java]"]
  node_user_details_service["Principal mapper<br/>UserDetails service"]
end

subgraph group_identity["Identity persistence"]
  node_user_repo["User repository<br/>JPA repository<br/>[UserRepo.java]"]
  node_user["User identity<br/>JPA entity<br/>[User.java]"]
  node_role["Role enum<br/>authorization role<br/>[Role.java]"]
  node_postgres[("PostgreSQL<br/>database")]
end

node_app{{"Spring Boot application<br/>bootstrap"}}
node_runtime_config["Runtime configuration<br/>properties"]
node_maven["Maven build<br/>build manifest<br/>[pom.xml]"]

node_maven -->|"builds"| node_app
node_app -->|"boots"| node_security_config
node_runtime_config -.->|"configures"| node_security_config
node_runtime_config -.->|"JWT secret"| node_jwt_service
node_runtime_config -.->|"datasource"| node_postgres
node_security_config -->|"installs"| node_jwt_filter
node_jwt_filter -->|"reloads principal"| node_user_details_service
node_jwt_filter -->|"validates token"| node_jwt_service
node_security_config -->|"permits /auth/**"| node_auth_controller
node_security_config -->|"ADMIN or STUDENT"| node_student_controller
node_security_config -->|"ADMIN or FACULTY"| node_faculty_controller
node_auth_controller -->|"uses"| node_api_dtos
node_auth_controller -->|"registers and logs in"| node_auth_service
node_auth_service -->|"uses encoder and auth manager"| node_security_config
node_auth_service -->|"issues JWT"| node_jwt_service
node_auth_service -->|"persists user"| node_user_repo
node_user_details_service -->|"finds by email"| node_user_repo
node_user_repo -->|"maps"| node_user
node_user -->|"has role"| node_role
node_user_repo -->|"JPA/Hibernate"| node_postgres

click node_app "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/MycodeApplication.java"
click node_auth_controller "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/AuthController.java"
click node_student_controller "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/StudentController.java"
click node_faculty_controller "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/FacultyController.java"
click node_admin_controller "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/AdminController.java"
click node_security_config "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/websecurityconfig.java"
click node_jwt_filter "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/JwtFilter.java"
click node_jwt_service "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/JwtService.java"
click node_auth_service "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Service/AuthService.java"
click node_user_details_service "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Service/CustomUserDetailsService.java"
click node_user_repo "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Repo/UserRepo.java"
click node_user "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Entity/User.java"
click node_role "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Entity/Role.java"
click node_runtime_config "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/resources/application.properties"
click node_maven "https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/pom.xml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_auth_controller,node_student_controller,node_faculty_controller,node_admin_controller,node_api_dtos toneBlue
class node_security_config,node_jwt_filter,node_jwt_service,node_auth_service,node_user_details_service toneAmber
class node_user_repo,node_user,node_role,node_postgres toneMint
class node_app,node_runtime_config,node_maven toneNeutral
```

## Components

### HTTP API

- [Auth endpoints](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/AuthController.java): REST controller · Public registration and login routes under /auth/**. · `src/main/java/com/chaiorcode/mycode/Controller/AuthController.java`
- [Student endpoints](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/StudentController.java): REST controller · Protected /students/** routes. · `src/main/java/com/chaiorcode/mycode/Controller/StudentController.java`
- [Faculty endpoints](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/FacultyController.java): REST controller · Protected /faculty/** routes. · `src/main/java/com/chaiorcode/mycode/Controller/FacultyController.java`
- [Admin endpoints](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Controller/AdminController.java): REST controller · Admin support is incomplete. · `src/main/java/com/chaiorcode/mycode/Controller/AdminController.java`
- **Auth API DTOs**: API contracts · Registration, login, and login-response payloads.

### Security boundary

- [Security filter chain](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/websecurityconfig.java): Spring Security config · Permits /auth/** and enforces role policies for protected namespaces. · `src/main/java/com/chaiorcode/mycode/security/websecurityconfig.java`
- [JWT filter](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/JwtFilter.java): security filter · Extracts Bearer tokens and establishes the SecurityContext. · `src/main/java/com/chaiorcode/mycode/security/JwtFilter.java`
- [JWT service](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/security/JwtService.java): token service · Signs and validates 15-minute JWTs. · `src/main/java/com/chaiorcode/mycode/security/JwtService.java`
- [Authentication service](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Service/AuthService.java): application service · Assigns registration roles, hashes passwords, and orchestrates login. · `src/main/java/com/chaiorcode/mycode/Service/AuthService.java`
- [Principal mapper](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Service/CustomUserDetailsService.java): UserDetails service · Maps persisted users and roles to Spring Security authorities. · `src/main/java/com/chaiorcode/mycode/Service/CustomUserDetailsService.java`

### Identity persistence

- [User repository](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Repo/UserRepo.java): JPA repository · Provides email-based identity lookup. · `src/main/java/com/chaiorcode/mycode/Repo/UserRepo.java`
- [User identity](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Entity/User.java): JPA entity · Primary persisted identity model. · `src/main/java/com/chaiorcode/mycode/Entity/User.java`
- [Role enum](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/Entity/Role.java): authorization role · Uses STUDENTS while policy checks STUDENT, risking denied student access. · `src/main/java/com/chaiorcode/mycode/Entity/Role.java`
- **PostgreSQL**: database · Runtime identity persistence.

### Other components

- [Spring Boot application](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/java/com/chaiorcode/mycode/MycodeApplication.java): bootstrap · `src/main/java/com/chaiorcode/mycode/MycodeApplication.java`
- [Runtime configuration](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/src/main/resources/application.properties): properties · Externalizes datasource, JWT secret, and Hibernate schema auto-update. · `src/main/resources/application.properties`
- [Maven build](https://github.com/prajapatiiranjeet/security_setup_with-jwt/blob/main/pom.xml): build manifest · Defines Java 21 and application dependencies. · `pom.xml`

## Connections

- 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
