# java **Repository Path**: MccreeLiu/java ## Basic Information - **Project Name**: java - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-15 - **Last Updated**: 2025-09-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # User Management API A RESTful API for user registration, login, and profile management. ## Features - User registration with username and password only (simplified registration) - User profile completion with nickname, age, email, and gender (via update endpoint) - User login with authentication and Bearer token generation - Profile viewing and updating using JWT token authentication - RESTful API endpoints - Interface-based architecture for better modularity and testability - Unit tests with mocking framework ## Prerequisites - Java Development Kit (JDK) 11 or higher - MySQL database - MySQL username: `hh696912` with password: `asd123123` ## Database Setup 1. Make sure you have MySQL installed and running 2. The application will automatically create the database and table when started ## Project Structure ``` src/ ├── main/ │ ├── java/ │ │ └── com/example/usermanagement/ │ │ ├── UserManagementApiApplication.java # Spring Boot main application │ │ ├── User.java # User entity with JPA annotations │ │ ├── UserRepository.java # JPA repository for user operations │ │ ├── UserService.java # Service implementation │ │ ├── UserServiceInterface.java # Service interface │ │ ├── UserRestController.java # REST controller for API endpoints │ │ ├── JwtUtil.java # JWT utility for token generation and validation │ │ ├── JwtAuthenticationFilter.java # JWT authentication filter │ │ ├── SecurityConfig.java # Spring Security configuration │ │ └── ServiceFactory.java # Factory for creating services (legacy) │ └── resources/ │ ├── application.properties # Application configuration │ └── schema.sql # Database schema (reference) └── test/ └── java/ └── com/example/usermanagement/ ├── UserTest.java # Unit tests for User class ├── UserServiceTest.java # Unit tests for UserService ├── UserServiceIntegrationTest.java # Integration tests ├── TestSuite.java # Unit test suite └── IntegrationTestSuite.java # Integration test suite ``` ## API Endpoints ### Register a new user (simplified) ``` POST /api/users/register Content-Type: application/json { "username": "testuser", "password": "password123" } ``` ### Login (returns Bearer token) ``` POST /api/users/login Content-Type: application/json { "username": "testuser", "password": "password123" } // Response: { "message": "Login successful", "user": { "id": 1, "username": "testuser", "password": "$2a$10$...", "nickname": null, "age": 0, "email": null, "gender": null }, "token": "Bearer eyJhbGciOiJIUzUxMiJ9..." } ``` ### Get current user profile (requires Bearer token) ``` GET /api/users/profile Authorization: Bearer eyJhbGciOiJIUzUxMiJ9... ``` ### Update current user profile (requires Bearer token) ``` PUT /api/users/profile Content-Type: application/json Authorization: Bearer eyJhbGciOiJIUzUxMiJ9... { "username": "testuser", "password": "password123", "nickname": "Test User", "age": 25, "email": "test@example.com", "gender": "Male" } ``` ### Get user by username (public endpoint) ``` GET /api/users/{username} ``` ### Get all users (public endpoint) ``` GET /api/users ``` ## How to Build and Run ### Method 1: Using Maven 1. Compile the project: ``` mvn clean compile ``` 2. Run the application: ``` mvn spring-boot:run ``` ### Method 2: Build and Run JAR 1. Package the application: ``` mvn clean package ``` 2. Run the JAR file: ``` java -jar target/user-management-api-1.0-SNAPSHOT.jar ``` ## Dependencies - Spring Boot 2.7.5 - Spring Web for RESTful APIs - Spring Data JPA for database operations - Spring Security for authentication and authorization - MySQL Connector/J 8.0.33 for database connectivity - Spring Security Crypto 5.7.1 for password encryption (BCrypt) - JWT 0.11.5 for token generation and validation - JUnit 4.13.2 for testing - Mockito 3.12.4 for mocking in unit tests ## Security Passwords are securely hashed using BCrypt before being stored in the database. JWT tokens are generated upon successful login and must be included in the Authorization header for authenticated endpoints. All endpoints except registration and login require a valid JWT token.