Compare commits

...

15 Commits

Author SHA1 Message Date
2388d32363 release: v2.6.0 2024-09-06 20:39:50 +08:00
9fdbfdf8bb fix: 修复单参查询加密失效的问题 2024-09-06 20:29:22 +08:00
e9b81f9466 fix: 重构并修复更新场景加密失效的问题(仍需处理 MP 和单参查询加密问题) 2024-09-06 00:03:35 +08:00
427143db69 chore: 升级依赖
SnailJob 1.1.0 => 1.1.2
SaToken 1.38.0 => 1.39.0
MyBatis Flex 1.9.3 => 1.9.7
Redisson 3.32.0 => 3.35.0
CosID 2.9.1 => 2.9.6
SMS4J 3.2.1 => 3.3.2
XFileStorage 2.2.0 => 2.2.1
aws-java-sdk-s3 1.12.761 => 1.12.771
snakeyaml 2.2 => 2.3
Hutool 5.8.32 => 5.8.29
2024-09-05 20:08:22 +08:00
2208dbd028 chore: 升级依赖 graceful-response 4.0.1-boot3 => 5.0.0-boot3 2024-09-05 19:54:19 +08:00
9e5f33b2c9 revert(data): 还原 SQL 函数接口 2024-09-04 20:21:07 +08:00
260f484af9 chore(web): 链路追踪配置属性响应头名称 => 链路 ID 名称 2024-09-04 20:12:44 +08:00
梓陌
4caf0a0db6 fix(log): 修复接口耗时统计始终为0ms问题 2024-09-03 06:19:54 +00:00
16b6e9b466 feat(log): 不记录日志也支持开启打印访问日志 2024-08-30 20:40:30 +08:00
eac5c1fa79 fix(log): 修复日志全局 includes 配置会被局部修改的问题 2024-08-30 06:54:00 +00:00
httpsjt
52fc9d086d 日志includes引用传递修改问题。 2024-08-30 14:45:50 +08:00
5e0eea2ed8 refactor(data): mybatis-plus => mp,mybatis-flex => mf 2024-08-29 19:47:56 +08:00
784a56fd4f refactor(web): 提升接口文档响应类型优化扩展性 2024-08-29 19:42:44 +08:00
d3fa00d14c fix(data): 移除 DataPermission 注解的 value 属性 2024-08-28 22:09:53 +08:00
e55eb17d64 feat(web): 新增 isMatch 路径是否匹配方法
match => isMatch
2024-08-19 22:40:53 +08:00
68 changed files with 592 additions and 485 deletions

View File

@@ -1,3 +1,38 @@
## [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 => mpmybatis-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
- SMS4J 3.2.1 => 3.3.2
- X File Storage 2.2.0 => 2.2.1
- Hutool 5.8.32 => 5.8.29
- 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)
### 💎 功能优化

View File

@@ -206,11 +206,11 @@ continew-starter.web:
### 数据访问模块
| 模块名称 | 模块说明 |
| ---------------------------------- | --------------------- |
| continew-starter-data-core | 数据访问核心模块 |
| continew-starter-data-mybatis-plus | MyBatis Plus 自动配置 |
| continew-starter-data-mybatis-flex | MyBatis Flex 自动配置 |
| 模块名称 | 模块说明 |
|----------------------------| --------------------- |
| continew-starter-data-core | 数据访问核心模块 |
| continew-starter-data-mp | MyBatis Plus 自动配置 |
| continew-starter-data-mf | MyBatis Flex 自动配置 |
### 认证模块

View File

@@ -272,6 +272,11 @@ public class StringConstants {
*/
public static final String ROUND_BRACKET_END = ")";
/**
* 等号(=
*/
public static final String EQUALS = "=";
/**
* 路径模式
*/

View File

@@ -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);
}
},;

View File

@@ -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);
}

View File

