How danaza1125/baseabstractcrud works
Purpose and Runtime
A Java 21 / Spring Boot 3.4 microservice template for building tenant-aware REST services. It combines generic JPA CRUD scaffolding with stateless Spring Security, JWT authentication, and OAuth2 social login. Maven build/dependency definition is in pom.xml; runtime settings, H2 database, JWT, OAuth2, tenancy, and OpenAPI configuration are in src/main/resources/application.yml.
Application Entry and HTTP Boundary
src/main/java/com/example/microservice/BaseMicroserviceApplication.java is the Spring Boot entry point. REST requests enter feature controllers, currently src/main/java/com/example/microservice/auth/controller/AuthController.java, and are expected to be extended through package-by-feature modules. Shared controller behavior returns standard response envelopes via src/main/java/com/example/microservice/common/response/ApiResponse.java.
Security Pipeline
src/main/java/com/example/microservice/auth/security/SecurityConfig.java defines the stateless Security 6 filter chain, route authorization rules, CORS integration, JWT processing, and OAuth2 login integration. Protected requests flow through src/main/java/com/example/microservice/auth/security/jwt/JwtAuthenticationFilter.java, which validates Bearer tokens and establishes Spring Security authentication. Authentication failures are rendered as JSON by src/main/java/com/example/microservice/auth/security/jwt/JwtAuthenticationEntryPoint.java.
Authentication and Identity
src/main/java/com/example/microservice/auth/service/AuthService.java implements registration, credential authentication, password hashing, token issuance, and refresh flows exposed by src/main/java/com/example/microservice/auth/controller/AuthController.java. src/main/java/com/example/microservice/auth/security/jwt/JwtTokenProvider.java signs, validates, and parses tokens carrying username, tenant ID, and roles. src/main/java/com/example/microservice/auth/security/user/CustomUserDetailsService.java and src/main/java/com/example/microservice/auth/security/user/UserPrincipal.java provide tenant-aware application identity to Spring Security.
OAuth2 External Boundary
OAuth2 client login delegates provider-user mapping to src/main/java/com/example/microservice/auth/security/oauth2/CustomOAuth2UserService.java. On success, src/main/java/com/example/microservice/auth/security/oauth2/OAuth2AuthenticationSuccessHandler.java converts the external identity into application JWT access/refresh tokens and redirects to the configured frontend callback. Google and GitHub are configured external identity providers; failure handling is isolated in src/main/java/com/example/microservice/auth/security/oauth2/OAuth2AuthenticationFailureHandler.java.
Tenancy and Persistence
Tenant identity enters through the X-Tenant-ID request header in src/main/java/com/example/microservice/common/tenant/TenantFilter.java and is held per request thread by src/main/java/com/example/microservice/common/tenant/TenantContext.java. JWT authentication also carries tenant context. Hibernate resolves the active tenant through src/main/java/com/example/microservice/config/TenantIdentifierResolver.java. JPA auditing is wired by src/main/java/com/example/microservice/config/JpaAuditConfig.java; domain entities should inherit audit/version fields from src/main/java/com/example/microservice/common/abstracts/BaseEntity.java.
Domain Extension and Error Contracts
New domains extend the generic CRUD layers: src/main/java/com/example/microservice/common/abstracts/AbstractController.java exposes conventional REST operations, while src/main/java/com/example/microservice/common/abstracts/AbstractService.java centralizes persistence operations, DTO mapping hooks, and lifecycle extension points. Exceptions cross the HTTP boundary through src/main/java/com/example/microservice/common/exception/GlobalExceptionHandler.java, producing uniform error payloads defined in src/main/java/com/example/microservice/common/exception/ErrorResponse.java.
Main entry points: JwtAuthenticationFilter.java JWT authentication filter · SecurityConfig.java Security filter chain · AuthService.java Auth service · AbstractService.java Generic CRUD service
Components 22
HTTP API
- Auth controller
src/main/java/com/example/microservice/auth/controller/AuthController.java - API response envelope
src/main/java/com/example/microservice/common/response/ApiResponse.java - Global exception handler
src/main/java/com/example/microservice/common/exception/GlobalExceptionHandler.java - Error response
src/main/java/com/example/microservice/common/exception/ErrorResponse.java
Security & Identity
- Security filter chain · Defines stateless authorization, CORS, JWT, and OAuth2 login.
src/main/java/com/example/microservice/auth/security/SecurityConfig.java - JWT authentication filter
src/main/java/com/example/microservice/auth/security/jwt/JwtAuthenticationFilter.java - JWT token provider · Signs and parses username, tenant, and role claims.
src/main/java/com/example/microservice/auth/security/jwt/JwtTokenProvider.java - JWT failure entry point
src/main/java/com/example/microservice/auth/security/jwt/JwtAuthenticationEntryPoint.java - Auth service · Handles registration, credentials, password hashing, and refresh.
src/main/java/com/example/microservice/auth/service/AuthService.java - Tenant-aware user details
src/main/java/com/example/microservice/auth/security/user/CustomUserDetailsService.java - User principal
src/main/java/com/example/microservice/auth/security/user/UserPrincipal.java - OAuth2 user mapper
src/main/java/com/example/microservice/auth/security/oauth2/CustomOAuth2UserService.java - OAuth2 success handler · Issues application tokens and redirects to the frontend callback.
src/main/java/com/example/microservice/auth/security/oauth2/OAuth2AuthenticationSuccessHandler.java - Google & GitHub
Tenancy & Persistence
- Tenant header filter · Reads X-Tenant-ID for each request.
src/main/java/com/example/microservice/common/tenant/TenantFilter.java - Tenant context
src/main/java/com/example/microservice/common/tenant/TenantContext.java - Tenant identifier resolver
src/main/java/com/example/microservice/config/TenantIdentifierResolver.java - Audited base entity · Supplies audit and version fields.
src/main/java/com/example/microservice/common/abstracts/BaseEntity.java
Template Foundation
- Spring Boot application
src/main/java/com/example/microservice/BaseMicroserviceApplication.java - Runtime configuration · Configures H2, JWT, OAuth2, tenancy, and OpenAPI.
src/main/resources/application.yml - Generic CRUD controller · Extension point for new feature controllers.
src/main/java/com/example/microservice/common/abstracts/AbstractController.java - Generic CRUD service · Provides persistence, DTO mapping, and lifecycle hooks.
src/main/java/com/example/microservice/common/abstracts/AbstractService.java
Connections 23
- Spring Boot application → Security filter chain: boots
- Spring Boot application → Runtime configuration: loads
- Security filter chain → Tenant header filter: runs
- Security filter chain → JWT authentication filter: runs
- JWT authentication filter → JWT token provider: validates Bearer token
- JWT authentication filter → User principal: establishes authentication
- JWT authentication filter → JWT failure entry point: authentication failure
- Tenant header filter → Tenant context: sets
- JWT authentication filter → Tenant context: sets token tenant
- Tenant context → Tenant identifier resolver: resolves active tenant
- Auth controller → Auth service: registration and login
- Auth service → JWT token provider: issues and refreshes tokens
- Auth service → Tenant-aware user details: loads identity
- Tenant-aware user details → User principal: builds
- Security filter chain → OAuth2 user mapper: OAuth2 login
- Google & GitHub → OAuth2 user mapper: external profile
- OAuth2 user mapper → OAuth2 success handler: mapped identity
- OAuth2 success handler → JWT token provider: issues application tokens
- Auth controller → API response envelope: returns
- Global exception handler → Error response: renders
- Generic CRUD controller → Generic CRUD service: delegates CRUD
- Generic CRUD service → Audited base entity: persists domain entities
- Generic CRUD service → Tenant identifier resolver: uses tenant scope