Skip to content

LinusLuo666/AIProject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Authentication and Authorization System

A comprehensive Spring Boot-based authentication and authorization system with RBAC (Role-Based Access Control) support.

๐Ÿš€ Features

  • 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

๐Ÿ“‹ Prerequisites

  • Java 17 or higher
  • MySQL 8.0 or higher (or H2 for development)
  • Maven 3.6+

๐Ÿš€ Quick Start (Zero Configuration)

1. Clone and Build

git clone <repository-url>
cd auth-system
mvn clean compile

2. Start Application

mvn spring-boot:run

Application starts on: http://localhost:8080

3. Verify Success

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

โœ… Proof of Functionality

Live Testing Examples

Once the application is running, test these endpoints in order:

๐Ÿ”‘ Step 1: Login as Admin

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": [...]
    }
  ]
}

๐Ÿ‘ฅ Step 2: List Users (requires ADMIN role)

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
}

โš™๏ธ Step 3: Create New User

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": []
}

๐Ÿ” Step 4: Test RBAC - Try accessing without token

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"
}

๐ŸŽฏ Web Interface

๐Ÿ“Š Pre-loaded Data

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

๐Ÿ” Database Schema

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

๐Ÿš€ Quick Test Commands

# 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

๐Ÿ“– API Documentation

Once the application is running, you can access the API documentation at:

๐Ÿ”ง Default Users

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

๐ŸŽฏ API Endpoints

Authentication

  • POST /api/auth/login - User login
  • GET /api/auth/me - Get current user info

User Management

  • GET /api/users - Get all users (paginated)
  • GET /api/users/{id} - Get user by ID
  • POST /api/users - Create new user
  • PUT /api/users/{id} - Update user
  • DELETE /api/users/{id} - Delete user
  • PATCH /api/users/{id}/status - Update user status

Role Management

  • GET /api/roles - Get all roles (paginated)
  • GET /api/roles/{id} - Get role by ID
  • POST /api/roles - Create new role
  • PUT /api/roles/{id} - Update role
  • DELETE /api/roles/{id} - Delete role
  • GET /api/roles/active - Get active roles

User-Role Assignment

  • GET /api/users/{userId}/roles - Get user roles
  • POST /api/users/{userId}/roles - Assign multiple roles
  • POST /api/users/{userId}/roles/{roleId} - Assign single role
  • DELETE /api/users/{userId}/roles/{roleId} - Remove role

๐Ÿงช Testing the System

1. Login

curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }'

2. Get Users (with JWT token)

curl -X GET http://localhost:8080/api/users \
  -H "Authorization: Bearer <your-jwt-token>"

3. Create New User

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"
  }'

๐Ÿ” Security Features

  • 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

๐Ÿ—๏ธ Project Structure

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

๐Ÿ“Š Database Schema

  • users: User accounts
  • roles: System roles
  • menus: Menu items and permissions
  • user_roles: User-role relationships
  • role_menus: Role-menu relationships

๐Ÿšฆ Development Tips

  1. Swagger Testing: Use Swagger UI for interactive API testing
  2. H2 Console: Access at http://localhost:8080/h2-console (dev mode)
  3. Logs: Enable debug logging in application.yml:
    logging:
      level:
        com.example.authsystem: DEBUG

๐Ÿ› Troubleshooting

Common Issues:

  1. Database Connection Issues:

    • Ensure MySQL is running
    • Check database credentials
    • Verify database exists
  2. Port Already in Use:

    • Change port in application.yml:
    server:
      port: 8081
  3. JWT Token Issues:

    • Ensure token is included in Authorization header
    • Check token expiration
    • Verify token format: Bearer <token>

๐Ÿ“ž Support

For issues or questions, please create an issue in the repository or contact the development team.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages