A comprehensive Spring Boot-based authentication and authorization system with RBAC (Role-Based Access Control) support.
- User Authentication: JWT-based authentication with secure token generation
- User Management: Complete CRUD operations with pagination
- Role Management: Create, update, delete and assign roles
- RBAC System: Fine-grained permission control using roles and menus
- Menu Management: Hierarchical menu system with permissions
- API Documentation: Swagger/OpenAPI integration for easy testing
- Security: Spring Security with method-level authorization
- Java 17 or higher
- MySQL 8.0 or higher (or H2 for development)
- Maven 3.6+
git clone <repository-url>
cd auth-system
mvn clean compile
mvn spring-boot:run
Application starts on: http://localhost:8080
The application will automatically:
- โ Initialize H2 in-memory database
- โ Create all tables and relationships
- โ Insert sample data (admin user, roles, menus)
- โ Start on port 8080
Once the application is running, test these endpoints in order:
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}'
Expected Response:
{
"accessToken": "eyJhbGciOiJIUzI1NiJ9...",
"tokenType": "Bearer",
"username": "admin",
"roles": ["ADMIN"],
"menus": [
{
"id": 1,
"name": "็จๆท็ฎก็",
"path": "/users",
"component": "UserManagement",
"children": [...]
}
]
}
curl -X GET "http://localhost:8080/api/users?page=0&size=10" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
Expected Response:
{
"content": [
{
"id": 1,
"username": "admin",
"email": "admin@example.com",
"roles": ["ADMIN"],
"status": 1
}
],
"page": 0,
"size": 10,
"totalElements": 3,
"totalPages": 1
}
curl -X POST http://localhost:8080/api/users \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "test123",
"email": "test@example.com",
"phone": "13800138000"
}'
Expected Response:
{
"id": 4,
"username": "testuser",
"email": "test@example.com",
"phone": "13800138000",
"status": 1,
"roles": []
}
curl -X GET http://localhost:8080/api/users
Expected Response:
{
"timestamp": "2024-01-01T00:00:00.000+00:00",
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource"
}
- Swagger UI: http://localhost:8080/swagger-ui.html
- H2 Console: http://localhost:8080/h2-console
- JDBC URL:
jdbc:h2:mem:auth_system
- Username:
sa
- Password: (empty)
- JDBC URL:
The system automatically creates these test accounts:
Username | Password | Role | Description |
---|---|---|---|
admin | admin123 | ADMIN | Full system access |
manager | admin123 | MANAGER | Management access |
user1 | admin123 | USER | Regular user access |
user2 | admin123 | USER | Regular user access |
Access H2 Console to see:
- users table with 4 users
- roles table with 3 roles
- menus table with hierarchical menu structure
- user_roles and role_menus relationship tables
# 1. Login
curl -s -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' | jq
# 2. Extract token and use it
TOKEN=$(curl -s -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' | jq -r '.accessToken')
echo "Token: $TOKEN"
# 3. Test authentication
curl -s -X GET http://localhost:8080/api/users \
-H "Authorization: Bearer $TOKEN" | jq
Once the application is running, you can access the API documentation at:
- Swagger UI: http://localhost:8080/swagger-ui.html
- API Docs: http://localhost:8080/api-docs
The system comes with pre-configured users for testing:
Username | Password | Role | Description |
---|---|---|---|
admin | admin123 | ADMIN | Full system access |
user1 | admin123 | USER | Regular user access |
POST /api/auth/login
- User loginGET /api/auth/me
- Get current user info
GET /api/users
- Get all users (paginated)GET /api/users/{id}
- Get user by IDPOST /api/users
- Create new userPUT /api/users/{id}
- Update userDELETE /api/users/{id}
- Delete userPATCH /api/users/{id}/status
- Update user status
GET /api/roles
- Get all roles (paginated)GET /api/roles/{id}
- Get role by IDPOST /api/roles
- Create new rolePUT /api/roles/{id}
- Update roleDELETE /api/roles/{id}
- Delete roleGET /api/roles/active
- Get active roles
GET /api/users/{userId}/roles
- Get user rolesPOST /api/users/{userId}/roles
- Assign multiple rolesPOST /api/users/{userId}/roles/{roleId}
- Assign single roleDELETE /api/users/{userId}/roles/{roleId}
- Remove role
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"password": "admin123"
}'
curl -X GET http://localhost:8080/api/users \
-H "Authorization: Bearer <your-jwt-token>"
curl -X POST http://localhost:8080/api/users \
-H "Authorization: Bearer <your-jwt-token>" \
-H "Content-Type: application/json" \
-d '{
"username": "newuser",
"password": "password123",
"email": "newuser@example.com",
"phone": "1234567890"
}'
- JWT Token: 2-hour expiration time
- Password Encryption: BCrypt hashing
- Role-based Access Control: Method-level security with
@PreAuthorize
- Input Validation: Comprehensive validation using Bean Validation
- SQL Injection Prevention: Using JPA/Hibernate
src/main/java/com/example/authsystem/
โโโ config/ # Security and Swagger configuration
โโโ controller/ # REST API controllers
โโโ dto/ # Data Transfer Objects
โโโ entity/ # JPA entities
โโโ repository/ # Data repositories
โโโ security/ # JWT and security components
โโโ service/ # Business logic services
- users: User accounts
- roles: System roles
- menus: Menu items and permissions
- user_roles: User-role relationships
- role_menus: Role-menu relationships
- Swagger Testing: Use Swagger UI for interactive API testing
- H2 Console: Access at http://localhost:8080/h2-console (dev mode)
- Logs: Enable debug logging in
application.yml
:logging: level: com.example.authsystem: DEBUG
-
Database Connection Issues:
- Ensure MySQL is running
- Check database credentials
- Verify database exists
-
Port Already in Use:
- Change port in
application.yml
:
server: port: 8081
- Change port in
-
JWT Token Issues:
- Ensure token is included in Authorization header
- Check token expiration
- Verify token format:
Bearer <token>
For issues or questions, please create an issue in the repository or contact the development team.
This project is licensed under the MIT License - see the LICENSE file for details.