mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-11-12 06:57:10 +08:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| af295d6938 | |||
| 802dcb5735 | |||
| cd69b2adb6 | |||
| f8437918de | |||
| f85839559a | |||
| 7666d56019 | |||
| 04e0b4b1cc | |||
| be4dec5a30 | |||
| 3535ac64f7 | |||
|
|
ca1b92cde3 | ||
| 1a97a1b709 | |||
| 0c334dadcc | |||
| 46773df9dd | |||
| 1fc80cda9e | |||
| 0cede6bf9f | |||
| 5d1601e6eb | |||
| 06d3a6ca41 | |||
| 08ef09c9b5 | |||
| f3f57b8a5e | |||
| 2388d32363 | |||
| 9fdbfdf8bb | |||
| e9b81f9466 | |||
| 427143db69 | |||
| 2208dbd028 | |||
| 9e5f33b2c9 | |||
| 260f484af9 | |||
|
|
4caf0a0db6 | ||
| 16b6e9b466 | |||
| eac5c1fa79 | |||
|
|
52fc9d086d | ||
| 5e0eea2ed8 | |||
| 784a56fd4f | |||
| d3fa00d14c | |||
| e55eb17d64 | |||
| 7571c05f9c | |||
| 4c4f98a86e | |||
|
|
bf51837991 | ||
| 22ebdfeb9f | |||
| b0a2a8c927 | |||
| ea6b316296 |
78
CHANGELOG.md
78
CHANGELOG.md
@@ -1,3 +1,81 @@
|
||||
## [v2.7.0](https://github.com/continew-org/continew-starter/compare/v2.6.0...v2.7.0) (2024-09-28)
|
||||
|
||||
### ✨ 新特性
|
||||
|
||||
- 【data/mp】新增乐观锁插件启用配置(默认关闭) ([08ef09c](https://github.com/continew-org/continew-starter/commit/08ef09c9b594dca75b39e36add38998826b234bf))
|
||||
- 【extension/tenant】新增 continew-starter-extension-tenant 多租户模块 ([1a97a1b](https://github.com/continew-org/continew-starter/commit/1a97a1b709ee0c04300fd39758506fd479da0713)) ([f843791](https://github.com/continew-org/continew-starter/commit/f8437918de342ee45d15df30c20de5e8d3b18379))
|
||||
- 【extension/datapermission】新增数据权限模块(原 data/mp 中数据权限移除) ([7666d56](https://github.com/continew-org/continew-starter/commit/7666d56019bb309dca004d43b0717f6bb0e56c8f))
|
||||
|
||||
### 💎 功能优化
|
||||
|
||||
- 【data/mp】移除多数据源依赖,如需使用可手动引入 ([06d3a6c](https://github.com/continew-org/continew-starter/commit/06d3a6ca412b0bdeba9c0e460db6a0b05215b6b3))
|
||||
- 完善 ConditionalOnProperty 配置 ([0cede6b](https://github.com/continew-org/continew-starter/commit/0cede6bf9fc89e0c5009e9721b5cea2cf73b890c))
|
||||
- 优化部分代码写法 ([1fc80cd](https://github.com/continew-org/continew-starter/commit/1fc80cda9eb5b377b30d834692dff58d8f93053b))
|
||||
- 优化代码格式 ([46773df](https://github.com/continew-org/continew-starter/commit/46773df9dd2dc473459d58fc17f650d3da260545))
|
||||
- 【data/mp】移除 QueryIgnore 的无用属性 ([0c334da](https://github.com/continew-org/continew-starter/commit/0c334dadcce9d74301dbcc3c336dc28ffc4cf62e))
|
||||
- 【file/excel】导出方法增加排除字段参数 ([3535ac6](https://github.com/continew-org/continew-starter/commit/3535ac64f79c7c3d8e03d8ed2a996ebdfab1ff92))
|
||||
- 统一部分命名风格 ([f858395](https://github.com/continew-org/continew-starter/commit/f85839559ad7002dffbe3c5999a75e801ef9c4d1))
|
||||
- 优化部分依赖传递范围 ([cd69b2a](https://github.com/continew-org/continew-starter/commit/cd69b2adb67cf17b12619f06b8e81492cbb41c26))
|
||||
|
||||
### 🐛 问题修复
|
||||
|
||||
- 【log/interceptor】修复 continew-starter.log.exclude-patterns 配置不生效的问题 ([ca1b92c](https://github.com/continew-org/continew-starter/commit/ca1b92cde3cf8f9d9ee0b7420f5b13f200e80781))
|
||||
- 【log/interceptor】修复全局配置和局部配置包含请求、响应体冲突 ([be4dec5](https://github.com/continew-org/continew-starter/commit/be4dec5a3039625e62d346dbb148206b602af6aa))
|
||||
|
||||
### 📦 依赖升级
|
||||
|
||||
- Spring Boot 3.2.7 => 3.2.10 ([802dcb5](https://github.com/continew-org/continew-starter/commit/802dcb5735562e911e3a51741cfcf17dbe59a89e))
|
||||
- MyBatis Plus 3.5.7 => 3.5.8
|
||||
- Redisson 3.35.0 => 3.36.0
|
||||
- CosID 2.9.6 => 2.9.8
|
||||
- SMS4J 3.2.1 => 3.3.3
|
||||
- X File Storage 2.2.0 => 2.2.1
|
||||
|
||||
## [v2.6.0](https://github.com/continew-org/continew-starter/compare/v2.5.2...v2.6.0) (2024-09-06)
|
||||
|
||||
### ✨ 新特性
|
||||
|
||||
- 【web】新增 isMatch 路径是否匹配方法 ([e55eb17](https://github.com/continew-org/continew-starter/commit/e55eb17d64d7b42c6e64b18a645cda9558f08d58))
|
||||
- 【log】不记录日志也支持开启打印访问日志 ([16b6e9b](https://github.com/continew-org/continew-starter/commit/16b6e9b466578d935ab9a8a85a5eac4d09025dee))
|
||||
|
||||
### 💎 功能优化
|
||||
|
||||
- 【data】移除 DataPermission 注解的 value 属性 ([d3fa00d](https://github.com/continew-org/continew-starter/commit/d3fa00d14ce95ab1bedaebbce8304e29df5da8fd))
|
||||
- 【data】mybatis-plus => mp,mybatis-flex => mf ([5e0eea2](https://github.com/continew-org/continew-starter/commit/5e0eea2ed801b526da9f65b8cb2942b3b7b050ef))
|
||||
- 【web】提升接口文档响应类型优化扩展性 ([784a56f](https://github.com/continew-org/continew-starter/commit/784a56fd4f85ae170b8a56dd0a64a33a7bff98bc))
|
||||
- 【web】链路追踪配置属性响应头名称 => 链路 ID 名称 ([260f484](https://github.com/continew-org/continew-starter/commit/260f484af98d112927edec4316c0375e628dd3ba))
|
||||
- 【log】优化接口耗时相关时间类型使用 ([4caf0a0](https://github.com/continew-org/continew-starter/commit/4caf0a0db69779a5ea409ec7e01c4044817afc94))
|
||||
|
||||
### 🐛 问题修复
|
||||
|
||||
- 【log】修复日志全局 includes 配置会被局部修改的问题 ([eac5c1f](https://github.com/continew-org/continew-starter/commit/eac5c1fa79f7f91217b0525a07e0f1314f8efe24))
|
||||
- 【data】还原 SQL 函数接口 ([9e5f33b](https://github.com/continew-org/continew-starter/commit/9e5f33b2c9b804741f9b77eab5b67ab3c4a8b220))
|
||||
- 【crypto】修复由于升级 mybatis plus 引发的更新场景加密失效问题 ([e9b81f9](https://github.com/continew-org/continew-starter/commit/e9b81f946603e2f9e44b50471102b2551b9d50ac)) ([9fdbfdf](https://github.com/continew-org/continew-starter/commit/9fdbfdf8bb6719d3d044c71a2a8ceab85ccef8f1))
|
||||
|
||||
### 📦 依赖升级
|
||||
|
||||
- Graceful Response 4.0.1-boot3 => 5.0.0-boot3 ([2208dbd](https://github.com/continew-org/continew-starter/commit/2208dbd0282964aab76b02e811d6689ba69d75fd))
|
||||
- Snail Job 1.1.0 => 1.1.2
|
||||
- Sa Token 1.38.0 => 1.39.0
|
||||
- MyBatis Flex 1.9.3 => 1.9.7
|
||||
- Redisson 3.32.0 => 3.35.0
|
||||
- Cos ID 2.9.1 => 2.9.6
|
||||
- Hutool 5.8.29 => 5.8.32
|
||||
- aws-java-sdk-s3 1.12.761 => 1.12.771
|
||||
- snakeyaml 2.2 => 2.3
|
||||
|
||||
## [v2.5.2](https://github.com/continew-org/continew-starter/compare/v2.5.1...v2.5.2) (2024-08-14)
|
||||
|
||||
### 💎 功能优化
|
||||
|
||||
- 【api-doc】重构接口文档枚举展示处理 ([bf51837](https://github.com/continew-org/continew-starter/commit/bf51837991746c5576d7baffcc2296fe8ca91586)) ([4c4f98a](https://github.com/continew-org/continew-starter/commit/4c4f98a86e6d10b2b52814406bd0dc3d248c8486))
|
||||
- 【web】针对最新响应风格增加全局响应格式 ([bf51837](https://github.com/continew-org/continew-starter/commit/bf51837991746c5576d7baffcc2296fe8ca91586)) ([4c4f98a](https://github.com/continew-org/continew-starter/commit/4c4f98a86e6d10b2b52814406bd0dc3d248c8486))
|
||||
|
||||
### 🐛 问题修复
|
||||
|
||||
- 【extension/crud】重构排序字段处理,预防 SQL 注入问题 ([c31fa75](https://github.com/continew-org/continew-starter/commit/c31fa753b6d9753d72a2e28bf3184981ed848ac2)) ([22ebdfe](https://github.com/continew-org/continew-starter/commit/22ebdfeb9f30a8832825424a05343acfaf31643b))
|
||||
- 【security/crypto】修复 updateById 修改未正确加密的问题 ([b0a2a8c](https://github.com/continew-org/continew-starter/commit/b0a2a8c927171795e3ed89a820caf829f76c80ee))
|
||||
|
||||
## [v2.5.1](https://github.com/continew-org/continew-starter/compare/v2.5.0...v2.5.1) (2024-08-12)
|
||||
|
||||
### 🐛 问题修复
|
||||
|
||||
147
README.md
147
README.md
@@ -72,6 +72,11 @@ ContiNew Starter 就是将脚手架项目中的通用基础配置进行了封装
|
||||
|
||||
第一种方式:如您使用的是 Spring Boot Parent 的方式,则替换 Spring Boot Parent 为 ContiNew Starter
|
||||
|
||||
> 最新稳定版(latest-version)
|
||||
<a href="https://central.sonatype.com/search?q=continew-starter" target="_blank" rel="noopener" style="display: inline-block;">
|
||||
<img src="https://img.shields.io/maven-central/v/top.continew/continew-starter.svg?label=Maven%20Central&logo=sonatype&logoColor=FFF" alt="Release" />
|
||||
</a>
|
||||
|
||||
```xml
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
@@ -137,100 +142,54 @@ continew-starter.web:
|
||||
|
||||
## 模块结构
|
||||
|
||||
### 核心模块
|
||||
|
||||
| 模块名称 | 模块说明 | 依赖版本 |
|
||||
| --------------------- | ------------------------------------ |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| continew-starter-core | 核心模块:包含线程池、项目等自动配置 | <a href="https://spring.io/projects/spring-boot" target="_blank">Spring Boot</a>:3.1.11<br /><a href="https://www.hutool.cn/" target="_blank">Hutool</a>:5.8.29<br />mica-ip2region:3.2.6 |
|
||||
|
||||
### JSON模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ----------------------------- | -------------------- |
|
||||
| continew-starter-json-jackson | Jackson 序列化等配置 |
|
||||
|
||||
### 接口文档
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ------------------------ | ---------------- |
|
||||
| continew-starter-api-doc | Knife4j 自动配置 |
|
||||
|
||||
### 安全模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ---------------------------------- | ----------------- |
|
||||
| continew-starter-security-password | 密码编码器 |
|
||||
| continew-starter-security-mask | JSON 脱敏 |
|
||||
| continew-starter-security-crypto | 数据库字段加/解密 |
|
||||
| continew-starter-security-limiter | 限流器 |
|
||||
|
||||
### Web模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| -------------------- | ---------------------------------- |
|
||||
| continew-starter-web | 跨域、全局异常、错误处理等自动配置 |
|
||||
|
||||
### 日志模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
|----------------------------------| ----------------------------------------- |
|
||||
| continew-starter-log-core | 日志核心模块 |
|
||||
| continew-starter-log-interceptor | 拦截器版(Spring Boot Actuator HttpTrace 增强版) |
|
||||
|
||||
### 存储模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ------------------------------ | -------- |
|
||||
| continew-starter-storage-local | 本地存储 |
|
||||
|
||||
### 文件处理模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| --------------------------- | -------------- |
|
||||
| continew-starter-file-excel | Excel 相关配置 |
|
||||
|
||||
### 验证码模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| --------------------------------- | ---------- |
|
||||
| continew-starter-captcha-graphic | 图形验证码 |
|
||||
| continew-starter-captcha-behavior | 行为验证码 |
|
||||
|
||||
### 缓存模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ---------------------------------- | --------------------- |
|
||||
| continew-starter-cache-redisson | Redisson 自动配置 |
|
||||
| continew-starter-cache-springcache | Spring Cache 自动配置 |
|
||||
| continew-starter-cache-jetcache | JetCache 自动配置 |
|
||||
|
||||
### 数据访问模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ---------------------------------- | --------------------- |
|
||||
| continew-starter-data-core | 数据访问核心模块 |
|
||||
| continew-starter-data-mybatis-plus | MyBatis Plus 自动配置 |
|
||||
| continew-starter-data-mybatis-flex | MyBatis Flex 自动配置 |
|
||||
|
||||
### 认证模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ------------------------------ | ----------------- |
|
||||
| continew-starter-auth-satoken | SaToken 自动配置 |
|
||||
| continew-starter-auth-justauth | JustAuth 自动配置 |
|
||||
|
||||
### 消息模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ------------------------------------ | --------- |
|
||||
| continew-starter-messaging-mail | 邮件 |
|
||||
| continew-starter-messaging-websocket | WebSocket |
|
||||
|
||||
### 扩展模块
|
||||
|
||||
| 模块名称 | 模块说明 |
|
||||
| ------------------------------- | --------------------------------------------- |
|
||||
| continew-starter-extension-crud | 扩展模块:BaseController 自定义 CRUD API 封装 |
|
||||
```
|
||||
continew-starter
|
||||
├─ continew-starter-core(核心模块:包含线程池等自动配置)
|
||||
├─ continew-starter-json(JSON 模块)
|
||||
│ └─ continew-starter-json-jackson
|
||||
├─ continew-starter-api-doc(接口文档模块:Spring Doc + Knife4j)
|
||||
├─ continew-starter-web(Web 开发模块:包含跨域、全局异常+响应、链路追踪等自动配置)
|
||||
├─ continew-starter-auth(认证模块)
|
||||
│ ├─ continew-starter-auth-satoken(国产轻量认证鉴权)
|
||||
│ └─ continew-starter-auth-justauth(第三方登录)
|
||||
├─ continew-starter-data(数据访问模块)
|
||||
│ ├─ continew-starter-data-core(通用模块)
|
||||
│ ├─ continew-starter-data-mp(MyBatis Plus)
|
||||
│ └─ continew-starter-data-mf(MyBatis Flex)
|
||||
├─ continew-starter-cache(缓存模块)
|
||||
│ ├─ continew-starter-cache-redisson(Redisson)
|
||||
│ ├─ continew-starter-cache-jetcache(JetCache 多级缓存)
|
||||
│ └─ continew-starter-cache-springcache(Spring 缓存)
|
||||
├─ continew-starter-security(安全模块)
|
||||
│ ├─ continew-starter-security-crypto(加密:字段加解密)
|
||||
│ ├─ continew-starter-security-mask(脱敏:JSON 数据脱敏)
|
||||
│ ├─ continew-starter-security-limiter(限流)
|
||||
│ └─ continew-starter-security-password(密码编码器)
|
||||
├─ continew-starter-captcha(验证码模块)
|
||||
│ ├─ continew-starter-captcha-graphic(静态验证码)
|
||||
│ └─ continew-starter-captcha-behavior(动态验证码)
|
||||
├─ continew-starter-messaging(消息模块)
|
||||
│ ├─ continew-starter-messaging-mail(邮件)
|
||||
│ └─ continew-starter-messaging-websocket(WebSocket)
|
||||
├─ continew-starter-log(日志模块)
|
||||
│ ├─ continew-starter-log-core(通用模块)
|
||||
│ └─ continew-starter-log-interceptor(拦截器版(Spring Boot Actuator HttpTrace 增强版))
|
||||
├─ continew-starter-file(文件处理模块)
|
||||
│ └─ continew-starter-file-excel(Easy Excel)
|
||||
├─ continew-starter-storage(存储模块)
|
||||
│ └─ continew-starter-storage-local(本地存储)
|
||||
└─ continew-starter-extension(扩展模块)
|
||||
├─ continew-starter-extension-datapermission(数据权限模块)
|
||||
│ ├─ continew-starter-extension-datapermission-core(通用模块)
|
||||
│ └─ continew-starter-extension-datapermission-mp(MyBatis Plus)
|
||||
├─ continew-starter-extension-tenant(多租户模块)
|
||||
│ ├─ continew-starter-extension-tenant-core(通用模块)
|
||||
│ └─ continew-starter-extension-tenant-mp(MyBatis Plus)
|
||||
└─ continew-starter-extension-crud(CRUD 模块)
|
||||
├─ continew-starter-extension-crud-core(通用模块)
|
||||
├─ continew-starter-extension-crud-mp(MyBatis Plus)
|
||||
└─ continew-starter-extension-crud-mf(MyBatis Flex)
|
||||
```
|
||||
|
||||
## 贡献代码
|
||||
|
||||
|
||||
@@ -17,23 +17,20 @@
|
||||
package top.continew.starter.apidoc.autoconfigure;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.info.License;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springdoc.core.configuration.SpringDocConfiguration;
|
||||
import org.springdoc.core.customizers.*;
|
||||
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
|
||||
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
|
||||
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
|
||||
import org.springdoc.core.properties.SpringDocConfigProperties;
|
||||
import org.springdoc.core.providers.JavadocProvider;
|
||||
import org.springdoc.core.service.OpenAPIService;
|
||||
@@ -48,15 +45,15 @@ import org.springframework.http.CacheControl;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import top.continew.starter.apidoc.handler.BaseEnumParameterHandler;
|
||||
import top.continew.starter.apidoc.handler.OpenApiHandler;
|
||||
import top.continew.starter.apidoc.util.EnumTypeUtils;
|
||||
import top.continew.starter.core.autoconfigure.project.ProjectProperties;
|
||||
import top.continew.starter.core.enums.BaseEnum;
|
||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* API 文档自动配置
|
||||
@@ -156,95 +153,14 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
|
||||
* 自定义 BaseEnum 枚举参数配置(针对实现了 BaseEnum 的枚举,优化其枚举值和描述展示)
|
||||
*
|
||||
* @return {@link BaseEnumParameterHandler }
|
||||
* @since 2.4.0
|
||||
*/
|
||||
@Bean
|
||||
public ParameterCustomizer customParameterCustomizer() {
|
||||
return (parameterModel, methodParameter) -> {
|
||||
Class<?> parameterType = methodParameter.getParameterType();
|
||||
// 判断是否为 BaseEnum 的子类型
|
||||
if (!ClassUtil.isAssignable(BaseEnum.class, parameterType)) {
|
||||
return parameterModel;
|
||||
}
|
||||
String description = parameterModel.getDescription();
|
||||
if (StrUtil.contains(description, "color:red")) {
|
||||
return parameterModel;
|
||||
}
|
||||
// 封装参数配置
|
||||
this.configureSchema(parameterModel.getSchema(), parameterType);
|
||||
// 自定义枚举描述
|
||||
parameterModel.setDescription(description + "<span style='color:red'>" + this
|
||||
.getDescMap(parameterType) + "</span>");
|
||||
return parameterModel;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
|
||||
*
|
||||
* @since 2.4.0
|
||||
*/
|
||||
@Bean
|
||||
public PropertyCustomizer customPropertyCustomizer() {
|
||||
return (schema, type) -> {
|
||||
Class<?> rawClass;
|
||||
// 获取原始类的类型
|
||||
if (type.getType() instanceof SimpleType) {
|
||||
rawClass = ((SimpleType)type.getType()).getRawClass();
|
||||
} else if (type.getType() instanceof CollectionType) {
|
||||
rawClass = ((CollectionType)type.getType()).getContentType().getRawClass();
|
||||
} else {
|
||||
rawClass = Object.class;
|
||||
}
|
||||
// 判断是否为 BaseEnum 的子类型
|
||||
if (!ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
|
||||
return schema;
|
||||
}
|
||||
// 封装参数配置
|
||||
this.configureSchema(schema, rawClass);
|
||||
// 自定义参数描述
|
||||
schema.setDescription(schema.getDescription() + "<span style='color:red'>" + this
|
||||
.getDescMap(rawClass) + "</span>");
|
||||
return schema;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装 Schema 配置
|
||||
*
|
||||
* @param schema Schema
|
||||
* @param enumClass 枚举类型
|
||||
* @since 2.4.0
|
||||
*/
|
||||
private void configureSchema(Schema schema, Class<?> enumClass) {
|
||||
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||
// 设置枚举可用值
|
||||
List<String> valueList = Arrays.stream(enums).map(e -> e.getValue().toString()).toList();
|
||||
schema.setEnum(valueList);
|
||||
// 设置枚举值类型和格式
|
||||
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(enumClass);
|
||||
schema.setType(enumValueType);
|
||||
switch (enumValueType) {
|
||||
case "integer" -> schema.setFormat("int32");
|
||||
case "long" -> schema.setFormat("int64");
|
||||
case "number" -> schema.setFormat("double");
|
||||
default -> schema.setFormat(enumValueType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举描述 Map
|
||||
*
|
||||
* @param enumClass 枚举类型
|
||||
* @return 枚举描述 Map
|
||||
* @since 2.4.0
|
||||
*/
|
||||
private Map<Object, String> getDescMap(Class<?> enumClass) {
|
||||
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||
return Arrays.stream(enums)
|
||||
.collect(Collectors.toMap(BaseEnum::getValue, BaseEnum::getDescription, (a, b) -> a, LinkedHashMap::new));
|
||||
public BaseEnumParameterHandler customParameterCustomizer() {
|
||||
return new BaseEnumParameterHandler();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.apidoc.handler;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import io.swagger.v3.core.converter.AnnotatedType;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||
import org.springdoc.core.customizers.ParameterCustomizer;
|
||||
import org.springdoc.core.customizers.PropertyCustomizer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import top.continew.starter.apidoc.util.DocUtils;
|
||||
import top.continew.starter.core.enums.BaseEnum;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 自定义 BaseEnum 枚举参数处理器
|
||||
* <p>
|
||||
* 针对实现了 BaseEnum 的枚举,优化其枚举值和描述展示
|
||||
* </p>
|
||||
*
|
||||
* @author echo
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public class BaseEnumParameterHandler implements ParameterCustomizer, PropertyCustomizer {
|
||||
|
||||
@Override
|
||||
public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
|
||||
Class<?> parameterType = methodParameter.getParameterType();
|
||||
// 判断是否为 BaseEnum 的子类型
|
||||
if (!ClassUtil.isAssignable(BaseEnum.class, parameterType)) {
|
||||
return parameterModel;
|
||||
}
|
||||
String description = parameterModel.getDescription();
|
||||
if (StrUtil.contains(description, "color:red")) {
|
||||
return parameterModel;
|
||||
}
|
||||
// 自定义枚举描述并封装参数配置
|
||||
configureSchema(parameterModel.getSchema(), parameterType);
|
||||
parameterModel.setDescription(appendEnumDescription(description, parameterType));
|
||||
return parameterModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema customize(Schema schema, AnnotatedType type) {
|
||||
Class<?> rawClass = resolveRawClass(type.getType());
|
||||
// 判断是否为 BaseEnum 的子类型
|
||||
if (!ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
|
||||
return schema;
|
||||
}
|
||||
// 自定义参数描述并封装参数配置
|
||||
configureSchema(schema, rawClass);
|
||||
schema.setDescription(appendEnumDescription(schema.getDescription(), rawClass));
|
||||
return schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装 Schema 配置
|
||||
*
|
||||
* @param schema Schema
|
||||
* @param enumClass 枚举类型
|
||||
*/
|
||||
private void configureSchema(Schema schema, Class<?> enumClass) {
|
||||
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||
List<String> valueList = Arrays.stream(enums).map(e -> e.getValue().toString()).toList();
|
||||
schema.setEnum(valueList);
|
||||
String enumValueType = DocUtils.getEnumValueTypeAsString(enumClass);
|
||||
schema.setType(enumValueType);
|
||||
schema.setFormat(DocUtils.resolveFormat(enumValueType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加枚举描述
|
||||
*
|
||||
* @param originalDescription 原始描述
|
||||
* @param enumClass 枚举类型
|
||||
* @return 追加后的描述字符串
|
||||
*/
|
||||
private String appendEnumDescription(String originalDescription, Class<?> enumClass) {
|
||||
return originalDescription + "<span style='color:red'>" + DocUtils.getDescMap(enumClass) + "</span>";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析原始类
|
||||
*
|
||||
* @param type 类型
|
||||
* @return 原始类的 Class 对象
|
||||
*/
|
||||
private Class<?> resolveRawClass(Type type) {
|
||||
if (type instanceof SimpleType simpleType) {
|
||||
return simpleType.getRawClass();
|
||||
} else if (type instanceof CollectionType collectionType) {
|
||||
return collectionType.getContentType().getRawClass();
|
||||
} else {
|
||||
return Object.class;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,12 +146,15 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
super(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider);
|
||||
if (openAPI.isPresent()) {
|
||||
this.openAPI = openAPI.get();
|
||||
if (this.openAPI.getComponents() == null)
|
||||
if (this.openAPI.getComponents() == null) {
|
||||
this.openAPI.setComponents(new Components());
|
||||
if (this.openAPI.getPaths() == null)
|
||||
}
|
||||
if (this.openAPI.getPaths() == null) {
|
||||
this.openAPI.setPaths(new Paths());
|
||||
if (!CollectionUtils.isEmpty(this.openAPI.getServers()))
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(this.openAPI.getServers())) {
|
||||
this.isServersPresent = true;
|
||||
}
|
||||
}
|
||||
this.propertyResolverUtils = propertyResolverUtils;
|
||||
this.securityParser = securityParser;
|
||||
@@ -159,8 +162,9 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
this.openApiBuilderCustomisers = openApiBuilderCustomizers;
|
||||
this.serverBaseUrlCustomizers = serverBaseUrlCustomizers;
|
||||
this.javadocProvider = javadocProvider;
|
||||
if (springDocConfigProperties.isUseFqn())
|
||||
if (springDocConfigProperties.isUseFqn()) {
|
||||
TypeNameResolver.std.setUseFqn(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -172,10 +176,11 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
buildTagsFromMethod(handlerMethod.getMethod(), tags, tagsStr, locale);
|
||||
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
|
||||
|
||||
if (!CollectionUtils.isEmpty(tagsStr))
|
||||
if (!CollectionUtils.isEmpty(tagsStr)) {
|
||||
tagsStr = tagsStr.stream()
|
||||
.map(str -> propertyResolverUtils.resolve(str, locale))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
if (springdocTags.containsKey(handlerMethod)) {
|
||||
Tag tag = springdocTags.get(handlerMethod);
|
||||
@@ -186,9 +191,9 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(tagsStr)) {
|
||||
if (CollectionUtils.isEmpty(operation.getTags()))
|
||||
if (CollectionUtils.isEmpty(operation.getTags())) {
|
||||
operation.setTags(new ArrayList<>(tagsStr));
|
||||
else {
|
||||
} else {
|
||||
Set<String> operationTagsSet = new HashSet<>(operation.getTags());
|
||||
operationTagsSet.addAll(tagsStr);
|
||||
operation.getTags().clear();
|
||||
@@ -223,8 +228,9 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
if (!CollectionUtils.isEmpty(tags)) {
|
||||
// Existing tags
|
||||
List<Tag> openApiTags = openAPI.getTags();
|
||||
if (!CollectionUtils.isEmpty(openApiTags))
|
||||
if (!CollectionUtils.isEmpty(openApiTags)) {
|
||||
tags.addAll(openApiTags);
|
||||
}
|
||||
openAPI.setTags(new ArrayList<>(tags));
|
||||
}
|
||||
|
||||
@@ -232,10 +238,11 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
io.swagger.v3.oas.annotations.security.SecurityRequirement[] securityRequirements = securityParser
|
||||
.getSecurityRequirements(handlerMethod);
|
||||
if (securityRequirements != null) {
|
||||
if (securityRequirements.length == 0)
|
||||
if (securityRequirements.length == 0) {
|
||||
operation.setSecurity(Collections.emptyList());
|
||||
else
|
||||
} else {
|
||||
securityParser.buildSecurityRequirement(securityRequirements, operation);
|
||||
}
|
||||
}
|
||||
|
||||
return operation;
|
||||
@@ -263,8 +270,9 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
tagsSet.forEach(tag -> {
|
||||
tag.name(propertyResolverUtils.resolve(tag.getName(), locale));
|
||||
tag.description(propertyResolverUtils.resolve(tag.getDescription(), locale));
|
||||
if (tags.stream().noneMatch(t -> t.getName().equals(tag.getName())))
|
||||
if (tags.stream().noneMatch(t -> t.getName().equals(tag.getName()))) {
|
||||
tags.add(tag);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.apidoc.util;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.continew.starter.core.enums.BaseEnum;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 接口文档工具类
|
||||
*
|
||||
* @author echo
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public class DocUtils {
|
||||
|
||||
private DocUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举值类型
|
||||
*
|
||||
* @param enumClass 枚举类型
|
||||
* @return 枚举值类型
|
||||
*/
|
||||
public static String getEnumValueTypeAsString(Class<?> enumClass) {
|
||||
// 获取枚举类实现的所有接口
|
||||
Type[] interfaces = enumClass.getGenericInterfaces();
|
||||
// 定义枚举值类型的映射
|
||||
Map<Class<?>, String> typeMap = Map
|
||||
.of(Integer.class, "integer", Long.class, "long", Double.class, "number", String.class, "string");
|
||||
// 遍历所有接口
|
||||
for (Type type : interfaces) {
|
||||
// 检查接口是否为参数化类型并且原始类型为 BaseEnum
|
||||
if (type instanceof ParameterizedType parameterizedType && parameterizedType
|
||||
.getRawType() == BaseEnum.class) {
|
||||
Type actualType = parameterizedType.getActualTypeArguments()[0];
|
||||
// 检查实际类型参数是否为类类型,并返回对应的字符串类型
|
||||
if (actualType instanceof Class<?> actualClass) {
|
||||
return typeMap.getOrDefault(actualClass, "string");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 默认返回 "string" 类型
|
||||
return "string";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析枚举值的格式
|
||||
*
|
||||
* @param enumValueType 枚举值类型
|
||||
* @return String 格式化类型
|
||||
*/
|
||||
public static String resolveFormat(String enumValueType) {
|
||||
return switch (enumValueType) {
|
||||
case "integer" -> "int32";
|
||||
case "long" -> "int64";
|
||||
case "number" -> "double";
|
||||
default -> enumValueType;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 具有 RestController 注释,既检查是否继承了BaseController
|
||||
*
|
||||
* @param clazz clazz
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean hasRestControllerAnnotation(Class<?> clazz) {
|
||||
// 如果注释包含 RestController 注解,则返回 true
|
||||
if (clazz.isAnnotationPresent(RestController.class)) {
|
||||
return true;
|
||||
}
|
||||
// 递归检查父类
|
||||
Class<?> superClass = clazz.getSuperclass();
|
||||
// 循环检查父类
|
||||
while (superClass != null && !superClass.equals(Object.class)) {
|
||||
// 如果父类包含 RestController 注解,则返回 true
|
||||
if (hasRestControllerAnnotation(superClass)) {
|
||||
return true;
|
||||
}
|
||||
// 递归检查接口
|
||||
superClass = superClass.getSuperclass();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举描述 Map
|
||||
*
|
||||
* @param enumClass 枚举类型
|
||||
* @return 枚举描述 Map
|
||||
*/
|
||||
public static Map<Object, String> getDescMap(Class<?> enumClass) {
|
||||
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||
return Arrays.stream(enums)
|
||||
.collect(Collectors.toMap(BaseEnum::getValue, BaseEnum::getDescription, (a, b) -> a, LinkedHashMap::new));
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.apidoc.util;
|
||||
|
||||
import top.continew.starter.core.enums.BaseEnum;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* 枚举类型工具
|
||||
*
|
||||
* @author echo
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public class EnumTypeUtils {
|
||||
|
||||
private EnumTypeUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举值类型
|
||||
*
|
||||
* @param enumClass 枚举类型
|
||||
* @return 枚举值类型
|
||||
*/
|
||||
public static String getEnumValueTypeAsString(Class<?> enumClass) {
|
||||
try {
|
||||
// 获取枚举类实现的所有接口
|
||||
Type[] interfaces = enumClass.getGenericInterfaces();
|
||||
// 遍历所有接口
|
||||
for (Type type : interfaces) {
|
||||
// 检查接口是否为参数化类型
|
||||
if (type instanceof ParameterizedType parameterizedType) {
|
||||
// 检查接口的原始类型是否为 BaseEnum
|
||||
if (parameterizedType.getRawType() != BaseEnum.class) {
|
||||
continue;
|
||||
}
|
||||
Type actualType = parameterizedType.getActualTypeArguments()[0];
|
||||
// 检查实际类型参数是否为类类型
|
||||
if (actualType instanceof Class<?> actualClass) {
|
||||
if (actualClass == Integer.class) {
|
||||
return "integer";
|
||||
} else if (actualClass == Long.class) {
|
||||
return "long";
|
||||
} else if (actualClass == Double.class) {
|
||||
return "number";
|
||||
} else if (actualClass == String.class) {
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// ignored
|
||||
}
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import top.continew.starter.auth.justauth.core.JustAuthStateCacheRedisImpl;
|
||||
import top.continew.starter.auth.justauth.core.AuthStateCacheRedisDefaultImpl;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
|
||||
/**
|
||||
@@ -35,19 +35,19 @@ import top.continew.starter.core.constant.PropertiesConstants;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfiguration(before = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class)
|
||||
@ConditionalOnProperty(prefix = "justauth", name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = "justauth", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class JustAuthAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(JustAuthAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* 自定义 State 缓存实现
|
||||
* State 缓存 Redis 实现(默认)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnClass(RedisClient.class)
|
||||
@ConditionalOnProperty(prefix = "justauth.cache", name = "type", havingValue = "redis")
|
||||
public AuthStateCache authStateCache() {
|
||||
JustAuthStateCacheRedisImpl impl = new JustAuthStateCacheRedisImpl();
|
||||
AuthStateCacheRedisDefaultImpl impl = new AuthStateCacheRedisDefaultImpl();
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Redis' completed initialization.");
|
||||
return impl;
|
||||
}
|
||||
|
||||
@@ -22,12 +22,12 @@ import top.continew.starter.cache.redisson.util.RedisUtils;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* JustAuth State 缓存 Redis 实现
|
||||
* 默认 State 缓存 Redis 实现
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class JustAuthStateCacheRedisImpl implements AuthStateCache {
|
||||
public class AuthStateCacheRedisDefaultImpl implements AuthStateCache {
|
||||
|
||||
private static final String KEY_PREFIX = "SOCIAL_AUTH_STATE";
|
||||
|
||||
@@ -29,7 +29,10 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import top.continew.starter.auth.satoken.autoconfigure.dao.SaTokenDaoConfiguration;
|
||||
@@ -78,8 +81,7 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
|
||||
@Configuration
|
||||
@Import({SaTokenDaoConfiguration.Default.class, SaTokenDaoConfiguration.Redis.class,
|
||||
SaTokenDaoConfiguration.Custom.class})
|
||||
protected static class SaTokenDaoAutoConfiguration {
|
||||
}
|
||||
protected static class SaTokenDaoAutoConfiguration {}
|
||||
|
||||
/**
|
||||
* 整合 JWT(简单模式)
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SaTokenDaoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层实现-Redis
|
||||
* 自定义持久层实现-Redis(默认)
|
||||
*/
|
||||
@ConditionalOnMissingBean(SaTokenDao.class)
|
||||
@ConditionalOnClass(RedisClient.class)
|
||||
@@ -73,7 +73,7 @@ public class SaTokenDaoConfiguration {
|
||||
|
||||
@Bean
|
||||
public SaTokenDao saTokenDao() {
|
||||
return new SaTokenDaoRedisImpl();
|
||||
return new SaTokenDaoRedisDefaultImpl();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,12 +26,12 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sa-Token 持久层 Redis 实现(参考:Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java)
|
||||
* 默认 Sa-Token 持久层 Redis 实现(参考:Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SaTokenDaoRedisImpl implements SaTokenDao {
|
||||
public class SaTokenDaoRedisDefaultImpl implements SaTokenDao {
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
@@ -46,7 +46,7 @@ import java.util.List;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(prefix = "spring.data.redisson", name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = "spring.data.redisson", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(RedissonProperties.class)
|
||||
public class RedissonAutoConfiguration {
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ import java.util.Properties;
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(BehaviorCaptchaProperties.class)
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class BehaviorCaptchaAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaAutoConfiguration.class);
|
||||
|
||||
@@ -35,7 +35,7 @@ import top.continew.starter.core.constant.PropertiesConstants;
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(GraphicCaptchaProperties.class)
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_GRAPHIC, name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_GRAPHIC, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class GraphicCaptchaAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GraphicCaptchaAutoConfiguration.class);
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package top.continew.starter.captcha.graphic.core;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.wf.captcha.base.Captcha;
|
||||
import top.continew.starter.captcha.graphic.autoconfigure.GraphicCaptchaProperties;
|
||||
|
||||
|
||||
@@ -31,5 +31,4 @@ import org.springframework.context.annotation.Import;
|
||||
@ComponentScan("cn.hutool.extra.spring")
|
||||
@Import(cn.hutool.extra.spring.SpringUtil.class)
|
||||
@EnableConfigurationProperties(ProjectProperties.class)
|
||||
public class ProjectAutoConfiguration {
|
||||
}
|
||||
public class ProjectAutoConfiguration {}
|
||||
|
||||
@@ -42,7 +42,7 @@ import java.util.concurrent.Executor;
|
||||
@Lazy
|
||||
@AutoConfiguration
|
||||
@EnableAsync(proxyTargetClass = true)
|
||||
@ConditionalOnProperty(prefix = "spring.task.execution.extension", name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = "spring.task.execution.extension", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class AsyncAutoConfiguration implements AsyncConfigurer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AsyncAutoConfiguration.class);
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ThreadPoolAutoConfiguration {
|
||||
* 异步任务线程池配置
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "spring.task.execution.extension", name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = "spring.task.execution.extension", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public ThreadPoolTaskExecutorCustomizer threadPoolTaskExecutorCustomizer(ThreadPoolExtensionProperties properties) {
|
||||
return executor -> {
|
||||
// 核心(最小)线程数
|
||||
@@ -71,7 +71,7 @@ public class ThreadPoolAutoConfiguration {
|
||||
* 定时任务线程池配置
|
||||
*/
|
||||
@EnableScheduling
|
||||
@ConditionalOnProperty(prefix = "spring.task.scheduling.extension", name = PropertiesConstants.ENABLED, matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = "spring.task.scheduling.extension", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public static class TaskSchedulerConfiguration {
|
||||
@Bean
|
||||
public ThreadPoolTaskSchedulerCustomizer threadPoolTaskSchedulerCustomizer(ThreadPoolExtensionProperties properties) {
|
||||
|
||||
@@ -119,6 +119,16 @@ public class PropertiesConstants {
|
||||
*/
|
||||
public static final String MESSAGING_WEBSOCKET = MESSAGING + StringConstants.DOT + "websocket";
|
||||
|
||||
/**
|
||||
* 数据权限配置
|
||||
*/
|
||||
public static final String DATA_PERMISSION = CONTINEW_STARTER + StringConstants.DOT + "data-permission";
|
||||
|
||||
/**
|
||||
* 多租户配置
|
||||
*/
|
||||
public static final String TENANT = CONTINEW_STARTER + StringConstants.DOT + "tenant";
|
||||
|
||||
private PropertiesConstants() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +120,7 @@ public class StringConstants {
|
||||
public static final String DOT = StrPool.DOT;
|
||||
|
||||
/**
|
||||
* 字符串常量:双点 {@code ".."} <br>
|
||||
* 用途:作为指向上级文件夹的路径,如:{@code "../path"}
|
||||
* 字符串常量:双点 {@code ".."} <br> 用途:作为指向上级文件夹的路径,如:{@code "../path"}
|
||||
*/
|
||||
public static final String DOUBLE_DOT = StrPool.DOUBLE_DOT;
|
||||
|
||||
@@ -136,8 +135,7 @@ public class StringConstants {
|
||||
public static final String BACKSLASH = StrPool.BACKSLASH;
|
||||
|
||||
/**
|
||||
* 字符串常量:回车符 {@code "\r"} <br>
|
||||
* 解释:该字符常用于表示 Linux 系统和 MacOS 系统下的文本换行
|
||||
* 字符串常量:回车符 {@code "\r"} <br> 解释:该字符常用于表示 Linux 系统和 MacOS 系统下的文本换行
|
||||
*/
|
||||
public static final String CR = StrPool.CR;
|
||||
|
||||
@@ -147,8 +145,7 @@ public class StringConstants {
|
||||
public static final String LF = StrPool.LF;
|
||||
|
||||
/**
|
||||
* 字符串常量:Windows 换行 {@code "\r\n"} <br>
|
||||
* 解释:该字符串常用于表示 Windows 系统下的文本换行
|
||||
* 字符串常量:Windows 换行 {@code "\r\n"} <br> 解释:该字符串常用于表示 Windows 系统下的文本换行
|
||||
*/
|
||||
public static final String CRLF = StrPool.CRLF;
|
||||
|
||||
@@ -272,6 +269,11 @@ public class StringConstants {
|
||||
*/
|
||||
public static final String ROUND_BRACKET_END = ")";
|
||||
|
||||
/**
|
||||
* 等号(=)
|
||||
*/
|
||||
public static final String EQUALS = "=";
|
||||
|
||||
/**
|
||||
* 路径模式
|
||||
*/
|
||||
|
||||
@@ -18,5 +18,10 @@
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-db</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -27,12 +27,4 @@ import java.lang.annotation.*;
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface QueryIgnore {
|
||||
|
||||
/**
|
||||
* 获取是否忽略查询解析
|
||||
*
|
||||
* @return 是否忽略查询解析
|
||||
*/
|
||||
boolean value() default true;
|
||||
}
|
||||
public @interface QueryIgnore {}
|
||||
|
||||
@@ -18,6 +18,8 @@ package top.continew.starter.data.core.enums;
|
||||
|
||||
import top.continew.starter.data.core.function.ISqlFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据库类型枚举
|
||||
*
|
||||
@@ -31,8 +33,8 @@ public enum DatabaseType implements ISqlFunction {
|
||||
*/
|
||||
MYSQL("MySQL") {
|
||||
@Override
|
||||
public String findInSet() {
|
||||
return "find_in_set({0}, {1}) <> 0";
|
||||
public String findInSet(Serializable value, String set) {
|
||||
return "find_in_set('%s', %s) <> 0".formatted(value, set);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -41,8 +43,8 @@ public enum DatabaseType implements ISqlFunction {
|
||||
*/
|
||||
POSTGRE_SQL("PostgreSQL") {
|
||||
@Override
|
||||
public String findInSet() {
|
||||
return "(select position(',{0},' in ','||{1}||',')) <> 0";
|
||||
public String findInSet(Serializable value, String set) {
|
||||
return "(select position(',%s,' in ','||%s||',')) <> 0".formatted(value, set);
|
||||
}
|
||||
},;
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package top.continew.starter.data.core.function;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* SQL 函数接口
|
||||
*
|
||||
@@ -27,7 +29,9 @@ public interface ISqlFunction {
|
||||
/**
|
||||
* find_in_set 函数
|
||||
*
|
||||
* @param value 值
|
||||
* @param set 集合
|
||||
* @return 函数实现
|
||||
*/
|
||||
String findInSet();
|
||||
String findInSet(Serializable value, String set);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.core.util;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* SQL 注入验证工具类
|
||||
*
|
||||
* @author hubin(<a href="https://github.com/baomidou/mybatis-plus">MyBatis Plus</a>)
|
||||
* @author zhoujf(<a href="https://github.com/jeecgboot/JeecgBoot">JeecgBoot</a>)
|
||||
* @author Charles7c
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public class SqlInjectionUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SqlInjectionUtils.class);
|
||||
|
||||
/**
|
||||
* SQL语法检查正则:符合两个关键字(有先后顺序)才算匹配
|
||||
*/
|
||||
private static final Pattern SQL_SYNTAX_PATTERN = Pattern
|
||||
.compile("(insert|delete|update|select|create|drop|truncate|grant|alter|deny|revoke|call|execute|exec|declare|show|rename|set)" + "\\s+.*(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)|(select\\s*\\*\\s*from\\s+)|(and|or)\\s+.*", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* 使用'、;或注释截断SQL检查正则
|
||||
*/
|
||||
private static final Pattern SQL_COMMENT_PATTERN = Pattern
|
||||
.compile("'.*(or|union|--|#|/\\*|;)", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* SQL 语法关键字
|
||||
*/
|
||||
private static final String SQL_SYNTAX_KEYWORD = "and |exec |peformance_schema|information_schema|extractvalue|updatexml|geohash|gtid_subset|gtid_subtract|insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |;|or |+|--";
|
||||
|
||||
/**
|
||||
* SQL 函数检查正则
|
||||
*/
|
||||
private static final String[] SQL_FUNCTION_PATTERN = new String[] {"chr\\s*\\(", "mid\\s*\\(", " char\\s*\\(",
|
||||
"sleep\\s*\\(", "user\\s*\\(", "show\\s+tables", "user[\\s]*\\([\\s]*\\)", "show\\s+databases",
|
||||
"sleep\\(\\d*\\)", "sleep\\(.*\\)",};
|
||||
|
||||
private static final String MESSAGE_TEMPLATE = "SQL 注入检查: 检查值=>{}<=存在 SQL 注入关键字, 关键字=>{}<=";
|
||||
|
||||
private SqlInjectionUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查参数是否存在 SQL 注入
|
||||
*
|
||||
* @param value 检查参数
|
||||
* @return true:非法;false:合法
|
||||
*/
|
||||
public static boolean check(String value) {
|
||||
return check(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查参数是否存在 SQL 注入
|
||||
*
|
||||
* @param value 检查参数
|
||||
* @param customKeyword 自定义关键字
|
||||
* @return true:非法;false:合法
|
||||
*/
|
||||
public static boolean check(String value, String customKeyword) {
|
||||
if (CharSequenceUtil.isBlank(value)) {
|
||||
return false;
|
||||
}
|
||||
// 处理是否包含 SQL 注释字符 || 检查是否包含 SQL 注入敏感字符
|
||||
if (SQL_COMMENT_PATTERN.matcher(value).find() || SQL_SYNTAX_PATTERN.matcher(value).find()) {
|
||||
log.warn("SQL 注入检查: 检查值=>{}<=存在 SQL 注释字符或 SQL 注入敏感字符", value);
|
||||
return true;
|
||||
}
|
||||
// 转换成小写再进行比较
|
||||
value = value.toLowerCase().trim();
|
||||
// 检查是否包含 SQL 语法关键字
|
||||
if (checkKeyword(value, SQL_SYNTAX_KEYWORD.split("\\|"))) {
|
||||
return true;
|
||||
}
|
||||
// 检查是否包含自定义关键字
|
||||
if (CharSequenceUtil.isNotBlank(customKeyword) && checkKeyword(value, customKeyword.split("\\|"))) {
|
||||
return true;
|
||||
}
|
||||
// 检查是否包含 SQL 注入敏感字符
|
||||
for (String pattern : SQL_FUNCTION_PATTERN) {
|
||||
if (Pattern.matches(".*" + pattern + ".*", value)) {
|
||||
log.warn(MESSAGE_TEMPLATE, value, pattern);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查参数是否存在关键字
|
||||
*
|
||||
* @param value 检查参数
|
||||
* @param keywords 关键字列表
|
||||
* @return true:非法;false:合法
|
||||
*/
|
||||
private static boolean checkKeyword(String value, String[] keywords) {
|
||||
for (String keyword : keywords) {
|
||||
if (value.contains(keyword)) {
|
||||
log.warn(MESSAGE_TEMPLATE, value, keyword);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-data-mybatis-flex</artifactId>
|
||||
<artifactId>continew-starter-data-mf</artifactId>
|
||||
<description>ContiNew Starter 数据访问模块 - MyBatis Flex</description>
|
||||
|
||||
<dependencies>
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.autoconfigure;
|
||||
package top.continew.starter.data.mf.autoconfigure;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.autoconfigure;
|
||||
package top.continew.starter.data.mf.autoconfigure;
|
||||
|
||||
import com.mybatisflex.core.dialect.DbType;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.autoconfigure;
|
||||
package top.continew.starter.data.mf.autoconfigure;
|
||||
|
||||
import com.mybatisflex.core.dialect.DbType;
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
@@ -30,8 +30,8 @@ import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
import top.continew.starter.data.mybatis.flex.datapermission.DataPermissionDialect;
|
||||
import top.continew.starter.data.mybatis.flex.datapermission.DataPermissionFilter;
|
||||
import top.continew.starter.data.mf.datapermission.DataPermissionDialect;
|
||||
import top.continew.starter.data.mf.datapermission.DataPermissionFilter;
|
||||
|
||||
/**
|
||||
* MyBatis Flex 自动配置
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.base;
|
||||
package top.continew.starter.data.mf.base;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.base;
|
||||
package top.continew.starter.data.mf.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -14,9 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -31,16 +29,9 @@ import java.lang.annotation.*;
|
||||
@Documented
|
||||
public @interface DataPermission {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #tableAlias()} attribute.
|
||||
*/
|
||||
@AliasFor("tableAlias")
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 表别名
|
||||
*/
|
||||
@AliasFor("value")
|
||||
String tableAlias() default "";
|
||||
|
||||
/**
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
import org.aspectj.lang.annotation.*;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
/**
|
||||
* 数据权限过滤器接口
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.datapermission;
|
||||
package top.continew.starter.data.mf.datapermission;
|
||||
|
||||
/**
|
||||
* 数据权限枚举
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.service;
|
||||
package top.continew.starter.data.mf.service;
|
||||
|
||||
/**
|
||||
* 通用业务接口
|
||||
@@ -23,5 +23,4 @@ package top.continew.starter.data.mybatis.flex.service;
|
||||
* @author hellokaton
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface IService<T> extends com.mybatisflex.core.service.IService<T> {
|
||||
}
|
||||
public interface IService<T> extends com.mybatisflex.core.service.IService<T> {}
|
||||
@@ -14,11 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.service.impl;
|
||||
package top.continew.starter.data.mf.service.impl;
|
||||
|
||||
import top.continew.starter.core.util.ClassUtils;
|
||||
import top.continew.starter.data.mybatis.flex.base.BaseMapper;
|
||||
import top.continew.starter.data.mybatis.flex.service.IService;
|
||||
import top.continew.starter.data.mf.base.BaseMapper;
|
||||
import top.continew.starter.data.mf.service.IService;
|
||||
|
||||
/**
|
||||
* 通用业务实现类
|
||||
@@ -14,21 +14,23 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.flex.query;
|
||||
package top.continew.starter.data.mf.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import top.continew.starter.core.exception.BadRequestException;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.continew.starter.data.core.annotation.Query;
|
||||
import top.continew.starter.data.core.annotation.QueryIgnore;
|
||||
import top.continew.starter.data.core.enums.QueryType;
|
||||
import top.continew.starter.data.core.util.SqlInjectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
@@ -56,10 +58,9 @@ public class QueryWrapperHelper {
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param <Q> 查询条件数据类型
|
||||
* @param <R> 查询数据类型
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
public static <Q, R> QueryWrapper build(Q query) {
|
||||
public static <Q> QueryWrapper build(Q query) {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create();
|
||||
// 没有查询条件,直接返回
|
||||
if (null == query) {
|
||||
@@ -70,6 +71,34 @@ public class QueryWrapperHelper {
|
||||
return build(query, fieldList, queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 QueryWrapper
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param sort 排序条件
|
||||
* @param <Q> 查询条件数据类型
|
||||
* @return QueryWrapper
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public static <Q> QueryWrapper build(Q query, Sort sort) {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create();
|
||||
// 没有查询条件,直接返回
|
||||
if (null == query) {
|
||||
return queryWrapper;
|
||||
}
|
||||
// 设置排序条件
|
||||
if (sort != null && sort.isSorted()) {
|
||||
for (Sort.Order order : sort) {
|
||||
String field = CharSequenceUtil.toUnderlineCase(order.getProperty());
|
||||
ValidationUtils.throwIf(SqlInjectionUtils.check(field), "排序字段包含非法字符");
|
||||
queryWrapper.orderBy(field, order.isAscending());
|
||||
}
|
||||
}
|
||||
// 获取查询条件中所有的字段
|
||||
List<Field> fieldList = ReflectUtils.getNonStaticFields(query.getClass());
|
||||
return build(query, fieldList, queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 QueryWrapper
|
||||
*
|
||||
@@ -114,7 +143,7 @@ public class QueryWrapperHelper {
|
||||
}
|
||||
// 设置了 @QueryIgnore 注解,直接忽略
|
||||
QueryIgnore queryIgnoreAnnotation = field.getAnnotation(QueryIgnore.class);
|
||||
if (null != queryIgnoreAnnotation && queryIgnoreAnnotation.value()) {
|
||||
if (null != queryIgnoreAnnotation) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名
|
||||
@@ -0,0 +1 @@
|
||||
top.continew.starter.data.mf.autoconfigure.MybatisFlexAutoConfiguration
|
||||
@@ -9,7 +9,7 @@
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-data-mybatis-plus</artifactId>
|
||||
<artifactId>continew-starter-data-mp</artifactId>
|
||||
<description>ContiNew Starter 数据访问模块 - MyBatis Plus</description>
|
||||
|
||||
<dependencies>
|
||||
@@ -19,12 +19,6 @@
|
||||
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dynamic Datasource(基于 Spring Boot 的快速集成多数据源的启动器) -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- P6Spy(SQL 性能分析组件) -->
|
||||
<dependency>
|
||||
<groupId>p6spy</groupId>
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure;
|
||||
package top.continew.starter.data.mp.autoconfigure;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
import top.continew.starter.data.mybatis.plus.autoconfigure.idgenerator.MyBatisPlusIdGeneratorProperties;
|
||||
import top.continew.starter.data.mp.autoconfigure.idgenerator.MyBatisPlusIdGeneratorProperties;
|
||||
|
||||
/**
|
||||
* MyBatis Plus 扩展配置属性
|
||||
@@ -49,40 +49,21 @@ public class MyBatisPlusExtensionProperties {
|
||||
@NestedConfigurationProperty
|
||||
private MyBatisPlusIdGeneratorProperties idGenerator;
|
||||
|
||||
/**
|
||||
* 数据权限插件配置
|
||||
*/
|
||||
private DataPermissionProperties dataPermission;
|
||||
|
||||
/**
|
||||
* 分页插件配置
|
||||
*/
|
||||
private PaginationProperties pagination;
|
||||
|
||||
/**
|
||||
* 启用乐观锁插件
|
||||
*/
|
||||
private boolean optimisticLockerEnabled = false;
|
||||
|
||||
/**
|
||||
* 启用防全表更新与删除插件
|
||||
*/
|
||||
private boolean blockAttackPluginEnabled = true;
|
||||
|
||||
/**
|
||||
* 数据权限插件配置属性
|
||||
*/
|
||||
public static class DataPermissionProperties {
|
||||
|
||||
/**
|
||||
* 是否启用数据权限插件
|
||||
*/
|
||||
private boolean enabled = false;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页插件配置属性
|
||||
*/
|
||||
@@ -165,14 +146,6 @@ public class MyBatisPlusExtensionProperties {
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
public DataPermissionProperties getDataPermission() {
|
||||
return dataPermission;
|
||||
}
|
||||
|
||||
public void setDataPermission(DataPermissionProperties dataPermission) {
|
||||
this.dataPermission = dataPermission;
|
||||
}
|
||||
|
||||
public PaginationProperties getPagination() {
|
||||
return pagination;
|
||||
}
|
||||
@@ -181,6 +154,14 @@ public class MyBatisPlusExtensionProperties {
|
||||
this.pagination = pagination;
|
||||
}
|
||||
|
||||
public boolean isOptimisticLockerEnabled() {
|
||||
return optimisticLockerEnabled;
|
||||
}
|
||||
|
||||
public void setOptimisticLockerEnabled(boolean optimisticLockerEnabled) {
|
||||
this.optimisticLockerEnabled = optimisticLockerEnabled;
|
||||
}
|
||||
|
||||
public boolean isBlockAttackPluginEnabled() {
|
||||
return blockAttackPluginEnabled;
|
||||
}
|
||||
@@ -14,15 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure;
|
||||
package top.continew.starter.data.mp.autoconfigure;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.*;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.slf4j.Logger;
|
||||
@@ -38,10 +35,10 @@ import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
import top.continew.starter.data.mybatis.plus.autoconfigure.idgenerator.MyBatisPlusIdGeneratorConfiguration;
|
||||
import top.continew.starter.data.mybatis.plus.datapermission.DataPermissionFilter;
|
||||
import top.continew.starter.data.mybatis.plus.datapermission.DataPermissionHandlerImpl;
|
||||
import top.continew.starter.data.mybatis.plus.handler.MybatisBaseEnumTypeHandler;
|
||||
import top.continew.starter.data.mp.autoconfigure.idgenerator.MyBatisPlusIdGeneratorConfiguration;
|
||||
import top.continew.starter.data.mp.handler.MybatisBaseEnumTypeHandler;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* MyBatis Plus 自动配置
|
||||
@@ -76,18 +73,20 @@ public class MybatisPlusAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 数据权限插件
|
||||
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties
|
||||
.getDataPermission();
|
||||
if (null != dataPermissionProperties && dataPermissionProperties.isEnabled()) {
|
||||
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil
|
||||
.getBean(DataPermissionHandler.class)));
|
||||
// 其他拦截器
|
||||
Map<String, InnerInterceptor> innerInterceptors = SpringUtil.getBeansOfType(InnerInterceptor.class);
|
||||
if (!innerInterceptors.isEmpty()) {
|
||||
innerInterceptors.values().forEach(interceptor::addInnerInterceptor);
|
||||
}
|
||||
// 分页插件
|
||||
MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination();
|
||||
if (null != paginationProperties && paginationProperties.isEnabled()) {
|
||||
interceptor.addInnerInterceptor(this.paginationInnerInterceptor(paginationProperties));
|
||||
}
|
||||
// 乐观锁插件
|
||||
if (properties.isOptimisticLockerEnabled()) {
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
}
|
||||
// 防全表更新与删除插件
|
||||
if (properties.isBlockAttackPluginEnabled()) {
|
||||
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
|
||||
@@ -103,16 +102,6 @@ public class MybatisPlusAutoConfiguration {
|
||||
MyBatisPlusIdGeneratorConfiguration.Custom.class})
|
||||
protected static class MyBatisPlusIdGeneratorAutoConfiguration {}
|
||||
|
||||
/**
|
||||
* 数据权限处理器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledDataPermission
|
||||
public DataPermissionHandler dataPermissionHandler(DataPermissionFilter dataPermissionFilter) {
|
||||
return new DataPermissionHandlerImpl(dataPermissionFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页插件配置(<a href="https://baomidou.com/pages/97710a/#paginationinnerinterceptor">PaginationInnerInterceptor</a>)
|
||||
*/
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure.idgenerator;
|
||||
package top.continew.starter.data.mp.autoconfigure.idgenerator;
|
||||
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import me.ahoo.cosid.snowflake.SnowflakeId;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure.idgenerator;
|
||||
package top.continew.starter.data.mp.autoconfigure.idgenerator;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
||||
@@ -14,9 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure.idgenerator;
|
||||
package top.continew.starter.data.mp.autoconfigure.idgenerator;
|
||||
|
||||
import top.continew.starter.data.mybatis.plus.enums.MyBatisPlusIdGeneratorType;
|
||||
import top.continew.starter.data.mp.enums.MyBatisPlusIdGeneratorType;
|
||||
|
||||
/**
|
||||
* MyBatis ID 生成器配置属性
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.base;
|
||||
package top.continew.starter.data.mp.base;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.enums;
|
||||
package top.continew.starter.data.mp.enums;
|
||||
|
||||
/**
|
||||
* MyBatis ID 生成器类型枚举
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.handler;
|
||||
package top.continew.starter.data.mp.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.baomidou.mybatisplus.annotation.IEnum;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.service;
|
||||
package top.continew.starter.data.mp.service;
|
||||
|
||||
/**
|
||||
* 通用业务接口
|
||||
@@ -23,5 +23,4 @@ package top.continew.starter.data.mybatis.plus.service;
|
||||
* @author Charles7c
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface IService<T> extends com.baomidou.mybatisplus.extension.service.IService<T> {
|
||||
}
|
||||
public interface IService<T> extends com.baomidou.mybatisplus.extension.service.IService<T> {}
|
||||
@@ -14,13 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.service.impl;
|
||||
package top.continew.starter.data.mp.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.util.validate.CheckUtils;
|
||||
import top.continew.starter.data.mybatis.plus.base.BaseMapper;
|
||||
import top.continew.starter.data.mybatis.plus.service.IService;
|
||||
import top.continew.starter.data.mp.base.BaseMapper;
|
||||
import top.continew.starter.data.mp.service.IService;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -14,21 +14,23 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.query;
|
||||
package top.continew.starter.data.mp.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import top.continew.starter.core.exception.BadRequestException;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.continew.starter.data.core.annotation.Query;
|
||||
import top.continew.starter.data.core.annotation.QueryIgnore;
|
||||
import top.continew.starter.data.core.enums.QueryType;
|
||||
import top.continew.starter.data.core.util.SqlInjectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
@@ -60,11 +62,33 @@ public class QueryWrapperHelper {
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
public static <Q, R> QueryWrapper<R> build(Q query) {
|
||||
return build(query, Sort.unsorted());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 QueryWrapper
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param sort 排序条件
|
||||
* @param <Q> 查询条件数据类型
|
||||
* @param <R> 查询数据类型
|
||||
* @return QueryWrapper
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public static <Q, R> QueryWrapper<R> build(Q query, Sort sort) {
|
||||
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
|
||||
// 没有查询条件,直接返回
|
||||
if (null == query) {
|
||||
return queryWrapper;
|
||||
}
|
||||
// 设置排序条件
|
||||
if (sort != null && sort.isSorted()) {
|
||||
for (Sort.Order order : sort) {
|
||||
String field = CharSequenceUtil.toUnderlineCase(order.getProperty());
|
||||
ValidationUtils.throwIf(SqlInjectionUtils.check(field), "排序字段包含非法字符");
|
||||
queryWrapper.orderBy(true, order.isAscending(), field);
|
||||
}
|
||||
}
|
||||
// 获取查询条件中所有的字段
|
||||
List<Field> fieldList = ReflectUtils.getNonStaticFields(query.getClass());
|
||||
return build(query, fieldList, queryWrapper);
|
||||
@@ -113,7 +137,7 @@ public class QueryWrapperHelper {
|
||||
}
|
||||
// 设置了 @QueryIgnore 注解,直接忽略
|
||||
QueryIgnore queryIgnoreAnnotation = field.getAnnotation(QueryIgnore.class);
|
||||
if (null != queryIgnoreAnnotation && queryIgnoreAnnotation.value()) {
|
||||
if (null != queryIgnoreAnnotation) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名
|
||||
@@ -0,0 +1 @@
|
||||
top.continew.starter.data.mp.autoconfigure.MybatisPlusAutoConfiguration
|
||||
@@ -1 +0,0 @@
|
||||
top.continew.starter.data.mybatis.flex.autoconfigure.MybatisFlexAutoConfiguration
|
||||
@@ -1 +0,0 @@
|
||||
top.continew.starter.data.mybatis.plus.autoconfigure.MybatisPlusAutoConfiguration
|
||||
@@ -15,8 +15,8 @@
|
||||
|
||||
<modules>
|
||||
<module>continew-starter-data-core</module>
|
||||
<module>continew-starter-data-mybatis-plus</module>
|
||||
<module>continew-starter-data-mybatis-flex</module>
|
||||
<module>continew-starter-data-mp</module>
|
||||
<module>continew-starter-data-mf</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>3.2.7</version>
|
||||
<version>3.2.10</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@@ -43,33 +43,33 @@
|
||||
|
||||
<properties>
|
||||
<!-- 项目版本号 -->
|
||||
<revision>2.5.1</revision>
|
||||
<snail-job.version>1.1.0</snail-job.version>
|
||||
<sa-token.version>1.38.0</sa-token.version>
|
||||
<revision>2.7.0</revision>
|
||||
<snail-job.version>1.1.2</snail-job.version>
|
||||
<sa-token.version>1.39.0</sa-token.version>
|
||||
<just-auth.version>1.16.6</just-auth.version>
|
||||
<mybatis-plus.version>3.5.7</mybatis-plus.version>
|
||||
<mybatis-flex.version>1.9.3</mybatis-flex.version>
|
||||
<mybatis-plus.version>3.5.8</mybatis-plus.version>
|
||||
<mybatis-flex.version>1.9.7</mybatis-flex.version>
|
||||
<dynamic-datasource.version>4.3.1</dynamic-datasource.version>
|
||||
<p6spy.version>3.9.1</p6spy.version>
|
||||
<jetcache.version>2.7.6</jetcache.version>
|
||||
<redisson.version>3.32.0</redisson.version>
|
||||
<cosid.version>2.9.1</cosid.version>
|
||||
<sms4j.version>3.2.1</sms4j.version>
|
||||
<redisson.version>3.36.0</redisson.version>
|
||||
<cosid.version>2.9.8</cosid.version>
|
||||
<sms4j.version>3.3.3</sms4j.version>
|
||||
<aj-captcha.version>1.3.0</aj-captcha.version>
|
||||
<easy-captcha.version>1.6.2</easy-captcha.version>
|
||||
<easy-excel.version>3.3.4</easy-excel.version>
|
||||
<nashorn.version>15.4</nashorn.version>
|
||||
<x-file-storage.version>2.2.0</x-file-storage.version>
|
||||
<aws-s3.version>1.12.761</aws-s3.version>
|
||||
<graceful-response.version>4.0.1-boot3</graceful-response.version>
|
||||
<x-file-storage.version>2.2.1</x-file-storage.version>
|
||||
<aws-s3.version>1.12.771</aws-s3.version>
|
||||
<graceful-response.version>5.0.0-boot3</graceful-response.version>
|
||||
<crane4j.version>2.9.0</crane4j.version>
|
||||
<knife4j.version>4.5.0</knife4j.version>
|
||||
<tlog.version>1.5.2</tlog.version>
|
||||
<snakeyaml.version>2.2</snakeyaml.version>
|
||||
<snakeyaml.version>2.3</snakeyaml.version>
|
||||
<okhttp.version>4.12.0</okhttp.version>
|
||||
<ttl.version>2.14.5</ttl.version>
|
||||
<ip2region.version>3.2.6</ip2region.version>
|
||||
<hutool.version>5.8.29</hutool.version>
|
||||
<hutool.version>5.8.32</hutool.version>
|
||||
<!-- Maven Plugin Versions -->
|
||||
<flatten.version>1.6.0</flatten.version>
|
||||
<spotless.version>2.43.0</spotless.version>
|
||||
@@ -137,6 +137,11 @@
|
||||
<artifactId>mybatis-plus-core</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Flex(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
|
||||
<dependency>
|
||||
@@ -341,14 +346,12 @@
|
||||
<artifactId>continew-starter-extension-crud-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 扩展模块 - CRUD - MyBatis Plus ORM 模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-crud-mp</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 扩展模块 - CRUD - MyBatis Flex ORM 模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
@@ -356,6 +359,32 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 扩展模块 - 数据权限 - 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-datapermission-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<!-- 扩展模块 - 数据权限 - MyBatis Plus ORM 模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-datapermission-mp</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 扩展模块 - 多租户 - 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-tenant-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<!-- 扩展模块 - 多租户 - MyBatis Plus ORM 模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-tenant-mp</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 认证模块 - JustAuth -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
@@ -373,14 +402,14 @@
|
||||
<!-- 数据访问模块 - MyBatis Plus -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-data-mybatis-plus</artifactId>
|
||||
<artifactId>continew-starter-data-mp</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 数据访问模块 - MyBatis Flex -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-data-mybatis-flex</artifactId>
|
||||
<artifactId>continew-starter-data-mf</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 认证模块 - SaToken -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
@@ -36,16 +42,22 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- 数据访问模块 - 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-data-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 文件处理模块 - Excel -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-file-excel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 模块 -->
|
||||
<!-- API 文档模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-web</artifactId>
|
||||
<artifactId>continew-starter-api-doc</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -31,5 +31,4 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import({CrudRestControllerAutoConfiguration.class})
|
||||
public @interface EnableCrudRestController {
|
||||
}
|
||||
public @interface EnableCrudRestController {}
|
||||
|
||||
@@ -21,6 +21,8 @@ import cn.hutool.core.util.ArrayUtil;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.continew.starter.data.core.util.SqlInjectionUtils;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
@@ -54,20 +56,17 @@ public class SortQuery implements Serializable {
|
||||
if (ArrayUtil.isEmpty(sort)) {
|
||||
return Sort.unsorted();
|
||||
}
|
||||
|
||||
ValidationUtils.throwIf(sort.length < 2, "排序条件非法");
|
||||
List<Sort.Order> orders = new ArrayList<>(sort.length);
|
||||
if (CharSequenceUtil.contains(sort[0], StringConstants.COMMA)) {
|
||||
// e.g "sort=createTime,desc&sort=name,asc"
|
||||
for (String s : sort) {
|
||||
List<String> sortList = CharSequenceUtil.splitTrim(s, StringConstants.COMMA);
|
||||
Sort.Order order = new Sort.Order(Sort.Direction.valueOf(sortList.get(1).toUpperCase()), sortList
|
||||
.get(0));
|
||||
orders.add(order);
|
||||
orders.add(this.getOrder(sortList.get(0), sortList.get(1)));
|
||||
}
|
||||
} else {
|
||||
// e.g "sort=createTime,desc"
|
||||
Sort.Order order = new Sort.Order(Sort.Direction.valueOf(sort[1].toUpperCase()), sort[0]);
|
||||
orders.add(order);
|
||||
orders.add(this.getOrder(sort[0], sort[1]));
|
||||
}
|
||||
return Sort.by(orders);
|
||||
}
|
||||
@@ -75,4 +74,16 @@ public class SortQuery implements Serializable {
|
||||
public void setSort(String[] sort) {
|
||||
this.sort = sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排序条件
|
||||
*
|
||||
* @param field 字段
|
||||
* @param direction 排序方向
|
||||
* @return 排序条件
|
||||
*/
|
||||
private Sort.Order getOrder(String field, String direction) {
|
||||
ValidationUtils.throwIf(SqlInjectionUtils.check(field), "排序字段包含非法字符");
|
||||
return new Sort.Order(Sort.Direction.valueOf(direction.toUpperCase()), field);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.crud.model.resp;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页信息
|
||||
*
|
||||
* @param <T> 列表数据类型
|
||||
* @author Charles7c
|
||||
* @since 2.5.2
|
||||
*/
|
||||
@Schema(description = "分页信息")
|
||||
public class BasePageResp<T> implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
@Schema(description = "列表数据")
|
||||
private List<T> list;
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
@Schema(description = "总记录数", example = "10")
|
||||
private long total;
|
||||
|
||||
public BasePageResp() {
|
||||
}
|
||||
|
||||
public BasePageResp(final List<T> list, final long total) {
|
||||
this.list = list;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public List<T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(long total) {
|
||||
this.total = total;
|
||||
}
|
||||
}
|
||||
@@ -33,13 +33,11 @@ public interface ValidateGroup extends Default {
|
||||
/**
|
||||
* 分组校验-创建
|
||||
*/
|
||||
interface Add extends Crud {
|
||||
}
|
||||
interface Add extends Crud {}
|
||||
|
||||
/**
|
||||
* 分组校验-修改
|
||||
*/
|
||||
interface Update extends Crud {
|
||||
}
|
||||
interface Update extends Crud {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<!-- 数据访问模块 - MyBatis Flex -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-data-mybatis-flex</artifactId>
|
||||
<artifactId>continew-starter-data-mf</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -22,7 +22,6 @@ import com.mybatisflex.core.paginate.Page;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -35,22 +34,17 @@ import java.util.List;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Schema(description = "分页信息")
|
||||
public class PageResp<L> implements Serializable {
|
||||
public class PageResp<L> extends BasePageResp<L> {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
@Schema(description = "列表数据")
|
||||
private List<L> list;
|
||||
public PageResp() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
@Schema(description = "总记录数", example = "10")
|
||||
private long total;
|
||||
public PageResp(final List<L> list, final long total) {
|
||||
super(list, total);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于 MyBatis Plus 分页数据构建分页信息,并将源数据转换为指定类型数据
|
||||
@@ -65,10 +59,7 @@ public class PageResp<L> implements Serializable {
|
||||
if (null == page) {
|
||||
return empty();
|
||||
}
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(BeanUtil.copyToList(page.getRecords(), targetClass));
|
||||
pageResp.setTotal(page.getTotalRow());
|
||||
return pageResp;
|
||||
return new PageResp<>(BeanUtil.copyToList(page.getRecords(), targetClass), page.getTotalRow());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,10 +73,7 @@ public class PageResp<L> implements Serializable {
|
||||
if (null == page) {
|
||||
return empty();
|
||||
}
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(page.getRecords());
|
||||
pageResp.setTotal(page.getTotalRow());
|
||||
return pageResp;
|
||||
return new PageResp<>(page.getRecords(), page.getTotalRow());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,24 +111,6 @@ public class PageResp<L> implements Serializable {
|
||||
* @return 分页信息
|
||||
*/
|
||||
private static <L> PageResp<L> empty() {
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(Collections.emptyList());
|
||||
return pageResp;
|
||||
}
|
||||
|
||||
public List<L> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<L> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(long total) {
|
||||
this.total = total;
|
||||
return new PageResp<>(Collections.emptyList(), 0L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ package top.continew.starter.extension.crud.service;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import cn.crane4j.core.support.OperateTemplate;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
@@ -34,16 +33,16 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.continew.starter.data.mybatis.flex.base.BaseMapper;
|
||||
import top.continew.starter.data.mybatis.flex.query.QueryWrapperHelper;
|
||||
import top.continew.starter.data.mybatis.flex.service.impl.ServiceImpl;
|
||||
import top.continew.starter.data.mf.base.BaseMapper;
|
||||
import top.continew.starter.data.mf.service.impl.ServiceImpl;
|
||||
import top.continew.starter.data.mf.util.QueryWrapperHelper;
|
||||
import top.continew.starter.extension.crud.annotation.TreeField;
|
||||
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
import top.continew.starter.extension.crud.service.BaseService;
|
||||
import top.continew.starter.extension.crud.util.TreeUtils;
|
||||
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
|
||||
import top.continew.starter.file.excel.util.ExcelUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -73,6 +72,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
|
||||
@Override
|
||||
public PageResp<L> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper queryWrapper = this.buildQueryWrapper(query);
|
||||
this.sort(queryWrapper, pageQuery);
|
||||
Page<T> page = mapper.paginate(pageQuery.getPage(), pageQuery.getSize(), queryWrapper);
|
||||
PageResp<L> pageResp = PageResp.build(page, listClass);
|
||||
pageResp.getList().forEach(this::fill);
|
||||
@@ -185,12 +185,12 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
|
||||
* @param sortQuery 排序查询条件
|
||||
*/
|
||||
protected void sort(QueryWrapper queryWrapper, SortQuery sortQuery) {
|
||||
Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort();
|
||||
if (sortQuery == null || sortQuery.getSort().isUnsorted()) {
|
||||
return;
|
||||
}
|
||||
Sort sort = sortQuery.getSort();
|
||||
List<Field> entityFields = ReflectUtils.getNonStaticFields(this.entityClass);
|
||||
for (Sort.Order order : sort) {
|
||||
if (null == order) {
|
||||
continue;
|
||||
}
|
||||
String property = order.getProperty();
|
||||
String checkProperty;
|
||||
// 携带表别名则获取 . 后面的字段名
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<!-- 数据访问模块 - MyBatis Plus -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-data-mybatis-plus</artifactId>
|
||||
<artifactId>continew-starter-data-mp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -30,13 +30,13 @@ import org.springframework.web.bind.annotation.*;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
|
||||
import top.continew.starter.extension.crud.enums.Api;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.req.BaseReq;
|
||||
import top.continew.starter.extension.crud.model.resp.BaseIdResp;
|
||||
import top.continew.starter.extension.crud.util.ValidateGroup;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
import top.continew.starter.extension.crud.service.BaseService;
|
||||
import top.continew.starter.extension.crud.util.ValidateGroup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.crud.model.query;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
import org.springdoc.core.annotations.ParameterObject;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/**
|
||||
* 分页查询条件
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ParameterObject
|
||||
@Schema(description = "分页查询条件")
|
||||
public class PageQuery extends SortQuery {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 默认页码:1
|
||||
*/
|
||||
private static final int DEFAULT_PAGE = 1;
|
||||
/**
|
||||
* 默认每页条数:10
|
||||
*/
|
||||
private static final int DEFAULT_SIZE = 10;
|
||||
|
||||
/**
|
||||
* 页码
|
||||
*/
|
||||
@Schema(description = "页码", example = "1")
|
||||
@Min(value = 1, message = "页码最小值为 {value}")
|
||||
private Integer page = DEFAULT_PAGE;
|
||||
|
||||
/**
|
||||
* 每页条数
|
||||
*/
|
||||
@Schema(description = "每页条数", example = "10")
|
||||
@Range(min = 1, max = 1000, message = "每页条数(取值范围 {min}-{max})")
|
||||
private Integer size = DEFAULT_SIZE;
|
||||
|
||||
/**
|
||||
* 基于分页查询条件转换为 MyBatis Plus 分页条件
|
||||
*
|
||||
* @param <T> 列表数据类型
|
||||
* @return MyBatis Plus 分页条件
|
||||
*/
|
||||
public <T> IPage<T> toPage() {
|
||||
Page<T> mybatisPage = new Page<>(this.getPage(), this.getSize());
|
||||
Sort pageSort = this.getSort();
|
||||
if (CollUtil.isNotEmpty(pageSort)) {
|
||||
for (Sort.Order order : pageSort) {
|
||||
OrderItem orderItem = new OrderItem();
|
||||
orderItem.setAsc(order.isAscending());
|
||||
orderItem.setColumn(CharSequenceUtil.toUnderlineCase(order.getProperty()));
|
||||
mybatisPage.addOrder(orderItem);
|
||||
}
|
||||
}
|
||||
return mybatisPage;
|
||||
}
|
||||
|
||||
public Integer getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public void setPage(Integer page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -34,22 +34,17 @@ import java.util.List;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Schema(description = "分页信息")
|
||||
public class PageResp<L> implements Serializable {
|
||||
public class PageResp<L> extends BasePageResp<L> {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
@Schema(description = "列表数据")
|
||||
private List<L> list;
|
||||
public PageResp() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
@Schema(description = "总记录数", example = "10")
|
||||
private long total;
|
||||
public PageResp(final List<L> list, final long total) {
|
||||
super(list, total);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于 MyBatis Plus 分页数据构建分页信息,并将源数据转换为指定类型数据
|
||||
@@ -64,10 +59,7 @@ public class PageResp<L> implements Serializable {
|
||||
if (null == page) {
|
||||
return empty();
|
||||
}
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(BeanUtil.copyToList(page.getRecords(), targetClass));
|
||||
pageResp.setTotal(page.getTotal());
|
||||
return pageResp;
|
||||
return new PageResp<>(BeanUtil.copyToList(page.getRecords(), targetClass), page.getTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,10 +73,7 @@ public class PageResp<L> implements Serializable {
|
||||
if (null == page) {
|
||||
return empty();
|
||||
}
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(page.getRecords());
|
||||
pageResp.setTotal(page.getTotal());
|
||||
return pageResp;
|
||||
return new PageResp<>(page.getRecords(), page.getTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,24 +111,6 @@ public class PageResp<L> implements Serializable {
|
||||
* @return 分页信息
|
||||
*/
|
||||
private static <L> PageResp<L> empty() {
|
||||
PageResp<L> pageResp = new PageResp<>();
|
||||
pageResp.setList(new ArrayList<>(0));
|
||||
return pageResp;
|
||||
}
|
||||
|
||||
public List<L> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<L> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(long total) {
|
||||
this.total = total;
|
||||
return new PageResp<>(Collections.emptyList(), 0L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ import cn.crane4j.core.support.OperateTemplate;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -37,22 +37,25 @@ import top.continew.starter.core.util.ClassUtils;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.util.validate.CheckUtils;
|
||||
import top.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.continew.starter.data.mybatis.plus.base.BaseMapper;
|
||||
import top.continew.starter.data.mybatis.plus.query.QueryWrapperHelper;
|
||||
import top.continew.starter.data.mybatis.plus.service.impl.ServiceImpl;
|
||||
import top.continew.starter.data.mp.base.BaseMapper;
|
||||
import top.continew.starter.data.mp.service.impl.ServiceImpl;
|
||||
import top.continew.starter.data.mp.util.QueryWrapperHelper;
|
||||
import top.continew.starter.extension.crud.annotation.DictField;
|
||||
import top.continew.starter.extension.crud.annotation.TreeField;
|
||||
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
import top.continew.starter.extension.crud.service.BaseService;
|
||||
import top.continew.starter.extension.crud.util.TreeUtils;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.file.excel.util.ExcelUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 业务实现基类
|
||||
@@ -76,7 +79,8 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
|
||||
@Override
|
||||
public PageResp<L> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper<T> queryWrapper = this.buildQueryWrapper(query);
|
||||
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
this.sort(queryWrapper, pageQuery);
|
||||
IPage<T> page = baseMapper.selectPage(new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper);
|
||||
PageResp<L> pageResp = PageResp.build(page, this.getListClass());
|
||||
pageResp.getList().forEach(this::fill);
|
||||
return pageResp;
|
||||
@@ -253,23 +257,24 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
|
||||
* @param sortQuery 排序查询条件
|
||||
*/
|
||||
protected void sort(QueryWrapper<T> queryWrapper, SortQuery sortQuery) {
|
||||
Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort();
|
||||
if (sortQuery == null || sortQuery.getSort().isUnsorted()) {
|
||||
return;
|
||||
}
|
||||
Sort sort = sortQuery.getSort();
|
||||
for (Sort.Order order : sort) {
|
||||
if (null != order) {
|
||||
String property = order.getProperty();
|
||||
String checkProperty;
|
||||
// 携带表别名则获取 . 后面的字段名
|
||||
if (property.contains(StringConstants.DOT)) {
|
||||
checkProperty = CollUtil.getLast(CharSequenceUtil.split(property, StringConstants.DOT));
|
||||
} else {
|
||||
checkProperty = property;
|
||||
}
|
||||
Optional<Field> optional = super.getEntityFields().stream()
|
||||
.filter(field -> checkProperty.equals(field.getName()))
|
||||
.findFirst();
|
||||
ValidationUtils.throwIf(optional.isEmpty(), "无效的排序字段 [{}]", property);
|
||||
queryWrapper.orderBy(true, order.isAscending(), CharSequenceUtil.toUnderlineCase(property));
|
||||
String property = order.getProperty();
|
||||
String checkProperty;
|
||||
// 携带表别名则获取 . 后面的字段名
|
||||
if (property.contains(StringConstants.DOT)) {
|
||||
checkProperty = CollUtil.getLast(CharSequenceUtil.split(property, StringConstants.DOT));
|
||||
} else {
|
||||
checkProperty = property;
|
||||
}
|
||||
Optional<Field> optional = super.getEntityFields().stream()
|
||||
.filter(field -> checkProperty.equals(field.getName()))
|
||||
.findFirst();
|
||||
ValidationUtils.throwIf(optional.isEmpty(), "无效的排序字段 [{}]", property);
|
||||
queryWrapper.orderBy(true, order.isAscending(), CharSequenceUtil.toUnderlineCase(property));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-datapermission</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-extension-datapermission-core</artifactId>
|
||||
<description>ContiNew Starter 扩展模块 - 数据权限 - 核心模块</description>
|
||||
</project>
|
||||
@@ -14,9 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.datapermission;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
package top.continew.starter.extension.datapermission.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -31,16 +29,9 @@ import java.lang.annotation.*;
|
||||
@Documented
|
||||
public @interface DataPermission {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #tableAlias()} attribute.
|
||||
*/
|
||||
@AliasFor("tableAlias")
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 表别名
|
||||
*/
|
||||
@AliasFor("value")
|
||||
String tableAlias() default "";
|
||||
|
||||
/**
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.datapermission.autoconfigure;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
|
||||
/**
|
||||
* 数据权限配置属性
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@ConfigurationProperties(PropertiesConstants.DATA_PERMISSION)
|
||||
public class DataPermissionProperties {
|
||||
|
||||
/**
|
||||
* 是否启用多租户
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.datapermission;
|
||||
package top.continew.starter.extension.datapermission.enums;
|
||||
|
||||
/**
|
||||
* 数据权限枚举
|
||||
@@ -14,15 +14,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.datapermission;
|
||||
package top.continew.starter.extension.datapermission.filter;
|
||||
|
||||
import top.continew.starter.extension.datapermission.model.UserContext;
|
||||
|
||||
/**
|
||||
* 数据权限过滤器接口
|
||||
* 数据权限用户上下文提供者
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public interface DataPermissionFilter {
|
||||
public interface DataPermissionUserContextProvider {
|
||||
|
||||
/**
|
||||
* 是否过滤
|
||||
@@ -32,9 +34,9 @@ public interface DataPermissionFilter {
|
||||
boolean isFilter();
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
* 获取用户上下文
|
||||
*
|
||||
* @return 当前用户信息
|
||||
* @return 用户上下文
|
||||
*/
|
||||
DataPermissionCurrentUser getCurrentUser();
|
||||
UserContext getUserContext();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.datapermission.model;
|
||||
|
||||
import top.continew.starter.extension.datapermission.enums.DataScope;
|
||||
|
||||
/**
|
||||
* 角色上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class RoleContext {
|
||||
|
||||
/**
|
||||
* 角色 ID
|
||||
*/
|
||||
private String roleId;
|
||||
|
||||
/**
|
||||
* 数据权限
|
||||
*/
|
||||
private DataScope dataScope;
|
||||
|
||||
public RoleContext() {
|
||||
}
|
||||
|
||||
public RoleContext(String roleId, DataScope dataScope) {
|
||||
this.roleId = roleId;
|
||||
this.dataScope = dataScope;
|
||||
}
|
||||
|
||||
public String getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
|
||||
public void setRoleId(String roleId) {
|
||||
this.roleId = roleId;
|
||||
}
|
||||
|
||||
public DataScope getDataScope() {
|
||||
return dataScope;
|
||||
}
|
||||
|
||||
public void setDataScope(DataScope dataScope) {
|
||||
this.dataScope = dataScope;
|
||||
}
|
||||
}
|
||||
@@ -14,17 +14,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.datapermission;
|
||||
package top.continew.starter.extension.datapermission.model;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 当前用户信息
|
||||
* 用户上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class DataPermissionCurrentUser {
|
||||
public class UserContext {
|
||||
|
||||
/**
|
||||
* 用户 ID
|
||||
@@ -34,53 +34,13 @@ public class DataPermissionCurrentUser {
|
||||
/**
|
||||
* 角色列表
|
||||
*/
|
||||
private Set<CurrentUserRole> roles;
|
||||
private Set<RoleContext> roles;
|
||||
|
||||
/**
|
||||
* 部门 ID
|
||||
*/
|
||||
private String deptId;
|
||||
|
||||
/**
|
||||
* 当前用户角色信息
|
||||
*/
|
||||
public static class CurrentUserRole {
|
||||
|
||||
/**
|
||||
* 角色 ID
|
||||
*/
|
||||
private String roleId;
|
||||
|
||||
/**
|
||||
* 数据权限
|
||||
*/
|
||||
private DataScope dataScope;
|
||||
|
||||
public CurrentUserRole() {
|
||||
}
|
||||
|
||||
public CurrentUserRole(String roleId, DataScope dataScope) {
|
||||
this.roleId = roleId;
|
||||
this.dataScope = dataScope;
|
||||
}
|
||||
|
||||
public String getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
|
||||
public void setRoleId(String roleId) {
|
||||
this.roleId = roleId;
|
||||
}
|
||||
|
||||
public DataScope getDataScope() {
|
||||
return dataScope;
|
||||
}
|
||||
|
||||
public void setDataScope(DataScope dataScope) {
|
||||
this.dataScope = dataScope;
|
||||
}
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
@@ -89,11 +49,11 @@ public class DataPermissionCurrentUser {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Set<CurrentUserRole> getRoles() {
|
||||
public Set<RoleContext> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Set<CurrentUserRole> roles) {
|
||||
public void setRoles(Set<RoleContext> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-datapermission</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-extension-datapermission-mp</artifactId>
|
||||
<description>ContiNew Starter 扩展模块 - 数据权限 - MyBatis Plus ORM 模块</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-datapermission-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.datapermission.autoconfigure;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.continew.starter.extension.datapermission.filter.DataPermissionUserContextProvider;
|
||||
import top.continew.starter.extension.datapermission.handler.DefaultDataPermissionHandler;
|
||||
|
||||
/**
|
||||
* 数据权限自动配置
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(DataPermissionProperties.class)
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.DATA_PERMISSION, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class DataPermissionAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DataPermissionAutoConfiguration.class);
|
||||
|
||||
private DataPermissionAutoConfiguration() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限拦截器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DataPermissionInterceptor dataPermissionInterceptor(DataPermissionHandler dataPermissionHandler) {
|
||||
return new DataPermissionInterceptor(dataPermissionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限处理器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DataPermissionHandler dataPermissionHandler(DataPermissionUserContextProvider dataPermissionUserContextProvider) {
|
||||
return new DefaultDataPermissionHandler(dataPermissionUserContextProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限用户上下文提供者
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DataPermissionUserContextProvider dataPermissionUserContextProvider() {
|
||||
if (log.isErrorEnabled()) {
|
||||
log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType
|
||||
.forClass(DataPermissionUserContextProvider.class));
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(DataPermissionUserContextProvider.class);
|
||||
}
|
||||
|
||||
static {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'DataPermission' completed initialization.");
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.datapermission;
|
||||
package top.continew.starter.extension.datapermission.handler;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
@@ -30,29 +30,36 @@ import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.select.*;
|
||||
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.extension.datapermission.annotation.DataPermission;
|
||||
import top.continew.starter.extension.datapermission.enums.DataScope;
|
||||
import top.continew.starter.extension.datapermission.filter.DataPermissionUserContextProvider;
|
||||
import top.continew.starter.extension.datapermission.model.RoleContext;
|
||||
import top.continew.starter.extension.datapermission.model.UserContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数据权限处理器实现类
|
||||
* 默认数据权限处理器
|
||||
*
|
||||
* @author <a href="https://gitee.com/baomidou/mybatis-plus/issues/I37I90">DataPermissionInterceptor 如何使用?</a>
|
||||
* @author Charles7c
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
public class DefaultDataPermissionHandler implements DataPermissionHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DataPermissionHandlerImpl.class);
|
||||
private final DataPermissionFilter dataPermissionFilter;
|
||||
private static final Logger log = LoggerFactory.getLogger(DefaultDataPermissionHandler.class);
|
||||
private final DataPermissionUserContextProvider dataPermissionUserContextProvider;
|
||||
|
||||
public DataPermissionHandlerImpl(DataPermissionFilter dataPermissionFilter) {
|
||||
this.dataPermissionFilter = dataPermissionFilter;
|
||||
public DefaultDataPermissionHandler(DataPermissionUserContextProvider dataPermissionUserContextProvider) {
|
||||
this.dataPermissionUserContextProvider = dataPermissionUserContextProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,7 +75,7 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
if (null == dataPermission || !CharSequenceUtil.equalsAny(methodName, name, name + "_COUNT")) {
|
||||
continue;
|
||||
}
|
||||
if (dataPermissionFilter.isFilter()) {
|
||||
if (dataPermissionUserContextProvider.isFilter()) {
|
||||
return buildDataScopeFilter(dataPermission, where);
|
||||
}
|
||||
}
|
||||
@@ -87,19 +94,19 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
*/
|
||||
private Expression buildDataScopeFilter(DataPermission dataPermission, Expression where) {
|
||||
Expression expression = null;
|
||||
DataPermissionCurrentUser currentUser = dataPermissionFilter.getCurrentUser();
|
||||
Set<DataPermissionCurrentUser.CurrentUserRole> roles = currentUser.getRoles();
|
||||
for (DataPermissionCurrentUser.CurrentUserRole role : roles) {
|
||||
DataScope dataScope = role.getDataScope();
|
||||
UserContext userContext = dataPermissionUserContextProvider.getUserContext();
|
||||
Set<RoleContext> roles = userContext.getRoles();
|
||||
for (RoleContext roleContext : roles) {
|
||||
DataScope dataScope = roleContext.getDataScope();
|
||||
if (DataScope.ALL.equals(dataScope)) {
|
||||
return where;
|
||||
}
|
||||
switch (dataScope) {
|
||||
case DEPT_AND_CHILD -> expression = this
|
||||
.buildDeptAndChildExpression(dataPermission, currentUser, expression);
|
||||
case DEPT -> expression = this.buildDeptExpression(dataPermission, currentUser, expression);
|
||||
case SELF -> expression = this.buildSelfExpression(dataPermission, currentUser, expression);
|
||||
case CUSTOM -> expression = this.buildCustomExpression(dataPermission, role, expression);
|
||||
.buildDeptAndChildExpression(dataPermission, userContext, expression);
|
||||
case DEPT -> expression = this.buildDeptExpression(dataPermission, userContext, expression);
|
||||
case SELF -> expression = this.buildSelfExpression(dataPermission, userContext, expression);
|
||||
case CUSTOM -> expression = this.buildCustomExpression(dataPermission, roleContext, expression);
|
||||
default -> throw new IllegalArgumentException("暂不支持 [%s] 数据权限".formatted(dataScope));
|
||||
}
|
||||
}
|
||||
@@ -115,12 +122,12 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
* </p>
|
||||
*
|
||||
* @param dataPermission 数据权限
|
||||
* @param currentUser 当前用户
|
||||
* @param userContext 用户上下文
|
||||
* @param expression 处理前的表达式
|
||||
* @return 处理完后的表达式
|
||||
*/
|
||||
private Expression buildDeptAndChildExpression(DataPermission dataPermission,
|
||||
DataPermissionCurrentUser currentUser,
|
||||
UserContext userContext,
|
||||
Expression expression) {
|
||||
ParenthesedSelect subSelect = new ParenthesedSelect();
|
||||
PlainSelect select = new PlainSelect();
|
||||
@@ -128,10 +135,10 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
select.setFromItem(new Table(dataPermission.deptTableAlias()));
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(new Column(dataPermission.id()));
|
||||
equalsTo.setRightExpression(new LongValue(currentUser.getDeptId()));
|
||||
equalsTo.setRightExpression(new LongValue(userContext.getDeptId()));
|
||||
Function function = new Function();
|
||||
function.setName("find_in_set");
|
||||
function.setParameters(new ExpressionList(new LongValue(currentUser.getDeptId()), new Column("ancestors")));
|
||||
function.setParameters(new ExpressionList(new LongValue(userContext.getDeptId()), new Column("ancestors")));
|
||||
select.setWhere(new OrExpression(equalsTo, function));
|
||||
subSelect.setSelect(select);
|
||||
// 构建父查询
|
||||
@@ -149,16 +156,16 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
* </p>
|
||||
*
|
||||
* @param dataPermission 数据权限
|
||||
* @param currentUser 当前用户
|
||||
* @param userContext 用户上下文
|
||||
* @param expression 处理前的表达式
|
||||
* @return 处理完后的表达式
|
||||
*/
|
||||
private Expression buildDeptExpression(DataPermission dataPermission,
|
||||
DataPermissionCurrentUser currentUser,
|
||||
UserContext userContext,
|
||||
Expression expression) {
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(this.buildColumn(dataPermission.tableAlias(), dataPermission.deptId()));
|
||||
equalsTo.setRightExpression(new LongValue(currentUser.getDeptId()));
|
||||
equalsTo.setRightExpression(new LongValue(userContext.getDeptId()));
|
||||
return null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
|
||||
}
|
||||
|
||||
@@ -170,16 +177,16 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
* </p>
|
||||
*
|
||||
* @param dataPermission 数据权限
|
||||
* @param currentUser 当前用户
|
||||
* @param userContext 用户上下文
|
||||
* @param expression 处理前的表达式
|
||||
* @return 处理完后的表达式
|
||||
*/
|
||||
private Expression buildSelfExpression(DataPermission dataPermission,
|
||||
DataPermissionCurrentUser currentUser,
|
||||
UserContext userContext,
|
||||
Expression expression) {
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(this.buildColumn(dataPermission.tableAlias(), dataPermission.userId()));
|
||||
equalsTo.setRightExpression(new LongValue(currentUser.getUserId()));
|
||||
equalsTo.setRightExpression(new LongValue(userContext.getUserId()));
|
||||
return null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
|
||||
}
|
||||
|
||||
@@ -192,12 +199,12 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
* </p>
|
||||
*
|
||||
* @param dataPermission 数据权限
|
||||
* @param role 当前用户角色
|
||||
* @param roleContext 角色上下文
|
||||
* @param expression 处理前的表达式
|
||||
* @return 处理完后的表达式
|
||||
*/
|
||||
private Expression buildCustomExpression(DataPermission dataPermission,
|
||||
DataPermissionCurrentUser.CurrentUserRole role,
|
||||
RoleContext roleContext,
|
||||
Expression expression) {
|
||||
ParenthesedSelect subSelect = new ParenthesedSelect();
|
||||
PlainSelect select = new PlainSelect();
|
||||
@@ -205,7 +212,7 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
select.setFromItem(new Table(dataPermission.roleDeptTableAlias()));
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(new Column(dataPermission.roleId()));
|
||||
equalsTo.setRightExpression(new LongValue(role.getRoleId()));
|
||||
equalsTo.setRightExpression(new LongValue(roleContext.getRoleId()));
|
||||
select.setWhere(equalsTo);
|
||||
subSelect.setSelect(select);
|
||||
// 构建父查询
|
||||
@@ -0,0 +1 @@
|
||||
top.continew.starter.extension.datapermission.autoconfigure.DataPermissionAutoConfiguration
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-extension-datapermission</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<description>ContiNew Starter 扩展模块 - 数据权限</description>
|
||||
|
||||
<modules>
|
||||
<module>continew-starter-extension-datapermission-core</module>
|
||||
<module>continew-starter-extension-datapermission-mp</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-extension-tenant</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-extension-tenant-core</artifactId>
|
||||
<description>ContiNew Starter 扩展模块 - 多租户 - 核心模块</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- TTL(线程间传递 ThreadLocal,异步执行时上下文传递的解决方案) -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>transmittable-thread-local</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -14,22 +14,18 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.data.mybatis.plus.autoconfigure;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
package top.continew.starter.extension.tenant.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 是否启用数据权限注解
|
||||
* 多租户数据源级隔离忽略注解
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.1.0
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ConditionalOnProperty(prefix = "mybatis-plus.extension.data-permission", name = PropertiesConstants.ENABLED, havingValue = "true")
|
||||
public @interface ConditionalOnEnabledDataPermission {
|
||||
}
|
||||
public @interface TenantDataSourceIgnore {
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package top.continew.starter.extension.tenant.autoconfigure;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import top.continew.starter.extension.tenant.context.TenantContext;
|
||||
import top.continew.starter.extension.tenant.context.TenantContextHolder;
|
||||
|
||||
/**
|
||||
* 租户拦截器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class TenantInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final TenantProperties tenantProperties;
|
||||
|
||||
public TenantInterceptor(TenantProperties tenantProperties) {
|
||||
this.tenantProperties = tenantProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
String tenantId = request.getHeader(tenantProperties.getTenantIdHeader());
|
||||
TenantContext tenantContext = new TenantContext();
|
||||
tenantContext.setTenantId(Convert.toLong(tenantId));
|
||||
TenantContextHolder.setContext(tenantContext);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.tenant.autoconfigure;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 租户配置属性
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@ConfigurationProperties(PropertiesConstants.TENANT)
|
||||
public class TenantProperties {
|
||||
|
||||
/**
|
||||
* 是否启用多租户
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* 租户隔离级别
|
||||
*/
|
||||
private TenantIsolationLevel isolationLevel = TenantIsolationLevel.LINE;
|
||||
|
||||
/**
|
||||
* 租户 ID 列名
|
||||
*/
|
||||
private String tenantIdColumn = "tenant_id";
|
||||
|
||||
/**
|
||||
* 请求头中租户 ID 键名
|
||||
*/
|
||||
private String tenantIdHeader = "X-Tenant-Id";
|
||||
|
||||
/**
|
||||
* 超级租户 ID
|
||||
*/
|
||||
private Long superTenantId = 1L;
|
||||
|
||||
/**
|
||||
* 忽略表(忽略拼接多租户条件)
|
||||
*/
|
||||
private List<String> ignoreTables;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public TenantIsolationLevel getIsolationLevel() {
|
||||
return isolationLevel;
|
||||
}
|
||||
|
||||
public void setIsolationLevel(TenantIsolationLevel isolationLevel) {
|
||||
this.isolationLevel = isolationLevel;
|
||||
}
|
||||
|
||||
public String getTenantIdColumn() {
|
||||
return tenantIdColumn;
|
||||
}
|
||||
|
||||
public void setTenantIdColumn(String tenantIdColumn) {
|
||||
this.tenantIdColumn = tenantIdColumn;
|
||||
}
|
||||
|
||||
public String getTenantIdHeader() {
|
||||
return tenantIdHeader;
|
||||
}
|
||||
|
||||
public void setTenantIdHeader(String tenantIdHeader) {
|
||||
this.tenantIdHeader = tenantIdHeader;
|
||||
}
|
||||
|
||||
public Long getSuperTenantId() {
|
||||
return superTenantId;
|
||||
}
|
||||
|
||||
public void setSuperTenantId(Long superTenantId) {
|
||||
this.superTenantId = superTenantId;
|
||||
}
|
||||
|
||||
public List<String> getIgnoreTables() {
|
||||
return ignoreTables;
|
||||
}
|
||||
|
||||
public void setIgnoreTables(List<String> ignoreTables) {
|
||||
this.ignoreTables = ignoreTables;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.tenant.autoconfigure;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
|
||||
/**
|
||||
* 多租户 Web MVC 自动配置
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnProperty(prefix = PropertiesConstants.TENANT, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class TenantWebMvcAutoConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private final TenantProperties tenantProperties;
|
||||
|
||||
public TenantWebMvcAutoConfiguration(TenantProperties tenantProperties) {
|
||||
this.tenantProperties = tenantProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new TenantInterceptor(tenantProperties));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package top.continew.starter.extension.tenant.config;
|
||||
|
||||
/**
|
||||
* 租户数据源配置
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class TenantDataSource {
|
||||
|
||||
/**
|
||||
* 连接池名称
|
||||
*/
|
||||
private String poolName;
|
||||
|
||||
/**
|
||||
* 驱动类名
|
||||
*/
|
||||
private String driverClassName;
|
||||
|
||||
/**
|
||||
* 数据库连接地址
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 数据库用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 数据库密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
public String getPoolName() {
|
||||
return poolName;
|
||||
}
|
||||
|
||||
public void setPoolName(String poolName) {
|
||||
this.poolName = poolName;
|
||||
}
|
||||
|
||||
public String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
public void setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package top.continew.starter.extension.tenant.config;
|
||||
|
||||
/**
|
||||
* 租户数据源提供者
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public interface TenantDataSourceProvider {
|
||||
|
||||
/**
|
||||
* 根据租户 ID 获取数据源配置
|
||||
*
|
||||
* @param tenantId 租户 ID
|
||||
* @return 数据源配置
|
||||
*/
|
||||
TenantDataSource getByTenantId(String tenantId);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package top.continew.starter.extension.tenant.context;
|
||||
|
||||
/**
|
||||
* 租户上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class TenantContext {
|
||||
|
||||
/**
|
||||
* 租户 ID
|
||||
*/
|
||||
private Long tenantId;
|
||||
|
||||
public Long getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(Long tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package top.continew.starter.extension.tenant.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 租户上下文 Holder
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class TenantContextHolder {
|
||||
|
||||
private static final TransmittableThreadLocal<TenantContext> CONTEXT_HOLDER = new TransmittableThreadLocal<>();
|
||||
|
||||
private TenantContextHolder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上下文
|
||||
*
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void setContext(TenantContext context) {
|
||||
CONTEXT_HOLDER.set(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文
|
||||
*
|
||||
* @return 上下文
|
||||
*/
|
||||
public static TenantContext getContext() {
|
||||
return CONTEXT_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除上下文
|
||||
*/
|
||||
public static void clearContext() {
|
||||
CONTEXT_HOLDER.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取租户 ID
|
||||
*
|
||||
* @return 租户 ID
|
||||
*/
|
||||
public static Long getTenantId() {
|
||||
return Optional.ofNullable(getContext()).map(TenantContext::getTenantId).orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package top.continew.starter.extension.tenant.enums;
|
||||
|
||||
/**
|
||||
* 租户隔离级别
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public enum TenantIsolationLevel {
|
||||
|
||||
/**
|
||||
* 行级
|
||||
*/
|
||||
LINE,
|
||||
|
||||
/**
|
||||
* 数据源级
|
||||
*/
|
||||
DATASOURCE
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package top.continew.starter.extension.tenant.handler;
|
||||
|
||||
import top.continew.starter.extension.tenant.config.TenantDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 租户数据源级隔离处理器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public interface TenantDataSourceHandler {
|
||||
|
||||
/**
|
||||
* 切换数据源
|
||||
*
|
||||
* @param dataSourceName 数据源名称
|
||||
*/
|
||||
void changeDataSource(String dataSourceName);
|
||||
|
||||
/**
|
||||
* 是否存在指定数据源
|
||||
*
|
||||
* @param dataSourceName 数据源名称
|
||||
* @return 是否存在指定数据源
|
||||
*/
|
||||
boolean containsDataSource(String dataSourceName);
|
||||
|
||||
/**
|
||||
* 创建数据源
|
||||
*
|
||||
* @param tenantDataSource 数据源配置
|
||||
* @return 数据源
|
||||
*/
|
||||
DataSource createDataSource(TenantDataSource tenantDataSource);
|
||||
|
||||
/**
|
||||
* 移除数据源
|
||||
*
|
||||
* @param dataSourceName 数据源名称
|
||||
*/
|
||||
void removeDataSource(String dataSourceName);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user