Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

Opensabre is a microservices development platform based on Spring Cloud 2023, integrating Spring Security, Spring Cloud Alibaba, and other components. It provides foundational RBAC permission management, authorization authentication, gateway management, service governance, audit logging, and other system management applications.

## Build and Development Commands

### Building the Project
```bash
# Build all modules
mvn clean install

# Build with tests
mvn clean install -DskipTests=false

# Skip tests for faster builds
mvn clean install -DskipTests=true

# Deploy to Maven Central (requires deploy profile)
mvn clean deploy -Pdeploy
```

### Testing
```bash
# Run all tests
mvn test

# Run tests for specific module
cd opensabre-web && mvn test

# Run single test class
mvn test -Dtest=UserContextHolderTest
```

### Code Quality
```bash
# Generate JavaDoc
mvn javadoc:javadoc

# Generate flattened POM (for deployment)
mvn flatten:flatten
```

## Architecture and Module Structure

### Core Modules

- **opensabre-base-dependencies**: Central dependency management with Spring Boot 3.4.1, Spring Cloud 2024.0.0, and Spring Cloud Alibaba 2023.0.3.2
- **opensabre-web**: Core web utilities including exception handling, validation, and entity converters
- **opensabre-starter-boot**: Auto-configuration for global exception handling, response wrapping, and sensitive data processing
- **opensabre-starter-rpc**: RPC components with OpenFeign configuration, load balancing, and Sentinel integration
- **opensabre-starter-persistence**: MyBatis Plus integration with base entity classes and meta object handlers
- **opensabre-starter-cache**: Redis caching with JetCache implementation
- **opensabre-starter-config**: Configuration management
- **opensabre-starter-register**: Service discovery and registration (Nacos)
- **opensabre-starter-eda**: Event-driven architecture components
- **opensabre-starter-webmvc**: Web MVC utilities and exception handling
- **opensabre-test**: Testing utilities

### Key Architectural Patterns

#### 1. Unified Response Format
All REST APIs return `Result<T>` objects with standardized structure:
- `code`: "000000" for success, other codes for errors
- `mesg`: Human-readable message
- `time`: Timestamp in UTC format
- `data`: Response payload (nullable)

#### 2. Entity Layering
- **BasePo**: Base persistence object with audit fields (createdBy, createdTime, updatedBy, updatedTime)
- **BaseVo**: Base view object for API responses
- **BaseForm**: Base form object for API requests
- **BaseQueryForm**: Base query form for search operations

#### 3. Exception Handling
- Global exception handling via `DefaultGlobalExceptionHandlerAdvice`
- Custom exception hierarchy with `BaseException` and `ServiceException`
- Error type system with `ErrorType` and `SystemErrorType`

#### 4. Sensitive Data Processing
- Log desensitization for sensitive fields (passwords, names)
- REST API response desensitization
- Configurable desensitization strategies

#### 5. Service Communication
- OpenFeign with custom load balancing (`OpensabreLoadBalancer`)
- Header propagation via `FeignHeaderInterceptor`
- Sentinel integration for circuit breaking

## Development Guidelines

### Adding New Modules
1. Create module directory under root
2. Add module to parent POM's `<modules>` section
3. Define dependencies in `opensabre-base-dependencies/pom.xml`
4. Use `@AutoConfiguration` for Spring Boot auto-configuration classes

### Creating REST Endpoints
- Extend from `BasePo` for persistence entities
- Use `Result<T>` for all API responses
- Implement proper exception handling
- Follow the entity conversion pattern: `Po -> Vo`

### Database Operations
- Extend `BasePo` for entity classes
- Use MyBatis Plus for ORM
- Implement `PoMetaObjectHandler` for audit field population

### Configuration
- Use YAML configuration files with `YamlPropertyLoaderFactory`
- Place configuration in `src/main/resources/`
- Follow naming convention: `opensabre-{module}.yml`

## Technology Stack