@@ -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>

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 自动配置

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 "";
/**

View File

@@ -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.*;

View File

@@ -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;

View File

@@ -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;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.flex.datapermission;
package top.continew.starter.data.mf.datapermission;
/**
* 数据权限过滤器接口

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.flex.datapermission;
package top.continew.starter.data.mf.datapermission;
/**
* 数据权限枚举

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.flex.service;
package top.continew.starter.data.mf.service;
/**
* 通用业务接口

View File

@@ -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;
/**
* 通用业务实现类

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.flex.util;
package top.continew.starter.data.mf.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;

View File

@@ -0,0 +1 @@
top.continew.starter.data.mf.autoconfigure.MybatisFlexAutoConfiguration

View File

@@ -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>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.autoconfigure;
package top.continew.starter.data.mp.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import top.continew.starter.core.constant.PropertiesConstants;

View File

@@ -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 扩展配置属性

View File

@@ -14,7 +14,7 @@
* 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;
@@ -22,6 +22,7 @@ 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.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import jakarta.annotation.PostConstruct;
import org.mybatis.spring.annotation.MapperScan;
@@ -38,10 +39,12 @@ 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.datapermission.DataPermissionFilter;
import top.continew.starter.data.mp.datapermission.DataPermissionHandlerImpl;
import top.continew.starter.data.mp.handler.MybatisBaseEnumTypeHandler;
import java.util.Map;
/**
* MyBatis Plus 自动配置
@@ -76,6 +79,11 @@ public class MybatisPlusAutoConfiguration {
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 其他拦截器
Map<String, InnerInterceptor> innerInterceptors = SpringUtil.getBeansOfType(InnerInterceptor.class);
if (!innerInterceptors.isEmpty()) {
innerInterceptors.values().forEach(interceptor::addInnerInterceptor);
}
// 数据权限插件
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties
.getDataPermission();

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 生成器配置属性

View File

@@ -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;

View File

@@ -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.data.mp.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 "";
/**

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.datapermission;
package top.continew.starter.data.mp.datapermission;
import java.util.Set;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.datapermission;
package top.continew.starter.data.mp.datapermission;
/**
* 数据权限过滤器接口

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.datapermission;
package top.continew.starter.data.mp.datapermission;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.datapermission;
package top.continew.starter.data.mp.datapermission;
/**
* 数据权限枚举

View File

@@ -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 生成器类型枚举

View File

@@ -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;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.service;
package top.continew.starter.data.mp.service;
/**
* 通用业务接口

View File

@@ -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;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.data.mybatis.plus.util;
package top.continew.starter.data.mp.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -0,0 +1 @@
top.continew.starter.data.mp.autoconfigure.MybatisPlusAutoConfiguration

View File

@@ -1 +0,0 @@
top.continew.starter.data.mybatis.flex.autoconfigure.MybatisFlexAutoConfiguration

View File

@@ -1 +0,0 @@
top.continew.starter.data.mybatis.plus.autoconfigure.MybatisPlusAutoConfiguration

View File

@@ -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>

View File

@@ -43,33 +43,33 @@
<properties>
<!-- 项目版本号 -->
<revision>2.5.2</revision>
<snail-job.version>1.1.0</snail-job.version>
<sa-token.version>1.38.0</sa-token.version>
<revision>2.6.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-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>
<redisson.version>3.35.0</redisson.version>
<cosid.version>2.9.6</cosid.version>
<sms4j.version>3.2.1</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>
<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 FlexMyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
<dependency>
@@ -373,14 +378,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>

View File

@@ -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>

View File

@@ -33,9 +33,9 @@ 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.util.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.util.QueryWrapperHelper;
import top.continew.starter.data.mf.service.impl.ServiceImpl;
import top.continew.starter.extension.crud.annotation.TreeField;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery;

View File

@@ -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>

View File

@@ -37,9 +37,9 @@ 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.service.impl.ServiceImpl;
import top.continew.starter.data.mybatis.plus.util.QueryWrapperHelper;
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;

View File

@@ -18,7 +18,6 @@ package top.continew.starter.log.core.model;
import top.continew.starter.log.core.enums.Include;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Set;
@@ -78,7 +77,7 @@ public class LogRecord {
* @return 日志记录器
*/
public static Started start(RecordableHttpRequest request) {
return start(Clock.systemUTC(), request);
return start(Instant.now(), request);
}
/**
@@ -88,7 +87,7 @@ public class LogRecord {
* @param request 请求信息
* @return 日志记录器
*/
public static Started start(Clock timestamp, RecordableHttpRequest request) {
public static Started start(Instant timestamp, RecordableHttpRequest request) {
return new Started(timestamp, request);
}
@@ -101,22 +100,11 @@ public class LogRecord {
private final RecordableHttpRequest request;
private Started(Clock clock, RecordableHttpRequest request) {
this.timestamp = Instant.now(clock);
private Started(Instant timestamp, RecordableHttpRequest request) {
this.timestamp = timestamp;
this.request = request;
}
/**
* 结束日志记录
*
* @param response 响应信息
* @param includes 包含信息
* @return 日志记录
*/
public LogRecord finish(RecordableHttpResponse response, Set<Include> includes) {
return finish(Clock.systemUTC(), response, includes);
}
/**
* 结束日志记录
*
@@ -125,10 +113,10 @@ public class LogRecord {
* @param includes 包含信息
* @return 日志记录
*/
public LogRecord finish(Clock clock, RecordableHttpResponse response, Set<Include> includes) {
public LogRecord finish(Instant timestamp, RecordableHttpResponse response, Set<Include> includes) {
LogRequest logRequest = new LogRequest(this.request, includes);
LogResponse logResponse = new LogResponse(response, includes);
Duration duration = Duration.between(this.timestamp, Instant.now(clock));
Duration duration = Duration.between(this.timestamp, timestamp);
return new LogRecord(this.timestamp, logRequest, logResponse, duration);
}
}

View File

@@ -19,9 +19,9 @@ package top.continew.starter.log.interceptor.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.log.core.enums.Include;
import top.continew.starter.web.util.SpringWebUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -41,13 +41,16 @@ public class LogProperties {
/**
* 是否打印日志,开启后可打印访问日志(类似于 Nginx access log
* <p>
* 不记录日志也支持开启打印访问日志
* </p>
*/
private Boolean isPrint = false;
/**
* 包含信息
*/
private Set<Include> includes = new HashSet<>(Include.defaultIncludes());
private Set<Include> includes = Include.defaultIncludes();
/**
* 放行路由
@@ -85,4 +88,14 @@ public class LogProperties {
public void setExcludePatterns(List<String> excludePatterns) {
this.excludePatterns = excludePatterns;
}
/**
* 是否匹配放行路由
*
* @param uri 请求 URI
* @return 是否匹配
*/
public boolean isMatch(String uri) {
return this.getExcludePatterns().stream().anyMatch(pattern -> SpringWebUtils.isMatch(pattern, uri));
}
}

View File

@@ -30,7 +30,6 @@ import org.springframework.web.util.ContentCachingResponseWrapper;
import org.springframework.web.util.WebUtils;
import top.continew.starter.log.core.enums.Include;
import top.continew.starter.log.interceptor.autoconfigure.LogProperties;
import top.continew.starter.web.util.SpringWebUtils;
import java.io.IOException;
import java.net.URI;
@@ -70,12 +69,13 @@ public class LogFilter extends OncePerRequestFilter implements Ordered {
filterChain.doFilter(request, response);
return;
}
boolean isMatch = logProperties.isMatch(request.getRequestURI());
// 包装输入流,可重复读取
if (this.isRequestWrapper(request)) {
if (!isMatch && this.isRequestWrapper(request)) {
request = new ContentCachingRequestWrapper(request);
}
// 包装输出流,可重复读取
boolean isResponseWrapper = this.isResponseWrapper(response);
boolean isResponseWrapper = !isMatch && this.isResponseWrapper(response);
if (isResponseWrapper) {
response = new ContentCachingResponseWrapper(response);
}
@@ -98,14 +98,7 @@ public class LogFilter extends OncePerRequestFilter implements Ordered {
}
// 不拦截 /error
ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class);
if (request.getRequestURI().equals(serverProperties.getError().getPath())) {
return false;
}
// 放行
boolean isMatch = logProperties.getExcludePatterns()
.stream()
.anyMatch(pattern -> SpringWebUtils.match(pattern, request.getRequestURI()));
return !isMatch;
return !request.getRequestURI().equals(serverProperties.getError().getPath());
}
/**

View File

@@ -32,10 +32,11 @@ import top.continew.starter.log.core.annotation.Log;
import top.continew.starter.log.core.dao.LogDao;
import top.continew.starter.log.core.enums.Include;
import top.continew.starter.log.core.model.LogRecord;
import top.continew.starter.log.core.model.LogResponse;
import top.continew.starter.log.interceptor.autoconfigure.LogProperties;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
/**
@@ -49,7 +50,8 @@ public class LogInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);
private final LogDao logDao;
private final LogProperties logProperties;
private final TransmittableThreadLocal<LogRecord.Started> timestampTtl = new TransmittableThreadLocal<>();
private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>();
private final TransmittableThreadLocal<LogRecord.Started> logTtl = new TransmittableThreadLocal<>();
public LogInterceptor(LogDao logDao, LogProperties logProperties) {
this.logDao = logDao;
@@ -60,13 +62,14 @@ public class LogInterceptor implements HandlerInterceptor {
public boolean preHandle(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler) {
Clock timestamp = Clock.systemUTC();
if (this.isRequestRecord(handler)) {
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
}
LogRecord.Started startedLogRecord = LogRecord.start(timestamp, new RecordableServletHttpRequest(request));
timestampTtl.set(startedLogRecord);
Instant startTime = Instant.now();
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
timeTtl.set(startTime);
}
if (this.isRequestRecord(handler, request)) {
LogRecord.Started startedLogRecord = LogRecord.start(startTime, new RecordableServletHttpRequest(request));
logTtl.set(startedLogRecord);
}
return true;
}
@@ -76,18 +79,23 @@ public class LogInterceptor implements HandlerInterceptor {
@NonNull HttpServletResponse response,
@NonNull Object handler,
Exception e) {
LogRecord.Started startedLogRecord = timestampTtl.get();
if (null == startedLogRecord) {
return;
}
timestampTtl.remove();
try {
Instant endTime = Instant.now();
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
Duration timeTaken = Duration.between(timeTtl.get(), endTime);
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response
.getStatus(), timeTaken.toMillis());
}
LogRecord.Started startedLogRecord = logTtl.get();
if (null == startedLogRecord) {
return;
}
HandlerMethod handlerMethod = (HandlerMethod)handler;
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
Log classLog = handlerMethod.getBeanType().getDeclaredAnnotation(Log.class);
Set<Include> includeSet = this.getIncludes(methodLog, classLog);
LogRecord finishedLogRecord = startedLogRecord.finish(new RecordableServletHttpResponse(response, response
.getStatus()), includeSet);
LogRecord finishedLogRecord = startedLogRecord
.finish(endTime, new RecordableServletHttpResponse(response, response.getStatus()), includeSet);
// 记录日志描述
if (includeSet.contains(Include.DESCRIPTION)) {
this.logDescription(finishedLogRecord, methodLog, handlerMethod);
@@ -96,14 +104,12 @@ public class LogInterceptor implements HandlerInterceptor {
if (includeSet.contains(Include.MODULE)) {
this.logModule(finishedLogRecord, methodLog, classLog, handlerMethod);
}
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
LogResponse logResponse = finishedLogRecord.getResponse();
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), logResponse
.getStatus(), finishedLogRecord.getTimeTaken().toMillis());
}
logDao.add(finishedLogRecord);
} catch (Exception ex) {
log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex);
} finally {
timeTtl.remove();
logTtl.remove();
}
}
@@ -115,7 +121,7 @@ public class LogInterceptor implements HandlerInterceptor {
* @return 日志包含信息
*/
private Set<Include> getIncludes(Log methodLog, Log classLog) {
Set<Include> includeSet = logProperties.getIncludes();
Set<Include> includeSet = new HashSet<>(logProperties.getIncludes());
if (null != classLog) {
this.processInclude(includeSet, classLog);
}
@@ -194,10 +200,14 @@ public class LogInterceptor implements HandlerInterceptor {
* @param handler 处理器
* @return true需要记录false不需要记录
*/
private boolean isRequestRecord(Object handler) {
private boolean isRequestRecord(Object handler, HttpServletRequest request) {
if (!(handler instanceof HandlerMethod handlerMethod)) {
return false;
}
// 如果接口匹配排除列表,不记录日志
if (logProperties.isMatch(request.getRequestURI())) {
return false;
}
// 如果接口被隐藏,不记录日志
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
if (null != methodOperation && methodOperation.hidden()) {

View File

@@ -22,7 +22,7 @@
<!-- MyBatis PlusMyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<artifactId>mybatis-plus-extension</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -50,7 +50,7 @@ public class CryptoAutoConfiguration {
* MyBatis 加密拦截器配置
*/
@Bean
@ConditionalOnMissingBean(MyBatisEncryptInterceptor.class)
@ConditionalOnMissingBean
public MyBatisEncryptInterceptor myBatisEncryptInterceptor() {
return new MyBatisEncryptInterceptor(properties);
}

View File

@@ -16,17 +16,13 @@
package top.continew.starter.security.crypto.core;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.exception.BaseException;
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
import top.continew.starter.security.crypto.encryptor.IEncryptor;
import top.continew.starter.security.crypto.enums.Algorithm;
@@ -44,8 +40,9 @@ import java.util.stream.Stream;
* @author Charles7c
* @since 1.4.0
*/
public abstract class AbstractMyBatisInterceptor implements Interceptor {
public abstract class AbstractMyBatisInterceptor {
private static final Map<Class<?>, List<Field>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
private static final Map<String, Map<String, FieldEncrypt>> ENCRYPT_PARAM_CACHE = new ConcurrentHashMap<>();
/**
@@ -54,15 +51,24 @@ public abstract class AbstractMyBatisInterceptor implements Interceptor {
* @param obj 对象
* @return 字段列表
*/
public List<Field> getEncryptFields(Object obj) {
protected List<Field> getEncryptFields(Object obj) {
if (null == obj) {
return Collections.emptyList();
}
return Arrays.stream(ReflectUtil.getFields(obj.getClass()))
return this.getEncryptFields(obj.getClass());
}
/**
* 获取所有字符串类型、需要加/解密的、有值字段
*
* @param clazz 类型对象
* @return 字段列表
*/
protected List<Field> getEncryptFields(Class<?> clazz) {
return CLASS_FIELD_CACHE.computeIfAbsent(clazz, key -> Arrays.stream(ReflectUtil.getFields(clazz))
.filter(field -> String.class.equals(field.getType()))
.filter(field -> null != field.getAnnotation(FieldEncrypt.class))
.filter(field -> null != ReflectUtil.getFieldValue(obj, field))
.toList();
.toList());
}
/**
@@ -71,7 +77,7 @@ public abstract class AbstractMyBatisInterceptor implements Interceptor {
* @param fieldEncrypt 字段加密注解
* @return 加/解密处理器
*/
public IEncryptor getEncryptor(FieldEncrypt fieldEncrypt) {
protected IEncryptor getEncryptor(FieldEncrypt fieldEncrypt) {
Class<? extends IEncryptor> encryptorClass = fieldEncrypt.encryptor();
// 使用预定义加/解密处理器
if (encryptorClass == IEncryptor.class) {
@@ -86,27 +92,47 @@ public abstract class AbstractMyBatisInterceptor implements Interceptor {
* 获取加密参数
*
* @param mappedStatement 映射语句
* @return 加密参数
* @return 获取加密参数
*/
public Map<String, FieldEncrypt> getEncryptParams(MappedStatement mappedStatement) {
return getEncryptParams(mappedStatement, null);
protected Map<String, FieldEncrypt> getEncryptParameters(MappedStatement mappedStatement) {
String mappedStatementId = mappedStatement.getId();
return ENCRYPT_PARAM_CACHE.computeIfAbsent(mappedStatementId, key -> {
Method method = this.getMethod(mappedStatementId);
if (null == method) {
return Collections.emptyMap();
}
Map<String, FieldEncrypt> encryptMap = new HashMap<>();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
FieldEncrypt fieldEncrypt = parameter.getAnnotation(FieldEncrypt.class);
if (null == fieldEncrypt) {
continue;
}
String parameterName = this.getParameterName(parameter);
encryptMap.put(parameterName, fieldEncrypt);
if (String.class.equals(parameter.getType())) {
encryptMap.put("param" + (i + 1), fieldEncrypt);
}
}
return encryptMap;
});
}
/**
* 获取加密参数
* 获取映射方法
*
* @param mappedStatement 映射语句
* @param parameterCount 参数数量
* @return 加密参数
* @param mappedStatementId 映射语句 ID
* @return 映射方法
*/
public Map<String, FieldEncrypt> getEncryptParams(MappedStatement mappedStatement, Integer parameterCount) {
String mappedStatementId = mappedStatement.getId();
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
if (SqlCommandType.UPDATE != sqlCommandType) {
return ENCRYPT_PARAM_CACHE.computeIfAbsent(mappedStatementId, key -> this
.getEncryptParams(mappedStatementId, parameterCount));
} else {
return this.getEncryptParams(mappedStatementId, parameterCount);
private Method getMethod(String mappedStatementId) {
String className = CharSequenceUtil.subBefore(mappedStatementId, StringConstants.DOT, true);
String methodName = CharSequenceUtil.subAfter(mappedStatementId, StringConstants.DOT, true);
try {
Method[] methods = ReflectUtil.getMethods(Class.forName(className));
return Stream.of(methods).filter(method -> method.getName().equals(methodName)).findFirst().orElse(null);
} catch (ClassNotFoundException e) {
throw new BaseException(e);
}
}
@@ -120,76 +146,4 @@ public abstract class AbstractMyBatisInterceptor implements Interceptor {
Param param = parameter.getAnnotation(Param.class);
return null != param ? param.value() : parameter.getName();
}
/**
* 获取加密参数列表
*
* @param mappedStatementId 映射语句 ID
* @param parameterCount 参数数量
* @return 加密参数列表
*/
private Map<String, FieldEncrypt> getEncryptParams(String mappedStatementId, Integer parameterCount) {
Method method = this.getMethod(mappedStatementId, parameterCount);
if (method == null) {
return Collections.emptyMap();
}
return this.getEncryptParams(method);
}
/**
* 获取映射方法
*
* @param mappedStatementId 映射语句 ID
* @param parameterCount 参数数量
* @return 映射方法
*/
private Method getMethod(String mappedStatementId, Integer parameterCount) {
try {
String className = CharSequenceUtil.subBefore(mappedStatementId, StringConstants.DOT, true);
String wrapperMethodName = CharSequenceUtil.subAfter(mappedStatementId, StringConstants.DOT, true);
String methodName = Stream.of("_mpCount", "_COUNT")
.filter(wrapperMethodName::endsWith)
.findFirst()
.map(suffix -> wrapperMethodName.substring(0, wrapperMethodName.length() - suffix.length()))
.orElse(wrapperMethodName);
// 获取真实方法
Optional<Method> methodOptional = Arrays.stream(ReflectUtil.getMethods(Class.forName(className), m -> {
if (parameterCount != null) {
return Objects.equals(m.getName(), methodName) && m.getParameterCount() == parameterCount;
}
return Objects.equals(m.getName(), methodName);
})).findFirst();
return methodOptional.orElse(null);
} catch (ClassNotFoundException e) {
throw new BusinessException(e.getMessage());
}
}
/**
* 获取加密参数列表
*
* @param method 方法
* @return 加密参数列表
*/
private Map<String, FieldEncrypt> getEncryptParams(Method method) {
// 获取方法中的加密参数
Map<String, FieldEncrypt> map = MapUtil.newHashMap();
Parameter[] parameterArr = method.getParameters();
for (int i = 0; i < parameterArr.length; i++) {
Parameter parameter = parameterArr[i];
String parameterName = this.getParameterName(parameter);
FieldEncrypt fieldEncrypt = parameter.getAnnotation(FieldEncrypt.class);
if (null != fieldEncrypt) {
map.put(parameterName, fieldEncrypt);
if (String.class.equals(parameter.getType())) {
map.put("param" + (i + 1), fieldEncrypt);
}
} else if (parameterName.startsWith(Constants.ENTITY)) {
map.put(parameterName, null);
} else if (parameterName.startsWith(Constants.WRAPPER)) {
map.put(parameterName, null);
}
}
return map;
}
}

View File

@@ -36,7 +36,7 @@ import java.util.List;
* @since 1.4.0
*/
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class MyBatisDecryptInterceptor extends AbstractMyBatisInterceptor {
public class MyBatisDecryptInterceptor extends AbstractMyBatisInterceptor implements Interceptor {
private CryptoProperties properties;
@@ -65,6 +65,9 @@ public class MyBatisDecryptInterceptor extends AbstractMyBatisInterceptor {
for (Field field : fieldList) {
IEncryptor encryptor = super.getEncryptor(field.getAnnotation(FieldEncrypt.class));
Object fieldValue = ReflectUtil.getFieldValue(result, field);
if (null == fieldValue) {
continue;
}
// 优先获取自定义对称加密算法密钥,获取不到时再获取全局配置
String password = ObjectUtil.defaultIfBlank(field.getAnnotation(FieldEncrypt.class)
.password(), properties.getPassword());

View File

@@ -17,33 +17,27 @@
package top.continew.starter.security.crypto.core;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.cache.CacheKey;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.SimpleTypeRegistry;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BaseException;
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
import top.continew.starter.security.crypto.encryptor.IEncryptor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字段加密拦截器
@@ -51,54 +45,43 @@ import java.util.*;
* @author Charles7c
* @since 1.4.0
*/
@Intercepts({@Signature(method = "update", type = Executor.class, args = {MappedStatement.class, Object.class}),
@Signature(method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class, CacheKey.class, BoundSql.class}),
@Signature(method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class})})
public class MyBatisEncryptInterceptor extends AbstractMyBatisInterceptor {
public class MyBatisEncryptInterceptor extends AbstractMyBatisInterceptor implements InnerInterceptor {
private CryptoProperties properties;
private static final Pattern PARAM_PAIRS_PATTERN = Pattern
.compile("#\\{ew\\.paramNameValuePairs\\.(" + Constants.WRAPPER_PARAM + "\\d+)\\}");
private final CryptoProperties properties;
public MyBatisEncryptInterceptor(CryptoProperties properties) {
this.properties = properties;
}
public MyBatisEncryptInterceptor() {
@Override
public void beforeQuery(Executor executor,
MappedStatement mappedStatement,
Object parameterObject,
RowBounds rowBounds,
ResultHandler resultHandler,
BoundSql boundSql) {
if (null == parameterObject) {
return;
}
if (parameterObject instanceof Map parameterMap) {
this.encryptQueryParameter(parameterMap, mappedStatement);
}
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement)args[0];
Object parameter = args[1];
if (!this.isEncryptRequired(parameter, mappedStatement.getSqlCommandType())) {
return invocation.proceed();
public void beforeUpdate(Executor executor, MappedStatement mappedStatement, Object parameterObject) {
if (null == parameterObject) {
return;
}
// 使用 @Param 注解的场景
if (parameter instanceof HashMap parameterMap) {
if (parameterObject instanceof Map parameterMap) {
// 带别名方法(使用 @Param 注解的场景)
this.encryptMap(parameterMap, mappedStatement);
} else {
this.doEncrypt(this.getEncryptFields(parameter), parameter);
// 无别名方法例如MP insert 等方法)
this.encryptEntity(super.getEncryptFields(parameterObject), parameterObject);
}
return invocation.proceed();
}
/**
* 是否需要加密处理
*
* @param parameter 参数
* @param sqlCommandType SQL 类型
* @return truefalse
*/
private boolean isEncryptRequired(Object parameter, SqlCommandType sqlCommandType) {
if (ObjectUtil.isEmpty(parameter)) {
return false;
}
if (!(SqlCommandType.UPDATE == sqlCommandType || SqlCommandType.INSERT == sqlCommandType || SqlCommandType.SELECT == sqlCommandType)) {
return false;
}
return !SimpleTypeRegistry.isSimpleType(parameter.getClass());
}
/**
@@ -106,143 +89,132 @@ public class MyBatisEncryptInterceptor extends AbstractMyBatisInterceptor {
*
* @param parameterMap 参数
* @param mappedStatement 映射语句
* @throws Exception /
*/
private void encryptMap(HashMap<String, Object> parameterMap, MappedStatement mappedStatement) throws Exception {
Map<String, FieldEncrypt> encryptParamMap = super.getEncryptParams(mappedStatement);
if (encryptParamMap.isEmpty() && !parameterMap.isEmpty()) {
encryptParamMap = super.getEncryptParams(mappedStatement, parameterMap.size() / 2);
private void encryptMap(Map<String, Object> parameterMap, MappedStatement mappedStatement) {
Object parameter;
// 别名带有 et针对 MP 的 updateById、update 等方法)
if (parameterMap.containsKey(Constants.ENTITY) && null != (parameter = parameterMap.get(Constants.ENTITY))) {
this.encryptEntity(super.getEncryptFields(parameter), parameter);
}
for (Map.Entry<String, FieldEncrypt> encryptParamEntry : encryptParamMap.entrySet()) {
String parameterName = encryptParamEntry.getKey();
if (parameterName.startsWith(Constants.ENTITY)) {
// 兼容 MyBatis Plus 封装的 update 相关方法updateById、update
Object entity = parameterMap.getOrDefault(parameterName, null);
this.doEncrypt(this.getEncryptFields(entity), entity);
} else if (parameterName.startsWith(Constants.WRAPPER)) {
// 处理参数为 Wrapper 的情况
Wrapper wrapper = (Wrapper)parameterMap.getOrDefault(parameterName, null);
this.doEncrypt(wrapper, mappedStatement);
// 别名带有 ew针对 MP 的 UpdateWrapper、LambdaUpdateWrapper 等参数)
if (parameterMap.containsKey(Constants.WRAPPER) && null != (parameter = parameterMap.get(Constants.WRAPPER))) {
this.encryptUpdateWrapper(parameter, mappedStatement);
}
}
/**
* 加密查询参数(针对 Map 类型参数)
*
* @param parameterMap 参数
* @param mappedStatement 映射语句
*/
private void encryptQueryParameter(Map<String, Object> parameterMap, MappedStatement mappedStatement) {
Map<String, FieldEncrypt> encryptParameterMap = super.getEncryptParameters(mappedStatement);
for (Map.Entry<String, Object> parameterEntrySet : parameterMap.entrySet()) {
String parameterName = parameterEntrySet.getKey();
Object parameterValue = parameterEntrySet.getValue();
if (null == parameterValue || ClassUtil.isBasicType(parameterValue
.getClass()) || parameterValue instanceof AbstractWrapper) {
continue;
}
if (parameterValue instanceof String str) {
FieldEncrypt fieldEncrypt = encryptParameterMap.get(parameterName);
if (null != fieldEncrypt) {
parameterMap.put(parameterName, this.doEncrypt(str, fieldEncrypt));
}
} else {
FieldEncrypt fieldEncrypt = encryptParamEntry.getValue();
parameterMap.put(parameterName, this.doEncrypt(parameterMap.get(parameterName), fieldEncrypt));
// 实体参数
this.encryptEntity(super.getEncryptFields(parameterValue), parameterValue);
}
}
}
/**
* 处理加密
* 处理 UpdateWrapper 类型参数加密(针对 MP 的 UpdateWrapper、LambdaUpdateWrapper 等参数)
*
* @param parameter Wrapper 参数
* @param mappedStatement 映射语句
* @since 2.1.1
* @author cary
* @author wangshaopeng@talkweb.com.cn<a
* href="https://blog.csdn.net/tianmaxingkonger/article/details/130986784">基于Mybatis-Plus拦截器实现MySQL数据加解密</a>
*/
private void encryptUpdateWrapper(Object parameter, MappedStatement mappedStatement) {
if (parameter instanceof AbstractWrapper updateWrapper) {
String sqlSet = updateWrapper.getSqlSet();
if (CharSequenceUtil.isBlank(sqlSet)) {
return;
}
// 将 name=#{ew.paramNameValuePairs.xxx},age=#{ew.paramNameValuePairs.xxx} 切出来
String[] elArr = sqlSet.split(StringConstants.COMMA);
Map<String, String> propMap = new HashMap<>(elArr.length);
Arrays.stream(elArr).forEach(el -> {
String[] elPart = el.split(StringConstants.EQUALS);
propMap.put(elPart[0], elPart[1]);
});
// 获取加密字段
Class<?> entityClass = mappedStatement.getParameterMap().getType();
List<Field> encryptFieldList = super.getEncryptFields(entityClass);
for (Field field : encryptFieldList) {
FieldEncrypt fieldEncrypt = field.getAnnotation(FieldEncrypt.class);
String el = propMap.get(field.getName());
if (CharSequenceUtil.isBlank(el)) {
continue;
}
Matcher matcher = PARAM_PAIRS_PATTERN.matcher(el);
if (matcher.matches()) {
String valueKey = matcher.group(1);
Object value = updateWrapper.getParamNameValuePairs().get(valueKey);
Object ciphertext = this.doEncrypt(value, fieldEncrypt);
updateWrapper.getParamNameValuePairs().put(valueKey, ciphertext);
}
}
}
}
/**
* 处理实体加密
*
* @param fieldList 加密字段列表
* @param entity 实体
* @throws Exception /
*/
private void doEncrypt(List<Field> fieldList, Object entity) throws Exception {
private void encryptEntity(List<Field> fieldList, Object entity) {
for (Field field : fieldList) {
IEncryptor encryptor = super.getEncryptor(field.getAnnotation(FieldEncrypt.class));
Object fieldValue = ReflectUtil.getFieldValue(entity, field);
if (null == fieldValue) {
continue;
}
// 优先获取自定义对称加密算法密钥,获取不到时再获取全局配置
String password = ObjectUtil.defaultIfBlank(field.getAnnotation(FieldEncrypt.class).password(), properties
.getPassword());
String ciphertext = encryptor.encrypt(fieldValue.toString(), password, properties.getPublicKey());
String ciphertext;
try {
ciphertext = encryptor.encrypt(fieldValue.toString(), password, properties.getPublicKey());
} catch (Exception e) {
throw new BaseException(e);
}
ReflectUtil.setFieldValue(entity, field, ciphertext);
}
}
/**
* 处理 Wrapper 加密
*
* @param wrapper Wrapper 对象
* @param mappedStatement 映射语句
* @throws Exception /
*/
private void doEncrypt(Wrapper wrapper, MappedStatement mappedStatement) throws Exception {
if (wrapper instanceof AbstractWrapper abstractWrapper) {
String sqlSet = abstractWrapper.getSqlSet();
if (CharSequenceUtil.isEmpty(sqlSet)) {
return;
}
String className = CharSequenceUtil.subBefore(mappedStatement.getId(), StringConstants.DOT, true);
Class<?> mapperClass = Class.forName(className);
Optional<Class> baseMapperGenerics = getEntityTypeByMapperClass(mapperClass, Optional.empty());
// 获取不到泛型对象 则不进行下面的逻辑
if (baseMapperGenerics.isEmpty()) {
return;
}
TableInfo tableInfo = TableInfoHelper.getTableInfo(baseMapperGenerics.get());
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
// 将 name=#{ew.paramNameValuePairs.xxx},age=#{ew.paramNameValuePairs.xxx} 切出来
for (String sqlFragment : sqlSet.split(Constants.COMMA)) {
String columnName = sqlFragment.split(Constants.EQUALS)[0];
// 截取其中的 xxx 字符,例如:#{ew.paramNameValuePairs.xxx}
String paramNameVal = sqlFragment.split(Constants.EQUALS)[1].substring(25, sqlFragment
.split(Constants.EQUALS)[1].length() - 1);
Optional<TableFieldInfo> fieldInfo = fieldList.stream()
.filter(f -> f.getColumn().equals(columnName))
.findAny();
if (fieldInfo.isPresent()) {
TableFieldInfo tableFieldInfo = fieldInfo.get();
FieldEncrypt fieldEncrypt = tableFieldInfo.getField().getAnnotation(FieldEncrypt.class);
if (fieldEncrypt != null) {
Map<String, Object> paramNameValuePairs = abstractWrapper.getParamNameValuePairs();
paramNameValuePairs.put(paramNameVal, this.doEncrypt(paramNameValuePairs
.get(paramNameVal), fieldEncrypt));
}
}
}
}
}
/**
* 处理加密
*
* @param parameterValue 参数值
* @param fieldEncrypt 字段加密注解
* @throws Exception /
*/
private Object doEncrypt(Object parameterValue, FieldEncrypt fieldEncrypt) throws Exception {
private Object doEncrypt(Object parameterValue, FieldEncrypt fieldEncrypt) {
if (null == parameterValue) {
return null;
}
IEncryptor encryptor = super.getEncryptor(fieldEncrypt);
// 优先获取自定义对称加密算法密钥,获取不到时再获取全局配置
String password = ObjectUtil.defaultIfBlank(fieldEncrypt.password(), properties.getPassword());
return encryptor.encrypt(parameterValue.toString(), password, properties.getPublicKey());
}
/**
* 从 Mapper 获取泛型
*
* @param mapperClass Mapper class
* @param tempResult 临时存储的泛型对象
* @return 泛型
*/
private static Optional<Class> getEntityTypeByMapperClass(Class<?> mapperClass, Optional<Class> tempResult) {
Type[] genericInterfaces = mapperClass.getGenericInterfaces();
Optional<Class> result = tempResult;
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType parameterizedType) {
Type rawType = parameterizedType.getRawType();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
// 如果匹配上 BaseMapper 且泛型参数是 Class 类型,则直接返回
if (rawType.equals(BaseMapper.class)) {
return actualTypeArguments[0] instanceof Class
? Optional.of((Class)actualTypeArguments[0])
: result;
} else if (rawType instanceof Class interfaceClass) {
// 如果泛型参数是 Class 类型,则传递给递归调用
if (actualTypeArguments[0] instanceof Class tempResultClass) {
result = Optional.of(tempResultClass);
}
// 递归调用,继续查找
Optional<Class> innerResult = getEntityTypeByMapperClass(interfaceClass, result);
if (innerResult.isPresent()) {
return innerResult;
}
}
}
try {
return encryptor.encrypt(parameterValue.toString(), password, properties.getPublicKey());
} catch (Exception e) {
throw new BaseException(e);
}
// 如果没有找到,返回传递进来的 tempResult
return Optional.empty();
}
}

View File

@@ -16,6 +16,7 @@
package top.continew.starter.web.autoconfigure.response;
import cn.hutool.core.util.ClassUtil;
import org.apache.commons.lang3.reflect.TypeUtils;
import org.springdoc.core.parsers.ReturnTypeParser;
import org.springframework.core.MethodParameter;
@@ -35,7 +36,13 @@ import java.lang.reflect.Type;
*/
public class ApiDocGlobalResponseHandler implements ReturnTypeParser {
private static final Class<R> R_TYPE = R.class;
private final GlobalResponseProperties globalResponseProperties;
private final Class<Object> responseClass;
public ApiDocGlobalResponseHandler(GlobalResponseProperties globalResponseProperties) {
this.globalResponseProperties = globalResponseProperties;
this.responseClass = ClassUtil.loadClass(globalResponseProperties.getResponseClassFullName());
}
/**
* 获取返回类型
@@ -51,15 +58,15 @@ public class ApiDocGlobalResponseHandler implements ReturnTypeParser {
if (!DocUtils.hasRestControllerAnnotation(methodParameter.getContainingClass())) {
return returnType;
}
// 如果为 R<T> 则直接返回
if (returnType.getTypeName().contains("top.continew.starter.web.model.R")) {
// 如果为响应类型,则直接返回
if (returnType.getTypeName().contains(globalResponseProperties.getResponseClassFullName())) {
return returnType;
}
// 如果是 void类型则返回 R<Void>
if (returnType == void.class || returnType == Void.class) {
return TypeUtils.parameterize(R_TYPE, Void.class);
return TypeUtils.parameterize(responseClass, Void.class);
}
// 返回 R<T>
return TypeUtils.parameterize(R_TYPE, returnType);
return TypeUtils.parameterize(responseClass, returnType);
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.web.autoconfigure.response;
import com.feiniaojin.gracefulresponse.advice.lifecycle.exception.BeforeControllerAdviceProcess;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.continew.starter.web.util.SpringWebUtils;
/**
* 默认回调处理器实现
*
* @author Charles7c
* @since 2.6.0
*/
public class DefaultBeforeControllerAdviceProcessImpl implements BeforeControllerAdviceProcess {
private final Logger log = LoggerFactory.getLogger(DefaultBeforeControllerAdviceProcessImpl.class);
private final GlobalResponseProperties globalResponseProperties;
public DefaultBeforeControllerAdviceProcessImpl(GlobalResponseProperties globalResponseProperties) {
this.globalResponseProperties = globalResponseProperties;
}
@Override
public void call(Throwable throwable) {
if (globalResponseProperties.isPrintExceptionInGlobalAdvice()) {
HttpServletRequest request = SpringWebUtils.getRequest();
log.error("[{}] {}", request.getMethod(), request.getRequestURI(), throwable);
}
}
}

View File

@@ -18,6 +18,9 @@ package top.continew.starter.web.autoconfigure.response;
import com.feiniaojin.gracefulresponse.ExceptionAliasRegister;
import com.feiniaojin.gracefulresponse.advice.*;
import com.feiniaojin.gracefulresponse.advice.lifecycle.exception.BeforeControllerAdviceProcess;
import com.feiniaojin.gracefulresponse.advice.lifecycle.exception.ControllerAdvicePredicate;
import com.feiniaojin.gracefulresponse.advice.lifecycle.response.ResponseBodyAdvicePredicate;
import com.feiniaojin.gracefulresponse.api.ResponseFactory;
import com.feiniaojin.gracefulresponse.api.ResponseStatusFactory;
import com.feiniaojin.gracefulresponse.defaults.DefaultResponseFactory;
@@ -37,6 +40,7 @@ import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import java.util.Locale;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 全局响应自动配置
@@ -50,23 +54,10 @@ import java.util.Locale;
public class GlobalResponseAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(GlobalResponseAutoConfiguration.class);
private final GlobalResponseProperties globalResponseProperties;
/**
* 全局异常处理
*/
@Bean
@ConditionalOnMissingBean
public GrGlobalExceptionAdvice globalExceptionAdvice() {
return new GrGlobalExceptionAdvice();
}
/**
* 全局校验异常处理
*/
@Bean
@ConditionalOnMissingBean
public GrValidationExceptionAdvice validationExceptionAdvice() {
return new GrValidationExceptionAdvice();
public GlobalResponseAutoConfiguration(GlobalResponseProperties globalResponseProperties) {
this.globalResponseProperties = globalResponseProperties;
}
/**
@@ -74,8 +65,13 @@ public class GlobalResponseAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public GrNotVoidResponseBodyAdvice notVoidResponseBodyAdvice() {
return new GrNotVoidResponseBodyAdvice();
public GrNotVoidResponseBodyAdvice grNotVoidResponseBodyAdvice() {
GrNotVoidResponseBodyAdvice notVoidResponseBodyAdvice = new GrNotVoidResponseBodyAdvice();
CopyOnWriteArrayList<ResponseBodyAdvicePredicate> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
copyOnWriteArrayList.add(notVoidResponseBodyAdvice);
notVoidResponseBodyAdvice.setPredicates(copyOnWriteArrayList);
notVoidResponseBodyAdvice.setResponseBodyAdviceProcessor(notVoidResponseBodyAdvice);
return notVoidResponseBodyAdvice;
}
/**
@@ -83,8 +79,104 @@ public class GlobalResponseAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public GrVoidResponseBodyAdvice voidResponseBodyAdvice() {
return new GrVoidResponseBodyAdvice();
public GrVoidResponseBodyAdvice grVoidResponseBodyAdvice() {
GrVoidResponseBodyAdvice voidResponseBodyAdvice = new GrVoidResponseBodyAdvice();
CopyOnWriteArrayList<ResponseBodyAdvicePredicate> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
copyOnWriteArrayList.add(voidResponseBodyAdvice);
voidResponseBodyAdvice.setPredicates(copyOnWriteArrayList);
voidResponseBodyAdvice.setResponseBodyAdviceProcessor(voidResponseBodyAdvice);
return voidResponseBodyAdvice;
}
/**
* 处理前回调(目前仅打印异常日志)
*/
@Bean
@ConditionalOnMissingBean
public BeforeControllerAdviceProcess beforeControllerAdviceProcess() {
return new DefaultBeforeControllerAdviceProcessImpl(globalResponseProperties);
}
/**
* 框架异常处理器
*/
@Bean
public FrameworkExceptionAdvice frameworkExceptionAdvice(BeforeControllerAdviceProcess beforeControllerAdviceProcess) {
FrameworkExceptionAdvice frameworkExceptionAdvice = new FrameworkExceptionAdvice();
frameworkExceptionAdvice.setRejectStrategy(new DefaultRejectStrategyImpl());
frameworkExceptionAdvice.setControllerAdviceProcessor(frameworkExceptionAdvice);
frameworkExceptionAdvice.setBeforeControllerAdviceProcess(beforeControllerAdviceProcess);
frameworkExceptionAdvice.setControllerAdviceHttpProcessor(frameworkExceptionAdvice);
return frameworkExceptionAdvice;
}
/**
* 数据校验异常处理器
*/
@Bean
public DataExceptionAdvice dataExceptionAdvice(BeforeControllerAdviceProcess beforeControllerAdviceProcess) {
DataExceptionAdvice dataExceptionAdvice = new DataExceptionAdvice();
dataExceptionAdvice.setRejectStrategy(new DefaultRejectStrategyImpl());
dataExceptionAdvice.setControllerAdviceProcessor(dataExceptionAdvice);
dataExceptionAdvice.setBeforeControllerAdviceProcess(beforeControllerAdviceProcess);
dataExceptionAdvice.setControllerAdviceHttpProcessor(dataExceptionAdvice);
return dataExceptionAdvice;
}
/**
* 默认全局异常处理器
*/
@Bean
public DefaultGlobalExceptionAdvice defaultGlobalExceptionAdvice(BeforeControllerAdviceProcess beforeControllerAdviceProcess) {
DefaultGlobalExceptionAdvice advice = new DefaultGlobalExceptionAdvice();
advice.setRejectStrategy(new DefaultRejectStrategyImpl());
CopyOnWriteArrayList<ControllerAdvicePredicate> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
copyOnWriteArrayList.add(advice);
advice.setPredicates(copyOnWriteArrayList);
advice.setControllerAdviceProcessor(advice);
advice.setBeforeControllerAdviceProcess(beforeControllerAdviceProcess);
advice.setControllerAdviceHttpProcessor(advice);
return advice;
}
/**
* 默认参数校验异常处理器
*/
@Bean
public DefaultValidationExceptionAdvice defaultValidationExceptionAdvice(BeforeControllerAdviceProcess beforeControllerAdviceProcess) {
DefaultValidationExceptionAdvice advice = new DefaultValidationExceptionAdvice();
advice.setRejectStrategy(new DefaultRejectStrategyImpl());
advice.setControllerAdviceProcessor(advice);
advice.setBeforeControllerAdviceProcess(beforeControllerAdviceProcess);
advice.setControllerAdviceHttpProcessor(advice);
return advice;
}
/**
* 国际化支持
*/
@Bean
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
public GrI18nResponseBodyAdvice grI18nResponseBodyAdvice() {
GrI18nResponseBodyAdvice i18nResponseBodyAdvice = new GrI18nResponseBodyAdvice();
CopyOnWriteArrayList<ResponseBodyAdvicePredicate> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
copyOnWriteArrayList.add(i18nResponseBodyAdvice);
i18nResponseBodyAdvice.setPredicates(copyOnWriteArrayList);
i18nResponseBodyAdvice.setResponseBodyAdviceProcessor(i18nResponseBodyAdvice);
return i18nResponseBodyAdvice;
}
/**
* 国际化配置
*/
@Bean
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("i18n", "i18n/empty-messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setDefaultLocale(Locale.CHINA);
return messageSource;
}
/**
@@ -121,28 +213,6 @@ public class GlobalResponseAutoConfiguration {
return new AdviceSupport();
}
/**
* 国际化支持
*/
@Bean
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
public GrI18nAdvice i18nAdvice() {
return new GrI18nAdvice();
}
/**
* 国际化配置
*/
@Bean
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("i18n", "i18n/empty-messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setDefaultLocale(Locale.CHINA);
return messageSource;
}
/**
* SpringDoc 全局响应处理器
*
@@ -151,7 +221,7 @@ public class GlobalResponseAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ApiDocGlobalResponseHandler apiDocGlobalResponseHandler() {
return new ApiDocGlobalResponseHandler();
return new ApiDocGlobalResponseHandler(globalResponseProperties);
}
@PostConstruct

View File

@@ -52,9 +52,9 @@ public class TLogServletFilter implements Filter {
try {
TLogWebCommon.loadInstance().preHandle(httpServletRequest);
// 把 traceId 放入 response 的 header为了方便有些人有这样的需求从前端拿整条链路的 traceId
String headerName = traceProperties.getHeaderName();
if (CharSequenceUtil.isNotBlank(headerName)) {
httpServletResponse.addHeader(headerName, TLogContext.getTraceId());
String traceIdName = traceProperties.getTraceIdName();
if (CharSequenceUtil.isNotBlank(traceIdName)) {
httpServletResponse.addHeader(traceIdName, TLogContext.getTraceId());
}
chain.doFilter(request, response);
} finally {

View File

@@ -35,9 +35,9 @@ public class TraceProperties {
private boolean enabled = false;
/**
* 响应头名称
* 链路 ID 名称
*/
private String headerName = "traceId";
private String traceIdName = "traceId";
/**
* TLog 配置
@@ -53,12 +53,12 @@ public class TraceProperties {
this.enabled = enabled;
}
public String getHeaderName() {
return headerName;
public String getTraceIdName() {
return traceIdName;
}
public void setHeaderName(String headerName) {
this.headerName = headerName;
public void setTraceIdName(String traceIdName) {
this.traceIdName = traceIdName;
}
public TLogProperties getTlog() {

View File

@@ -21,9 +21,7 @@ import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.PathContainer;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import top.continew.starter.web.util.SpringWebUtils;
import java.io.IOException;
import java.util.List;
@@ -57,14 +55,15 @@ public class XssFilter implements Filter {
if (servletRequest instanceof HttpServletRequest request && xssProperties.isEnabled()) {
// 放行路由:忽略 XSS 过滤
List<String> excludePatterns = xssProperties.getExcludePatterns();
if (CollectionUtil.isNotEmpty(excludePatterns) && isMatchPath(request.getServletPath(), excludePatterns)) {
if (CollectionUtil.isNotEmpty(excludePatterns) && SpringWebUtils.isMatch(request
.getServletPath(), excludePatterns)) {
filterChain.doFilter(request, servletResponse);
return;
}
// 拦截路由:执行 XSS 过滤
List<String> includePatterns = xssProperties.getIncludePatterns();
if (CollectionUtil.isNotEmpty(includePatterns)) {
if (isMatchPath(request.getServletPath(), includePatterns)) {
if (SpringWebUtils.isMatch(request.getServletPath(), includePatterns)) {
filterChain.doFilter(new XssServletRequestWrapper(request, xssProperties), servletResponse);
} else {
filterChain.doFilter(request, servletResponse);
@@ -77,22 +76,4 @@ public class XssFilter implements Filter {
}
filterChain.doFilter(servletRequest, servletResponse);
}
/**
* 判断数组中是否存在匹配的路径
*
* @param requestUrl 请求地址
* @param pathPatterns 指定匹配路径
* @return true匹配false不匹配
*/
private static boolean isMatchPath(String requestUrl, List<String> pathPatterns) {
for (String pattern : pathPatterns) {
PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern);
PathContainer pathContainer = PathContainer.parsePath(requestUrl);
if (pathPattern.matches(pathContainer)) {
return true;
}
}
return false;
}
}

View File

@@ -16,8 +16,8 @@
package top.continew.starter.web.util;
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 jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
@@ -35,6 +35,8 @@ import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import top.continew.starter.core.constant.StringConstants;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -70,12 +72,36 @@ public class SpringWebUtils {
/**
* 路径是否匹配
*
* @param pattern 匹配模式
* @param path 路径
* @param patterns 匹配模式列表
* @return 是否匹配
* @since 2.6.0
*/
public static boolean isMatch(String path, List<String> patterns) {
return patterns.stream().anyMatch(pattern -> isMatch(path, pattern));
}
/**
* 路径是否匹配
*
* @param path 路径
* @param patterns 匹配模式列表
* @return 是否匹配
* @since 2.6.0
*/
public static boolean isMatch(String path, String... patterns) {
return Arrays.stream(patterns).anyMatch(pattern -> isMatch(path, pattern));
}
/**
* 路径是否匹配
*
* @param path 路径
* @param pattern 匹配模式
* @return 是否匹配
* @since 2.4.0
*/
public static boolean match(String pattern, String path) {
public static boolean isMatch(String path, String pattern) {
PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern);
PathContainer pathContainer = PathContainer.parsePath(path);
return pathPattern.matches(pathContainer);

View File

@@ -4,6 +4,8 @@ continew-starter.web.response:
i18n: false
# 响应类全名(配置后 response-style 将不再生效)
response-class-full-name: top.continew.starter.web.model.R
# 自定义失败 HTTP 状态码默认200建议业务和通信状态码区分
default-http-status-code-on-error: 200
# 自定义成功响应码默认0
default-success-code: 0
# 自定义成功提示默认ok