A complete Spring Boot microservice demonstrating Keycloak integration for authentication and authorization with role-based access control, user management, and password reset functionality.
- π Authentication & Authorization: Role-based access control with Keycloak
- π₯ User Management: Create, login, and manage users programmatically
- π Password Reset: Secure email-based password reset with JWT tokens
- π§ Email Integration: SMTP email service for notifications
- π³ Docker Support: Complete Keycloak setup with MariaDB
- π‘οΈ Security: OAuth2 Resource Server with JWT validation
This project demonstrates:
- How to guard endpoints with
@RolesAllowed
& path prefix - Create users with Spring Boot
- Login users and get access tokens with Spring Boot
- Implement secure password reset functionality
- Java 11 or higher
- Maven 3.6+
- Docker and Docker Compose
- MySQL/MariaDB (if not using Docker)
git clone https://github.com/devpayoub/Keycloak-with-Spring-Boot-Authentication-System.git
cd Keycloak-with-Spring-Boot-Authentication-System
cd src/main/resources
docker-compose up -d
This will start:
- Keycloak server on port
18080
- MariaDB database for Keycloak
- Access Keycloak admin console:
http://localhost:18080
- Login with admin credentials (check
keycloak.env
file) - Import the realm from
import-this-realm.json
Create application.properties
file in src/main/resources/
with the following configuration:
# Application Configuration
spring.application.name=test
server.port=8081
# Keycloak Configuration
keycloak.realm=think-auth
keycloak.auth-server-url=http://localhost:8080
keycloak.ssl-required=external
keycloak.resource=oauth2
keycloak.public-client=true
keycloak.confidential-port=0
# OAuth2 Resource Server Configuration
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/realms/think-auth
# Keycloak Client Secret (replace with your actual client secret)
keycloak.credentials.secret=YOUR_CLIENT_SECRET_HERE
keycloak.use-resource-role-mappings=false
keycloak.bearer-only=true
logging.level.org.springframework.security=DEBUG
logging.level.org.keycloak=DEBUG
# MySQL Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/keycloak_1
spring.datasource.username=YOUR_DB_USERNAME
spring.datasource.password=YOUR_DB_PASSWORD
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Email Configuration
spring.mail.host=YOUR_SMTP_HOST
spring.mail.port=465
spring.mail.username=YOUR_EMAIL_USERNAME
spring.mail.password=YOUR_EMAIL_PASSWORD
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
mvn clean install
mvn spring-boot:run
The application will start on http://localhost:8081
Variable | Description | Example |
---|---|---|
keycloak.realm |
Keycloak realm name | think-auth |
keycloak.auth-server-url |
Keycloak server URL | http://localhost:8080 |
keycloak.resource |
Keycloak client ID | oauth2 |
keycloak.credentials.secret |
Keycloak client secret | your-client-secret |
spring.datasource.url |
Database connection URL | jdbc:mysql://localhost:3306/keycloak_1 |
spring.datasource.username |
Database username | root |
spring.datasource.password |
Database password | your-db-password |
spring.mail.host |
SMTP server host | smtp.gmail.com |
spring.mail.username |
Email username | your-email@gmail.com |
spring.mail.password |
Email password | your-email-password |
GET /public/hello
- Public greeting message
GET /member/hello
- Requires MEMBER roleGET /moderator/hello
- Requires MODERATOR roleGET /admin/hello
- Requires ADMIN role
POST /user/create
- Create new userPOST /user/login
- User loginPOST /user/forgot-password
- Request password resetPOST /user/reset-password
- Reset password with token
- User Registration: Admin creates user via
/user/create
- User Login: User authenticates via
/user/login
- Token Validation: JWT tokens are validated for protected endpoints
- Role-based Access: Endpoints are protected based on user roles
The project uses MySQL/MariaDB for Keycloak data storage. When using Docker Compose, the database is automatically configured.
For manual setup:
- Create database:
keycloak_1
- Update database credentials in
application.properties
- Keycloak will create necessary tables automatically
The application supports email notifications for password reset. Configure your SMTP settings in application.properties
:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your-email@gmail.com
spring.mail.password=your-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
The project includes Docker Compose configuration for easy Keycloak setup:
version: '3'
services:
keycloak:
image: jboss/keycloak:15.0.2
ports:
- "18080:8080"
depends_on:
- keycloak_db
keycloak_db:
image: mariadb:10.3.26
- OAuth2 Resource Server: JWT token validation
- Role-based Access Control: Fine-grained authorization
- Password Reset: Secure token-based password reset
- Email Verification: User email verification support
- CSRF Protection: Configurable CSRF protection
curl -X POST http://localhost:8081/user/create \
-H "Content-Type: application/json" \
-d '{
"firstname": "John",
"lastname": "Doe",
"email": "john.doe@example.com",
"password": "password123"
}'
curl -X POST http://localhost:8081/user/login \
-H "Content-Type: application/json" \
-d '{
"username": "john.doe@example.com",
"password": "password123"
}'
curl -X GET http://localhost:8081/member/hello \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
src/main/java/com/oauth/keycloak/springbootauth/
βββ controller/ # REST API endpoints
β βββ HelloController.java
β βββ UserController.java
βββ service/ # Business logic
β βββ KeycloakAdminClientService.java
β βββ TokenService.java
βββ config/ # Configuration classes
β βββ KeycloakSecurityConfig.java
β βββ KeycloakProvider.java
β βββ KeycloakResolverConfig.java
βββ http/ # Request/Response models
βββ requests/
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
- Never commit sensitive information like passwords, API keys, or database credentials
- Use environment variables or external configuration for production deployments
- The
application.properties
file is excluded from version control for security reasons - Always use HTTPS in production environments