- **Java 17**: Primary development language
- **Spring Boot 3.4.1**: Application framework
- **Spring Cloud 2024.0.0**: Microservices framework
- **Spring Cloud Alibaba 2023.0.3.2**: Alibaba cloud components
- **MyBatis Plus 3.5.5**: ORM framework
- **JetCache 2.7.7**: Multi-level caching
- **Knife4j 4.5.0**: API documentation
- **Hutool 5.8.35**: Utility library
- **JUnit 5**: Testing framework

## Deployment

### Docker Support
- Dockerfile Maven plugin for image building
- Jib Maven plugin for containerization
- Base image: `eclipse-temurin:21-jre-alpine`

### Maven Central Publishing
- Uses `central-publishing-maven-plugin`
- Requires `deploy` profile activation
- Automatic source and JavaDoc packaging

## Testing Strategy

- Unit tests in `src/test/java` directories
- Integration tests for configuration validation
- Test utilities in `opensabre-test` module
- Use `@AutoConfiguration` for testing configuration classes
52 changes: 40 additions & 12 deletions opensabre-base-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<jib-maven-plugin.version>3.4.6</jib-maven-plugin.version>
<!-- 测试 -->
<junit-jupiter.version>5.11.4</junit-jupiter.version>
<httpclient5.version>5.3.1</httpclient5.version>
<httpclient5.version>5.4.1</httpclient5.version>
</properties>

<!-- 依赖管理定义 -->
Expand Down Expand Up @@ -99,7 +99,6 @@
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- 负载组件 -->
<dependency>
Expand All @@ -108,11 +107,6 @@
<version>${feign-interceptors}</version>
</dependency>
<!-- 服务调用负载 -->
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>${httpclient5.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
Expand Down Expand Up @@ -285,22 +279,56 @@
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<dependencies>
<!-- 添加所有权扩展 -->
<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-ownership-extension-maven</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
<configuration>
<pluginExtensions>
<pluginExtension>
<implementation>com.google.cloud.tools.jib.maven.extension.ownership.JibOwnershipExtension</implementation>
<configuration implementation="com.google.cloud.tools.jib.maven.extension.ownership.Configuration">
<rules>
<rule>
<glob>/app/logs/**</glob>
<ownership>1001</ownership>
</rule>
</rules>
</configuration>
</pluginExtension>
</pluginExtensions>
<!-- 基础镜像 -->
<from>
<image>docker.1ms.run/eclipse-temurin:21-jre-alpine</image>
<image>eclipse-temurin:21-jre-alpine</image>
</from>
<!-- 目标镜像 -->
<to>
<image>opensabre/${project.artifactId}:${project.version}</image>
<image>${REGISTRY_URL}/${project.artifactId}:${project.version}</image>
<auth>
<username>${secret.id}</username>
<password>${secret.key}</password>
</auth>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
</to>
<!-- 容器配置 -->
<container>
<user>1001</user>
<workingDirectory>/app</workingDirectory>
<ports>
<port>8000</port>
</ports>
<volumes>
<volume>/app/logs</volume>
</volumes>
<!-- 设置环境变量 -->
<environment>
<!-- 设置时区 -->
<TZ>Asia/Shanghai</TZ>
</environment>
<jvmFlags>-XX:+UseContainerSupport</jvmFlags>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
Expand Down
4 changes: 4 additions & 0 deletions opensabre-starter-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,9 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.opensabre.boot.annotations;

import java.lang.annotation.*;

/**
* 审计日志注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Audit {

/**
* 操作类型
*/
OperationType operationType();

/**
* 操作描述
*/
String description();

/**
* 操作模块
*/
String module() default "";

/**
* 是否记录请求参数
*/
boolean request() default true;

/**
* 是否记录响应结果
*/
boolean response() default false;

/**
* 目标对象关键信息,可支持 spel
*/
String key() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.opensabre.boot.annotations;

import io.github.opensabre.boot.aspect.AuditAspect;
import io.github.opensabre.boot.event.DefaultAuditEventHandler;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

/**
* 审计日志注解
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({AuditAspect.class, DefaultAuditEventHandler.class})
public @interface EnabledAudit {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.opensabre.boot.annotations;

public enum OperationType {
CREATE, UPDATE, DELETE, QUERY, LOGIN, LOGOUT, SCAN, EXPORT, IMPORT, DOWNLOAD, UPLOAD
}
Loading