# tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak architecture

> Architecture diagram of the GitHub repository tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak
- Repository: https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak

## Overview

##### Project Type and Purpose
- This is a full-stack OAuth2 project integrating Keycloak, Angular, and Spring Boot.
- The Angular frontend uses the angular-oauth2-oidc library to handle OAuth2 flows.
- The Spring Boot backend is secured using JWT tokens and contains a custom JWT converter and security configuration.
- Keycloak is provisioned (via Docker) to manage realms, clients, users, and roles.

##### File Structure Analysis and Key Components
a. Backend:
- Located under the "backend" directory.
- Contains standard Maven project structure with source code under “src/main/java”.
- Key configuration files include:
- pom.xml (Maven configuration)
- application.properties (application settings including JWT issuer)
- Core classes:
- DemoApplication.java (Spring Boot entry point)
- SecurityConfig.java (configures OAuth2 resource server, CORS, and security filters)
- HelloController.java (a REST controller with a protected endpoint)
- CustomJwt and CustomJwtConverter (handle JWT extraction and mapping of authorities)
- Tests reside in the "src/test" directory.

b. Frontend (Angular Webapp):
- Located under the "webapp" directory.
- Contains Angular configuration files such as angular.json, package.json, and TypeScript configuration files.
- The main.ts file bootstraps the webapp, setting up the HTTP client and OAuth service, including silent token refresh and login initiation.
- AppComponent demonstrates basic functionality such as triggering logout and performing authenticated API calls.

c. Keycloak:
- Located under the "keycloak" folder.
- Contains Dockerfile and docker-compose.yml to run Keycloak and an associated PostgreSQL instance.
- Includes a realm export file (my-test-realm-realm.json) to preconfigure the Keycloak realm, clients, users, and roles needed for the OAuth2 setup.

##### Relationships and Interactions
- The Angular webapp interacts with both Keycloak (for obtaining access tokens, managing authentication flows, and performing silent token refresh) and the Spring Boot backend (for consuming protected APIs).
- The Spring Boot backend validates JWT tokens that are issued by the Keycloak server. It uses a custom JWT converter to extract roles (for example, `ROLE_fullstack-developer`) to handle authorization.
- Keycloak acts as the central identity provider (IdP) that manages realms, clients, and user authentication. It is deployed using Docker and exposes control (and discovery) endpoints for token validation.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each component has a clear responsibility – authentication (Keycloak), business logic (Spring Boot backend), and client-side UI/auth flows (Angular webapp).
- OAuth2 / JWT based security is implemented throughout the system.
- Containerization for Keycloak ensures consistency in authentication service deployment.

##### Technologies and Frameworks
- Angular 17 for frontend UI.
- Spring Boot (Java 21 with Maven) for backend APIs.
- Keycloak for OAuth2 and identity management.
- Additional libraries include angular-oauth2-oidc on the frontend and Spring Security on the backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend"
        direction TB
        A["Angular Webapp"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend"
        direction TB
        B["Spring Boot Backend"]:::backend
        B1["Maven Configuration (pom.xml)"]:::backendfiles
        B2["DemoApplication.java"]:::backendfiles
        B3["SecurityConfig.java"]:::backendfiles
        B4["HelloController.java"]:::backendfiles
        B5["CustomJwt.java"]:::backendfiles
        B6["CustomJwtConverter.java"]:::backendfiles
        B7["application.properties"]:::backendfiles

        B --> B1
        B --> B2
        B --> B3
        B --> B4
        B --> B5
        B --> B6
        B --> B7
    end

    %% Identity Provider Layer
    subgraph "Identity Provider"
        direction TB
        K["Keycloak Identity Server"]:::idp
        K1["Keycloak Docker Configuration"]:::idpfiles
        K2["Keycloak Realm Export"]:::idpfiles

        K --> K1
        K --> K2
    end

    %% Database Layer
    subgraph "Database"
        direction TB
        P["PostgreSQL"]:::database
    end

    %% Relationships
    A -->|"OAuth2_token_request"| K
    A -->|"API_call_with_token"| B
    B -->|"JWT_validation"| K
    K -->|"stores_realms_and_users"| P

    %% Click Events
    click A "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/tree/main/webapp/"
    click B "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/tree/main/backend/"
    click B1 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/pom.xml"
    click B2 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/java/com/example/demo/DemoApplication.java"
    click B3 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/java/com/example/demo/config/SecurityConfig.java"
    click B4 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/java/com/example/demo/controller/HelloController.java"
    click B5 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/java/com/example/demo/jwt/CustomJwt.java"
    click B6 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/java/com/example/demo/jwt/CustomJwtConverter.java"
    click B7 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/backend/src/main/resources/application.properties"
    click K "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/tree/main/keycloak/"
    click K1 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/tree/main/keycloak/Dockerfile"
    click K2 "https://github.com/tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak/blob/main/keycloak/my-test-realm-realm.json"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef backend fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef backendfiles fill:#D5F5E3,stroke:#28B463,stroke-width:1px;
    classDef idp fill:#F5CBA7,stroke:#BA4A00,stroke-width:2px;
    classDef idpfiles fill:#FAD7A0,stroke:#E67E22,stroke-width:1px;
    classDef database fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
```
