How tamani-coding/fullstack-oauth2-angular-spring-boot-keycloak works
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.