This project demonstrates the implementation of OAuth 2.0 authentication and authorization using Spring Security and Spring Boot. It includes three modules:
- Authorization Server (
localhost:9000
) – Handles OAuth 2.0 authorization and token generation. - Resource Server (localhost
:8090
) – Secures APIs and exposes user/product endpoints. - Spring Security Client (
127.0.0.1:8080
) – Uses WebClient to securely access the Resource Server using OAuth2 tokens.
- Handles OAuth 2 login and authorization code flow.
- Issues access and refresh tokens.
- Configured clients:
user-service
user-service-authorization-code
- Secured using tokens.
- Exposes REST endpoints:
GET /api/users
GET /api/products
GET /api/product/{id}
- Includes:
Product
model- Repository layer using Spring Data JPA
- Service and Controller
- Uses
OAuth2AuthorizedClientManager
and WebClient for secure calls. - Endpoints:
GET /users
→ fetches/api/users
from resource serverGET /products
→ fetches/api/products
GET /product/{id}
→ fetches/api/product/{id}
- User opens
127.0.0.1:8080
and initiates login. - Redirects to
localhost:9000/oauth2/authorize
. - After successful login, user is redirected back with an authorization code.
- Client exchanges it for an access token.
- Client uses the token to call resource server endpoints.
- OAuth2 Authorization Code Flow
- Access & Refresh Token Support
- WebClient Integration with token propagation
- Role-based and scope-based access
- Custom user domain model and repository integration
- Java JDK 21
- Spring Boot
- Spring Security OAuth2
- Maven
- WebClient (OAuth2 support)
Two clients are configured:
user-service
→ foropenid
scope (used for login)user-service-authorization-code
→ formessage.read
scope (used to fetch data from resource server and within client )
/users
→ GET → calls/api/users
/products
→ GET → calls/api/products
/product/{id}
→ GET → calls/api/product/{id}
/api/hello
→ returns a message within the client app/api/employee
→ another internal secured endpoint
/oauth2/authorize
/oauth2/token
/api/users
→ secured/api/products
→ secured/api/product/{id}
→ secured
Here are a few resources I used to study OAuth2 with Spring Boot:
images/
folder:
git clone https://github.com/Thekishor/spring-security-oauth.git cd spring-security-oauth