- 新增 SystemLoginLog 实体、Mapper、Service 及 XML 配置 - UserController 登录接口补充异常捕获与失败日志记录 - 删除旧压缩日志,新增最新日志文件 - 补充 AGENTS.md 使用说明文档
6.9 KiB
AGENTS.md
This file provides guidance to Codex (Codex.ai/code) when working with code in this repository.
Project Overview
This is a Spring Boot 2.7.2 application serving as a multi-tenant system with AI integration capabilities. The application focuses on host/streamer data management with AI chat functionality, built on a layered architecture pattern.
Key Technologies:
- Spring Boot 2.7.2 with Java 17
- MyBatis-Plus 3.5.2 for data access
- SA-Token 1.44.0 for authentication
- Redis for distributed caching and session management
- RabbitMQ for message-driven architecture
- Knife4j 4.4.0 for API documentation
- MySQL database
Build and Run Commands
Development
# Run the application (dev profile is default)
mvn spring-boot:run
# Build the project
mvn clean package
# Run tests
mvn test
# Skip tests during build
mvn clean package -DskipTests
Database Setup
# Initialize database using the SQL script
mysql -u root -p < sql/create_table.sql
Important: Update database credentials in src/main/resources/application.yml before running.
Access Points
- Application: http://localhost:8101/api
- API Documentation: http://localhost:8101/api/doc.html
Architecture
Layered Structure
The codebase follows a standard layered architecture:
Controller Layer (controller/) → Service Layer (service/) → Mapper Layer (mapper/) → Database
Key Packages
annotation/- Custom annotations like@AuthCheckfor role-based access controlaop/- Aspect-oriented programming for cross-cutting concerns (logging, auth)config/- Spring configuration classes for SA-Token, MyBatis-Plus, Redis, RabbitMQ, CORSmodel/entity/- Database entities mapped with MyBatis-Plusmodel/dto/- Data Transfer Objects for API requestsmodel/vo/- View Objects for API responsesmodel/enums/- Enumerations includingLoginSceneEnumfor multi-scenario authenticationexception/- Custom exception handling withGlobalExceptionHandlerutils/- Utility classes for common operations
Authentication System (SA-Token)
The application uses SA-Token with multiple login scenarios:
- HOST - Host/streamer login (
/user/doLogin) - BIG_BROTHER - Admin tenant login (
/user/bigbrother-doLogin) - AI_CHAT - AI chat login (
/user/aiChat-doLogin) - WEB_AI - Web AI login (
/user/webAi-doLogin)
Each scenario uses a different SA-Token login mode (defined in LoginSceneEnum) and has its own role validation logic in SystemUsersService.
Token Configuration:
- Token name:
vvtoken - Timeout: 172800 seconds (2 days)
- Non-concurrent login (new login kicks out old session)
- Token style: random-128
Public Endpoints (no authentication required):
- All Swagger/Knife4j documentation endpoints
- Login endpoints for all scenarios
/tenant/get-id-by-name/error
See SaTokenConfigure.java:35 for the complete list of excluded paths.
Database Layer (MyBatis-Plus)
Configuration Notes:
- Camel case to underscore mapping is disabled (
map-underscore-to-camel-case: false) - Field names in entities must match database column names exactly
- Logical deletion is enabled globally via
isDeletefield - Pagination is configured via
PaginationInnerInterceptor
Custom Mappers:
- Complex queries use XML mappers in
src/main/resources/mapper/ - Simple CRUD operations use MyBatis-Plus built-in methods
Message Queue (RabbitMQ)
The application uses HeadersExchange pattern for message routing. Configuration is in RabbitMQConfig.java.
Key Queues:
- Multiple business-specific queues with persistent messages
- Manual acknowledgment mode
- JSON message serialization with Jackson
Redis Integration
Redis is used for:
- Distributed session storage (Spring Session)
- SA-Token session management
- Custom caching needs
Configuration: See RedisConfig.java for JSON serialization setup with Jackson.
Multi-Tenant Architecture
The system supports multi-tenancy with:
SystemTenantentity for tenant managementSystemUsersentity with tenant associations- Tenant-specific authentication flows (BIG_BROTHER scenario)
- Tenant ID retrieval endpoint:
/tenant/get-id-by-name
Development Guidelines
Adding New Endpoints
- Create/update DTO in
model/dto/for request validation - Create/update VO in
model/vo/for response formatting - Add service method in appropriate service interface and implementation
- Add controller endpoint with proper
@AuthCheckannotation if authentication is required - If endpoint should be public, add path to
SaTokenConfigure.getExcludePaths()
Working with Authentication
Role-based Access Control:
@AuthCheck(mustRole = UserRoleEnum.ADMIN)
public BaseResponse<String> adminOnlyEndpoint() {
// Implementation
}
Multi-scenario Login:
- Use
LoginSceneEnumto determine the login scenario - Each scenario has its own SA-Token mode and role validation
- Logout endpoints are scenario-specific (e.g.,
/user/aiChat-logout)
Database Entities
Important: When creating or modifying entities:
- Use
@TableNameto specify exact table name - Use
@TableFieldfor fields that don't follow naming conventions - Include
isDeletefield for logical deletion support - Add
createTimeandupdateTimewith appropriate defaults
Code Generation
The project includes FreeMarker-based code generation utilities in the generate/ package for scaffolding new modules.
Configuration Files
application.yml- Main configuration with SA-Token, database, and server settingsapplication-dev.yml- Development environment overridesapplication-prod.yml- Production environment overrides
Active Profile: Defaults to dev (see application.yml:9)
Important Notes
- Redis: Currently disabled in
MainApplication.java. To enable, removeRedisAutoConfigurationfrom theexcludelist and uncomment session store-type inapplication.yml:17 - MyBatis Logging: Disabled by default in static block of
MainApplication.java:24-27 - Context Path: All endpoints are prefixed with
/api(seeapplication.yml:42) - File Upload Limit: 10MB (see
application.yml:37) - CORS: Configured to allow all origins in
SaTokenConfigure.corsHandle()
Testing
Tests are located in src/test/java/. The project uses Spring Boot Test framework.
Run specific test class:
mvn test -Dtest=ClassName
Common Issues
- Authentication Errors: Verify the correct login endpoint is being used for the scenario (HOST, BIG_BROTHER, AI_CHAT, WEB_AI)
- Database Connection: Ensure MySQL is running and credentials in
application.ymlare correct - Redis Connection: If Redis features are needed, enable Redis in
MainApplication.javaand configure connection inapplication.yml - Field Mapping Issues: Remember that camel-to-underscore mapping is disabled; field names must match exactly