This is the Authorization Server for the KitchenSink application. It provides authentication and token generation using Spring Authorization Server with OAuth2 and PKCE.
- OAuth2 Authorization Code Flow with PKCE
- JWT-based authentication
- User authentication with MongoDB
- Role-based access control (Admin, Client)
- CORS support for frontend integration
- Docker support
- Java 21
- Spring Boot 3.x
- Spring Security OAuth2 Authorization Server
- MongoDB
- Docker
/auth-server
│── src/main/java/com/mongo/auth_server
│ ├── config/ # Security & Authorization Configuration
│ ├── controller/ # User API Controllers
│ ├── model/ # User Entity
│ ├── repository/ # MongoDB Repositories
│ ├── service/ # Authentication Services
│── src/main/resources
│ ├── application.properties # Application configuration
│── Dockerfile # Docker setup
│── pom.xml # Maven dependencies
- Java 21
- Maven 3+
- MongoDB running on Docker
- Docker (if running in containers)
Ensure MongoDB is running and update application.properties with the correct database URL:
spring.data.mongodb.uri=mongodb://admin:adminpassword@localhost:27017/auth-db?authSource=admin# Clone the repository
git clone <repo-url>
cd auth-server
# Build the project
mvn clean install
# Run the server
mvn spring-boot:run# Build Docker Image
docker build -t auth-server .
# Run Container
docker run -p 9000:9000 --name auth-server auth-serverIf using docker-compose.yml, start all services together:
docker-compose up -dRun the AuthServerApplication.java file which will start the project on port 9000
| Method | Endpoint | Description |
|---|---|---|
GET |
/login |
Login Page |
POST |
/oauth2/token |
Generate Access Token |
GET |
/oauth2/authorize |
Authorization Endpoint |
GET |
/api/user/role |
Fetch User Role |
GET |
/logout |
Logout & Redirect |
Visit the following URL in your browser:
http://localhost:9000/oauth2/authorize?response_type=code&client_id=public-client-react-app&redirect_uri=http://localhost:3000/login/oauth2/code/public-client-react-app&scope=openid
After logging in, copy the code from the redirect URL.
Run the following curl command:
curl --location 'http://localhost:9000/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id=public-client-react-app' \
--data-urlencode 'redirect_uri=http://localhost:3000/login/oauth2/code/public-client-react-app' \
--data-urlencode 'code=<YOUR_AUTHORIZATION_CODE>'This will return an access token that can be used for authentication.
Press CTRL + C
docker stop auth-server
docker rm auth-serverdocker-compose downThis project is licensed under the Apache License 2.0.