Compare commits

...

11 Commits

Author SHA1 Message Date
bootvue 12fe9e73b8
Merge a292adddc6 into 147288799a 2024-03-10 00:33:33 +08:00
卢春梦 147288799a 完善 DigestUtil 2024-03-09 16:55:25 +08:00
smallchill c57ece80ee 🎉 3.7.2.1 2024-03-09 16:51:40 +08:00
smallchill 253d2ae99c 🎉 新增工具类 2024-03-09 16:30:14 +08:00
smallchill 8cfbadaa60 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	blade-core-boot/pom.xml
#	blade-core-cloud/pom.xml
#	blade-core-crypto/pom.xml
#	blade-core-datascope/pom.xml
#	blade-core-develop/pom.xml
#	blade-core-launch/pom.xml
#	blade-core-loadbalancer/pom.xml
#	blade-core-log/pom.xml
#	blade-core-mybatis/pom.xml
#	blade-core-oss/pom.xml
#	blade-core-report/pom.xml
#	blade-core-secure/pom.xml
#	blade-core-social/pom.xml
#	blade-core-swagger/pom.xml
#	blade-core-test/pom.xml
#	blade-core-tool/pom.xml
#	blade-core-transaction/pom.xml
#	pom.xml
2024-03-09 16:24:07 +08:00
卢春梦 5159d93296 使用 flatten-maven-plugin 优化项目 2024-03-09 16:20:29 +08:00
卢春梦 84f5f218b7 使用 flatten-maven-plugin 优化项目 2024-03-09 16:16:59 +08:00
smallchill d70692931a Merge remote-tracking branch 'origin/master' 2024-03-09 16:08:54 +08:00
卢春梦 55b092665b 完善工具类 2024-03-09 15:45:00 +08:00
smallchill 42249abb17 🎉 3.7.2.RELEASE 2024-02-28 05:42:39 +08:00
bootvue a292adddc6 fix client query sql 2023-10-10 13:47:23 +00:00
37 changed files with 1529 additions and 90 deletions

13
.github/ISSUE_TEMPLATE vendored Normal file
View File

@ -0,0 +1,13 @@
**注意github 上的少,可能响应不够及时,建议 gitee 上发起**
Please answer some questions before submitting your issue. Thanks!
### Which version do you using使用的哪个版本?
### Expected behavior预期的效果
### Actual behavior实际的效果
### Steps to reproduce the behavior重现步骤
### Other information其他信息

16
.github/PULL_REQUEST_TEMPLATE vendored Normal file
View File

@ -0,0 +1,16 @@
**注意github 上的少,可能响应不够及时,建议 gitee 上发起**
**What kind of change does this PR introduce?** (check at least one)
- [ ] Bugfixbug修复
- [ ] Feature新功能
- [ ] Code style update代码格式调整
- [ ] Refactor重构
- [ ] Build-related changes与构建相关的更改
- [ ] Other, please describe其他请说明:
**The description of the PRPR描述:**
**Other information其他信息:**

17
.github/workflows/test-and-build.yml vendored Normal file
View File

@ -0,0 +1,17 @@
name: Java CI
on: [push, pull_request]
jobs:
buildOnJava8:
name: build on java8
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
distribution: 'zulu'
cache: 'maven'
java-version: 8
- name: Build with Maven
run: mvn package

4
.gitignore vendored
View File

@ -25,3 +25,7 @@ Thumbs.db
*.war
*.ear
/target
# Flattened pom
.flattened-pom.xml
/**/.flattened-pom.xml

38
blade-core-bom/pom.xml Normal file
View File

@ -0,0 +1,38 @@
<?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">
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-bom</artifactId>
<name>${project.artifactId}</name>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${maven-flatten.version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>oss</flattenMode>
<pomElements>
<dependencyManagement>expand</dependencyManagement>
<pluginManagement>remove</pluginManagement>
<dependencies>remove</dependencies>
<properties>remove</properties>
<repositories>remove</repositories>
</pomElements>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -5,14 +5,13 @@
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-boot</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -35,38 +34,31 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-secure</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-log</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-swagger</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-mybatis</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>

View File

@ -101,7 +101,7 @@ mybatis-plus:
swagger:
title: SpringBlade 接口文档系统
description: SpringBlade 接口文档系统
version: 3.7.1
version: 3.7.2
license: Powered By SpringBlade
licenseUrl: https://bladex.vip
terms-of-service-url: https://bladex.vip

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-cloud</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
@ -20,7 +19,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-secure</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Feign-->
<dependency>

View File

@ -5,20 +5,18 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-crypto</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-datascope</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,7 +18,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-mybatis</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Jdbc-->
<dependency>

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-develop</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,7 +18,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Mybatis-Plus-->
<dependency>

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-launch</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>

View File

@ -25,7 +25,7 @@ public interface AppConstant {
/**
* 应用版本
*/
String APPLICATION_VERSION = "3.7.1";
String APPLICATION_VERSION = "3.7.2";
/**
* 基础包

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-loadbalancer</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-log</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -20,17 +19,14 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-secure</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Mybatis-->
<dependency>

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-mybatis</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -36,7 +35,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-secure</artifactId>
<version>${blade.tool.version}</version>
</dependency>
</dependencies>

View File

@ -16,6 +16,8 @@
package org.springblade.core.mp.base;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@ -36,6 +38,14 @@ import java.util.Date;
@Data
public class BaseEntity implements Serializable {
/**
* 主键
*/
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.ASSIGN_ID)
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
/**
* 创建人
*/
@ -43,6 +53,13 @@ public class BaseEntity implements Serializable {
@ApiModelProperty(value = "创建人")
private Long createUser;
/**
* 创建部门
*/
@JsonSerialize(using = ToStringSerializer.class)
@ApiModelProperty(value = "创建部门")
private Long createDept;
/**
* 创建时间
*/

View File

@ -15,6 +15,7 @@
*/
package org.springblade.core.mp.base;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import javax.validation.constraints.NotEmpty;
@ -36,4 +37,14 @@ public interface BaseService<T> extends IService<T> {
*/
boolean deleteLogic(@NotEmpty List<Long> ids);
/**
* 判断字段是否重复
*
* @param field 字段
* @param value 字段值
* @param excludedId 排除的id
* @return boolean
*/
boolean isFieldDuplicate(SFunction<T, ?> field, Object value, Long excludedId);
}

View File

@ -15,8 +15,12 @@
*/
package org.springblade.core.mp.base;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.constant.BladeConstant;
@ -24,6 +28,7 @@ import org.springblade.core.tool.utils.DateUtil;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@ -39,6 +44,45 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
public boolean save(T entity) {
this.resolveSave(entity);
return super.save(entity);
}
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
entityList.forEach(this::resolveSave);
return super.saveBatch(entityList, batchSize);
}
@Override
public boolean updateById(T entity) {
this.resolveUpdate(entity);
return super.updateById(entity);
}
@Override
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
entityList.forEach(this::resolveUpdate);
return super.updateBatchById(entityList, batchSize);
}
@Override
public boolean deleteLogic(@NotEmpty List<Long> ids) {
return super.removeByIds(ids);
}
@Override
public boolean isFieldDuplicate(SFunction<T, ?> field, Object value, Long excludedId) {
LambdaQueryWrapper<T> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(field, value);
if (excludedId != null) {
queryWrapper.ne(T::getId, excludedId);
}
return baseMapper.selectCount(queryWrapper) > 0;
}
@SneakyThrows
private void resolveSave(T entity) {
BladeUser user = SecureUtil.getUser();
if (user != null) {
entity.setCreateUser(user.getUserId());
@ -51,22 +95,15 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
entity.setStatus(BladeConstant.DB_STATUS_NORMAL);
}
entity.setIsDeleted(BladeConstant.DB_NOT_DELETED);
return super.save(entity);
}
@Override
public boolean updateById(T entity) {
@SneakyThrows
private void resolveUpdate(T entity) {
BladeUser user = SecureUtil.getUser();
if (user != null) {
entity.setUpdateUser(user.getUserId());
}
entity.setUpdateTime(DateUtil.now());
return super.updateById(entity);
}
@Override
public boolean deleteLogic(@NotEmpty List<Long> ids) {
return super.removeByIds(ids);
}
}

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-oss</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,7 +18,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Aliyun-->
<dependency>

View File

@ -5,20 +5,18 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-report</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-mybatis</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>com.bstek.ureport</groupId>

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-secure</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -31,7 +30,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Jdbc-->
<dependency>

View File

@ -55,6 +55,6 @@ public interface SecureConstant {
/**
* 查询client_id
*/
String DEFAULT_SELECT_STATEMENT = BASE_STATEMENT + " where client_id = ?";
String DEFAULT_SELECT_STATEMENT = BASE_STATEMENT + " where client_id = ? and is_deleted = 0";
}

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-social</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,7 +18,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!-- 第三方登陆 -->
<dependency>

View File

@ -5,14 +5,13 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-swagger</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -20,7 +19,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!--Swagger-->
<dependency>

View File

@ -55,7 +55,7 @@ public class SwaggerProperties {
/**
* 版本
**/
private String version = "3.7.1";
private String version = "3.7.2";
/**
* 许可证
**/

View File

@ -5,13 +5,12 @@
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-test</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,7 +18,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!-- Test -->
<dependency>

View File

@ -2,18 +2,16 @@
<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">
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-tool</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -21,7 +19,6 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>

View File

@ -21,16 +21,51 @@ package org.springblade.core.tool.utils;
* @author L.cm
*/
public interface CharPool {
char UPPER_A = 'A';
char LOWER_A = 'a';
char UPPER_Z = 'Z';
char LOWER_Z = 'z';
char DOT = '.';
char AT = '@';
char LEFT_BRACE = '{';
char RIGHT_BRACE = '}';
char LEFT_BRACKET = '(';
char RIGHT_BRACKET = ')';
// @formatter:off
char UPPER_A = 'A';
char LOWER_A = 'a';
char UPPER_Z = 'Z';
char LOWER_Z = 'z';
char DOT = '.';
char AT = '@';
char LEFT_BRACE = '{';
char RIGHT_BRACE = '}';
char LEFT_BRACKET = '(';
char RIGHT_BRACKET = ')';
char DASH = '-';
char PERCENT = '%';
char PIPE = '|';
char PLUS = '+';
char QUESTION_MARK = '?';
char EXCLAMATION_MARK = '!';
char EQUALS = '=';
char AMPERSAND = '&';
char ASTERISK = '*';
char STAR = ASTERISK;
char BACK_SLASH = '\\';
char COLON = ':';
char COMMA = ',';
char DOLLAR = '$';
char SLASH = '/';
char HASH = '#';
char HAT = '^';
char LEFT_CHEV = '<';
char NEWLINE = '\n';
char N = 'n';
char Y = 'y';
char QUOTE = '\"';
char RETURN = '\r';
char TAB = '\t';
char RIGHT_CHEV = '>';
char SEMICOLON = ';';
char SINGLE_QUOTE = '\'';
char BACKTICK = '`';
char SPACE = ' ';
char TILDA = '~';
char LEFT_SQ_BRACKET = '[';
char RIGHT_SQ_BRACKET = ']';
char UNDERSCORE = '_';
char ONE = '1';
char ZERO = '0';
// @formatter:on
}

View File

@ -0,0 +1,295 @@
/**
* Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com).
* <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 org.springblade.core.tool.utils;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import java.util.Arrays;
/**
* 脱敏工具类
*
* @author Katrel同事
* @author L.cm
*/
public class DesensitizationUtil {
/**
* [中文姓名] 只显示第一个汉字其他隐藏为2个星号(例子**)
*
* @param fullName 全名
* @return 脱敏后的字符串
*/
@Nullable
public static String chineseName(@Nullable final String fullName) {
return sensitive(fullName, 1, 0);
}
/**
* [身份证号] 显示最后四位其他隐藏共计18位或者15位(例子*************5762)
*
* @param id 身份证号
* @return 脱敏后的字符串
*/
@Nullable
public static String idCardNum(@Nullable final String id) {
return sensitive(id, 0, 4);
}
/**
* [固定电话] 后四位其他隐藏(例子****1234)
*
* @param num 固定电话号
* @return 脱敏后的字符串
*/
@Nullable
public static String phoneNo(@Nullable final String num) {
return sensitive(num, 0, 4);
}
/**
* [手机号码] 前三位后四位其他隐藏(例子:138****1234)
*
* @param num 手机号
* @return 脱敏后的字符串
*/
@Nullable
public static String mobileNo(@Nullable final String num) {
return sensitive(num, 3, 4);
}
/**
* [地址] 只显示到地区不显示详细地址我们要对个人信息增强保护(例子北京市海淀区****)
*
* @param address 地区
* @param sensitiveSize 敏感信息长度
* @return 脱敏后的字符串
*/
@Nullable
public static String address(@Nullable final String address, final int sensitiveSize) {
return sensitive(address, 0, sensitiveSize);
}
/**
* [电子邮箱] 邮箱前缀仅显示第一个字母前缀其他隐藏用星号代替@及后面的地址显示(例子:g**@163.com)
*
* @param email 邮箱
* @return 脱敏后的字符串
*/
@Nullable
public static String email(@Nullable final String email) {
if (email == null) {
return null;
}
if (!StringUtils.hasText(email)) {
return StringPool.EMPTY;
}
final int index = email.indexOf(CharPool.AT);
if (index <= 1) {
return email;
} else {
return sensitive(email, 1, email.length() - index);
}
}
/**
* [银行卡号] 前六位后四位其他用星号隐藏每位1个星号(例子:622260***********1234)
*
* @param cardNum 银行卡号
* @return 脱敏后的字符串
*/
@Nullable
public static String bankCard(@Nullable final String cardNum) {
return sensitive(cardNum, 6, 4);
}
/**
* [公司开户银行联号] 公司开户银行联行号,显示前两位其他用星号隐藏每位1个星号(例子:12********)
*
* @param code 银行联行号
* @return 脱敏后的字符串
*/
@Nullable
public static String cnApsCode(@Nullable final String code) {
return sensitive(code, 2, 0);
}
/**
* 右边脱敏
*
* @param sensitiveStr 待脱敏的字符串
* @return 脱敏后的字符串
*/
@Nullable
public static String right(@Nullable final String sensitiveStr) {
if (sensitiveStr == null) {
return null;
}
if (!StringUtils.hasText(sensitiveStr)) {
return StringPool.EMPTY;
}
int length = sensitiveStr.length();
return sensitive(sensitiveStr, length / 2, 0);
}
/**
* 左边脱敏
*
* @param sensitiveStr 待脱敏的字符串
* @return 脱敏后的字符串
*/
@Nullable
public static String left(@Nullable final String sensitiveStr) {
if (sensitiveStr == null) {
return null;
}
if (!StringUtils.hasText(sensitiveStr)) {
return StringPool.EMPTY;
}
int length = sensitiveStr.length();
return sensitive(sensitiveStr, 0, length / 2);
}
/**
* 中间脱敏保留两端
*
* @param sensitiveStr 待脱敏的字符串
* @return 脱敏后的字符串
*/
@Nullable
public static String middle(@Nullable final String sensitiveStr) {
if (sensitiveStr == null) {
return null;
}
if (!StringUtils.hasText(sensitiveStr)) {
return StringPool.EMPTY;
}
int length = sensitiveStr.length();
if (length < 3) {
return StringUtil.leftPad(StringPool.EMPTY, length, CharPool.STAR);
} else if (length < 6) {
// 小于6个字符脱敏中间
char[] chars = new char[length];
int last = length - 1;
Arrays.fill(chars, 1, last, CharPool.STAR);
chars[0] = sensitiveStr.charAt(0);
chars[last] = sensitiveStr.charAt(last);
return new String(chars);
} else {
// 大于6个字符
int fromLastLen = length / 3;
return sensitive(sensitiveStr, fromLastLen, fromLastLen);
}
}
/**
* 全部脱敏
*
* @param sensitiveStr 待脱敏的字符串
* @return 脱敏后的字符串
*/
@Nullable
public static String all(@Nullable final String sensitiveStr) {
return sensitive(sensitiveStr, 0, 0);
}
/**
* 文本脱敏
*
* @param str 字符串
* @param fromIndex 开始的索引
* @param lastSize 尾部长度
* @return 脱敏后的字符串
*/
@Nullable
public static String sensitive(@Nullable String str, int fromIndex, int lastSize) {
return sensitive(str, fromIndex, lastSize, CharPool.STAR);
}
/**
* 文本脱敏
*
* @param str 字符串
* @param fromIndex 开始的索引
* @param lastSize 尾部长度
* @param padSize 填充的长度
* @return 脱敏后的字符串
*/
@Nullable
public static String sensitive(@Nullable String str, int fromIndex, int lastSize, int padSize) {
return sensitive(str, fromIndex, lastSize, CharPool.STAR, padSize);
}
/**
* 文本脱敏
*
* @param str 字符串
* @param fromIndex 开始的索引
* @param lastSize 尾部长度
* @param padChar 填充的字符
* @return 脱敏后的字符串
*/
@Nullable
public static String sensitive(@Nullable String str, int fromIndex, int lastSize, char padChar) {
return sensitive(str, fromIndex, lastSize, padChar, -1);
}
/**
* 文本脱敏
*
* @param str 字符串
* @param fromIndex 开始的索引
* @param lastSize 尾部长度
* @param padChar 填充的字符
* @param padSize 填充的长度
* @return 脱敏后的字符串
*/
@Nullable
public static String sensitive(@Nullable String str, int fromIndex, int lastSize, char padChar, int padSize) {
if (str == null) {
return null;
}
if (!StringUtils.hasText(str)) {
return StringPool.EMPTY;
}
int length = str.length();
// 全部脱敏
if (fromIndex == 0 && lastSize == 0) {
int padSiz = padSize > 0 ? padSize : length;
return StringUtil.repeat(CharPool.STAR, padSiz);
}
int toIndex = length - lastSize;
int padSiz = padSize > 0 ? padSize : toIndex - fromIndex;
// 头部脱敏
if (fromIndex == 0) {
String tail = str.substring(toIndex);
return StringUtil.repeat(padChar, padSiz).concat(tail);
}
// 尾部脱敏
if (toIndex == length) {
String head = str.substring(0, fromIndex);
return head.concat(StringUtil.repeat(padChar, padSiz));
}
// 中部
String head = str.substring(0, fromIndex);
String tail = str.substring(toIndex);
return head + StringUtil.repeat(padChar, padSiz) + tail;
}
}

View File

@ -17,6 +17,11 @@ package org.springblade.core.tool.utils;
import org.springframework.lang.Nullable;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -65,6 +70,331 @@ public class DigestUtil extends org.springframework.util.DigestUtils {
return hash("SHA-512", srcStr);
}
/**
* hmacMd5
*
* @param data Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacMd5(String data, String key) {
return DigestUtil.hmacMd5(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacMd5
*
* @param bytes Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacMd5(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacMD5", bytes, key);
}
/**
* hmacMd5 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacMd5Hex(String data, String key) {
return DigestUtil.encodeHex(hmacMd5(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacMd5 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacMd5Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacMd5(bytes, key));
}
/**
* hmacSha1
*
* @param data Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha1(String data, String key) {
return DigestUtil.hmacSha1(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacSha1
*
* @param bytes Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha1(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacSHA1", bytes, key);
}
/**
* hmacSha1 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha1Hex(String data, String key) {
return DigestUtil.encodeHex(hmacSha1(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacSha1 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha1Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacSha1(bytes, key));
}
/**
* hmacSha224
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static byte[] hmacSha224(String data, String key) {
return DigestUtil.hmacSha224(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacSha224
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static byte[] hmacSha224(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacSHA224", bytes, key);
}
/**
* hmacSha224 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha224Hex(String data, String key) {
return DigestUtil.encodeHex(hmacSha224(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacSha224 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha224Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacSha224(bytes, key));
}
/**
* hmacSha256
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static byte[] hmacSha256(String data, String key) {
return DigestUtil.hmacSha256(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacSha256
*
* @param bytes Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha256(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacSHA256", bytes, key);
}
/**
* hmacSha256 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a byte array
*/
public static String hmacSha256Hex(String data, String key) {
return DigestUtil.encodeHex(hmacSha256(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacSha256 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha256Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacSha256(bytes, key));
}
/**
* hmacSha384
*
* @param data Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha384(String data, String key) {
return DigestUtil.hmacSha384(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacSha384
*
* @param bytes Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha384(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacSHA384", bytes, key);
}
/**
* hmacSha384 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha384Hex(String data, String key) {
return DigestUtil.encodeHex(hmacSha384(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacSha384 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha384Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacSha384(bytes, key));
}
/**
* hmacSha512
*
* @param data Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha512(String data, String key) {
return DigestUtil.hmacSha512(data.getBytes(Charsets.UTF_8), key);
}
/**
* hmacSha512
*
* @param bytes Data to digest
* @param key key
* @return digest as a byte array
*/
public static byte[] hmacSha512(final byte[] bytes, String key) {
return DigestUtil.digestHmac("HmacSHA512", bytes, key);
}
/**
* hmacSha512 Hex
*
* @param data Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha512Hex(String data, String key) {
return DigestUtil.encodeHex(hmacSha512(data.getBytes(Charsets.UTF_8), key));
}
/**
* hmacSha512 Hex
*
* @param bytes Data to digest
* @param key key
* @return digest as a hex string
*/
public static String hmacSha512Hex(final byte[] bytes, String key) {
return DigestUtil.encodeHex(hmacSha512(bytes, key));
}
/**
* digest Hmac Hex
*
* @param algorithm 算法
* @param text text
* @return digest as a hex string
*/
public static String digestHmacHex(String algorithm, String text, String key) {
return digestHmacHex(algorithm, text.getBytes(StandardCharsets.UTF_8), key);
}
/**
* digest Hmac Hex
*
* @param algorithm 算法
* @param bytes Data to digest
* @return digest as a hex string
*/
public static String digestHmacHex(String algorithm, final byte[] bytes, String key) {
return DigestUtil.encodeHex(DigestUtil.digestHmac(algorithm, bytes, key));
}
/**
* digest Hmac
*
* @param algorithm 算法
* @param bytes Data to digest
* @return digest as a byte array
*/
public static byte[] digestHmac(String algorithm, final byte[] bytes, String key) {
SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);
try {
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(bytes);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw Exceptions.unchecked(e);
}
}
/**
* encode Hex
*
* @param bytes Data to Hex
* @return bytes as a hex string
*/
public static String encodeHex(byte[] bytes) {
return HexUtil.encodeToString(bytes);
}
/**
* decode Hex
*
* @param hexStr Hex string
* @return decode hex to bytes
*/
public static byte[] decodeHex(final String hexStr) {
return HexUtil.decode(hexStr);
}
public static String hash(String algorithm, String srcStr) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);

View File

@ -19,6 +19,7 @@ import org.springblade.core.tool.support.StrFormatter;
import org.springblade.core.tool.support.StrSpliter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;
import java.io.StringReader;
@ -36,6 +37,10 @@ import java.util.stream.Stream;
public class StringUtil extends org.springframework.util.StringUtils {
public static final int INDEX_NOT_FOUND = -1;
/**
* <p>The maximum size to which the padding constant(s) can expand.</p>
*/
private static final int PAD_LIMIT = 8192;
/**
* Check whether the given {@code CharSequence} contains actual <em>text</em>.
@ -1473,5 +1478,396 @@ public class StringUtil extends org.springframework.util.StringUtils {
}
/**
* 参考自 commons lang 微调
*
* <p>Returns padding using the specified delimiter repeated
* to a given length.</p>
*
* <pre>
* StringUtils.repeat('e', 0) = ""
* StringUtils.repeat('e', 3) = "eee"
* StringUtils.repeat('e', -2) = ""
* </pre>
*
* <p>Note: this method does not support padding with
* <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a>
* as they require a pair of {@code char}s to be represented.
* </p>
*
* @param ch character to repeat
* @param repeat number of times to repeat char, negative treated as zero
* @return String with repeated character
*/
public static String repeat(final char ch, final int repeat) {
if (repeat <= 0) {
return StringPool.EMPTY;
}
final char[] buf = new char[repeat];
Arrays.fill(buf, ch);
return new String(buf);
}
/**
* 参考自 commons lang 微调
*
* <p>Gets the leftmost {@code len} characters of a String.</p>
*
* <p>If {@code len} characters are not available, or the
* String is {@code null}, the String will be returned without
* an exception. An empty String is returned if len is negative.</p>
*
* <pre>
* StringUtils.left(null, *) = null
* StringUtils.left(*, -ve) = ""
* StringUtils.left("", *) = ""
* StringUtils.left("abc", 0) = ""
* StringUtils.left("abc", 2) = "ab"
* StringUtils.left("abc", 4) = "abc"
* </pre>
*
* @param str the CharSequence to get the leftmost characters from, may be null
* @param len the length of the required String
* @return the leftmost characters, {@code null} if null String input
*/
@Nullable
public static String left(@Nullable final String str, final int len) {
if (str == null) {
return null;
}
if (len < 0) {
return StringPool.EMPTY;
}
if (str.length() <= len) {
return str;
}
return str.substring(0, len);
}
/**
* 参考自 commons lang 微调
*
* <p>Gets the rightmost {@code len} characters of a String.</p>
*
* <p>If {@code len} characters are not available, or the String
* is {@code null}, the String will be returned without an
* an exception. An empty String is returned if len is negative.</p>
*
* <pre>
* StringUtils.right(null, *) = null
* StringUtils.right(*, -ve) = ""
* StringUtils.right("", *) = ""
* StringUtils.right("abc", 0) = ""
* StringUtils.right("abc", 2) = "bc"
* StringUtils.right("abc", 4) = "abc"
* </pre>
*
* @param str the String to get the rightmost characters from, may be null
* @param len the length of the required String
* @return the rightmost characters, {@code null} if null String input
*/
@Nullable
public static String right(@Nullable final String str, final int len) {
if (str == null) {
return null;
}
if (len < 0) {
return StringPool.EMPTY;
}
int length = str.length();
if (length <= len) {
return str;
}
return str.substring(length - len);
}
/**
* 参考自 commons lang 微调
*
* <p>Right pad a String with spaces (' ').</p>
*
* <p>The String is padded to the size of {@code size}.</p>
*
* <pre>
* StringUtils.rightPad(null, *) = null
* StringUtils.rightPad("", 3) = " "
* StringUtils.rightPad("bat", 3) = "bat"
* StringUtils.rightPad("bat", 5) = "bat "
* StringUtils.rightPad("bat", 1) = "bat"
* StringUtils.rightPad("bat", -1) = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @return right padded String or original String if no padding is necessary,
* {@code null} if null String input
*/
@Nullable
public static String rightPad(@Nullable final String str, final int size) {
return rightPad(str, size, CharPool.SPACE);
}
/**
* 参考自 commons lang 微调
*
* <p>Right pad a String with a specified character.</p>
*
* <p>The String is padded to the size of {@code size}.</p>
*
* <pre>
* StringUtils.rightPad(null, *, *) = null
* StringUtils.rightPad("", 3, 'z') = "zzz"
* StringUtils.rightPad("bat", 3, 'z') = "bat"
* StringUtils.rightPad("bat", 5, 'z') = "batzz"
* StringUtils.rightPad("bat", 1, 'z') = "bat"
* StringUtils.rightPad("bat", -1, 'z') = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padChar the character to pad with
* @return right padded String or original String if no padding is necessary,
* {@code null} if null String input
*/
@Nullable
public static String rightPad(@Nullable final String str, final int size, final char padChar) {
if (str == null) {
return null;
}
final int pads = size - str.length();
if (pads <= 0) {
// returns original String when possible
return str;
}
if (pads > PAD_LIMIT) {
return rightPad(str, size, String.valueOf(padChar));
}
return str.concat(repeat(padChar, pads));
}
/**
* 参考自 commons lang 微调
*
* <p>Right pad a String with a specified String.</p>
*
* <p>The String is padded to the size of {@code size}.</p>
*
* <pre>
* StringUtils.rightPad(null, *, *) = null
* StringUtils.rightPad("", 3, "z") = "zzz"
* StringUtils.rightPad("bat", 3, "yz") = "bat"
* StringUtils.rightPad("bat", 5, "yz") = "batyz"
* StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
* StringUtils.rightPad("bat", 1, "yz") = "bat"
* StringUtils.rightPad("bat", -1, "yz") = "bat"
* StringUtils.rightPad("bat", 5, null) = "bat "
* StringUtils.rightPad("bat", 5, "") = "bat "
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padStr the String to pad with, null or empty treated as single space
* @return right padded String or original String if no padding is necessary,
* {@code null} if null String input
*/
@Nullable
public static String rightPad(@Nullable final String str, final int size, String padStr) {
if (str == null) {
return null;
}
if (!StringUtils.hasLength(padStr)) {
padStr = StringPool.SPACE;
}
final int padLen = padStr.length();
final int strLen = str.length();
final int pads = size - strLen;
if (pads <= 0) {
// returns original String when possible
return str;
}
if (padLen == 1 && pads <= PAD_LIMIT) {
return rightPad(str, size, padStr.charAt(0));
}
if (pads == padLen) {
return str.concat(padStr);
} else if (pads < padLen) {
return str.concat(padStr.substring(0, pads));
} else {
final char[] padding = new char[pads];
final char[] padChars = padStr.toCharArray();
for (int i = 0; i < pads; i++) {
padding[i] = padChars[i % padLen];
}
return str.concat(new String(padding));
}
}
/**
* 参考自 commons lang 微调
*
* <p>Left pad a String with spaces (' ').</p>
*
* <p>The String is padded to the size of {@code size}.</p>
*
* <pre>
* StringUtils.leftPad(null, *) = null
* StringUtils.leftPad("", 3) = " "
* StringUtils.leftPad("bat", 3) = "bat"
* StringUtils.leftPad("bat", 5) = " bat"
* StringUtils.leftPad("bat", 1) = "bat"
* StringUtils.leftPad("bat", -1) = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @return left padded String or original String if no padding is necessary,
* {@code null} if null String input
*/
@Nullable
public static String leftPad(@Nullable final String str, final int size) {
return leftPad(str, size, CharPool.SPACE);
}
/**
* 参考自 commons lang 微调
*
* <p>Left pad a String with a specified character.</p>
*
* <p>Pad to a size of {@code size}.</p>
*
* <pre>
* StringUtils.leftPad(null, *, *) = null
* StringUtils.leftPad("", 3, 'z') = "zzz"
* StringUtils.leftPad("bat", 3, 'z') = "bat"
* StringUtils.leftPad("bat", 5, 'z') = "zzbat"
* StringUtils.leftPad("bat", 1, 'z') = "bat"
* StringUtils.leftPad("bat", -1, 'z') = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padChar the character to pad with
* @return left padded String or original String if no padding is necessary,
* {@code null} if null String input
* @since 2.0
*/
@Nullable
public static String leftPad(@Nullable final String str, final int size, final char padChar) {
if (str == null) {
return null;
}
final int pads = size - str.length();
if (pads <= 0) {
// returns original String when possible
return str;
}
if (pads > PAD_LIMIT) {
return leftPad(str, size, String.valueOf(padChar));
}
return repeat(padChar, pads).concat(str);
}
/**
* 参考自 commons lang 微调
*
* <p>Left pad a String with a specified String.</p>
*
* <p>Pad to a size of {@code size}.</p>
*
* <pre>
* StringUtils.leftPad(null, *, *) = null
* StringUtils.leftPad("", 3, "z") = "zzz"
* StringUtils.leftPad("bat", 3, "yz") = "bat"
* StringUtils.leftPad("bat", 5, "yz") = "yzbat"
* StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
* StringUtils.leftPad("bat", 1, "yz") = "bat"
* StringUtils.leftPad("bat", -1, "yz") = "bat"
* StringUtils.leftPad("bat", 5, null) = " bat"
* StringUtils.leftPad("bat", 5, "") = " bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padStr the String to pad with, null or empty treated as single space
* @return left padded String or original String if no padding is necessary,
* {@code null} if null String input
*/
@Nullable
public static String leftPad(@Nullable final String str, final int size, String padStr) {
if (str == null) {
return null;
}
if (!StringUtils.hasLength(padStr)) {
padStr = StringPool.SPACE;
}
final int padLen = padStr.length();
final int strLen = str.length();
final int pads = size - strLen;
if (pads <= 0) {
// returns original String when possible
return str;
}
if (padLen == 1 && pads <= PAD_LIMIT) {
return leftPad(str, size, padStr.charAt(0));
}
if (pads == padLen) {
return padStr.concat(str);
} else if (pads < padLen) {
return padStr.substring(0, pads).concat(str);
} else {
final char[] padding = new char[pads];
final char[] padChars = padStr.toCharArray();
for (int i = 0; i < pads; i++) {
padding[i] = padChars[i % padLen];
}
return new String(padding).concat(str);
}
}
/**
* 参考自 commons lang 微调
*
* <p>Gets {@code len} characters from the middle of a String.</p>
*
* <p>If {@code len} characters are not available, the remainder
* of the String will be returned without an exception. If the
* String is {@code null}, {@code null} will be returned.
* An empty String is returned if len is negative or exceeds the
* length of {@code str}.</p>
*
* <pre>
* StringUtils.mid(null, *, *) = null
* StringUtils.mid(*, *, -ve) = ""
* StringUtils.mid("", 0, *) = ""
* StringUtils.mid("abc", 0, 2) = "ab"
* StringUtils.mid("abc", 0, 4) = "abc"
* StringUtils.mid("abc", 2, 4) = "c"
* StringUtils.mid("abc", 4, 2) = ""
* StringUtils.mid("abc", -2, 2) = "ab"
* </pre>
*
* @param str the String to get the characters from, may be null
* @param pos the position to start from, negative treated as zero
* @param len the length of the required String
* @return the middle characters, {@code null} if null String input
*/
@Nullable
public static String mid(@Nullable final String str, int pos, final int len) {
if (str == null) {
return null;
}
int length = str.length();
if (len < 0 || pos > length) {
return StringPool.EMPTY;
}
if (pos < 0) {
pos = 0;
}
if (length <= pos + len) {
return str.substring(pos);
}
return str.substring(pos, pos + len);
}
}

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <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 org.springblade.core.tool.utils;
import org.springblade.core.tool.support.Kv;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 模版解析工具类
*/
public class TemplateUtil {
/**
* 支持 ${} #{} 两种模版占位符
*/
private static final Pattern pattern = Pattern.compile("\\$\\{([^{}]+)}|\\#\\{([^{}]+)}");
/**
* 解析模版
*
* @param template 模版
* @param params 参数
* @return 解析后的字符串
*/
public static String process(String template, Kv params) {
Matcher matcher = pattern.matcher(template);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String key = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
String replacement = params.getStr(key);
if (replacement == null) {
throw new IllegalArgumentException("参数中缺少必要的键: " + key);
}
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
return sb.toString();
}
}

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <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 org.springblade.core.tool.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
/**
* 校验规则类
*
* @author Chill
*/
@Data
@AllArgsConstructor
public class ValidationRule implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 校验值
*/
private String value;
/**
* 校验正则
*/
private String regex;
/**
* 校验信息提示
*/
private String message;
}

View File

@ -0,0 +1,55 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <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 org.springblade.core.tool.utils;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* 校验通用工具类
*
* @author Chill
*/
public class ValidationUtil {
/**
* 自定义字段校验
*
* @param value 字段值
* @param regex 正则表达式
* @param message 验证消息
* @return String
*/
public static String validateField(String value, String regex, String message) {
if (!RegexUtil.match(regex, value)) {
return message;
}
return StringPool.EMPTY;
}
/**
* 如果字段值为空则设置一个默认值
*
* @param getter 字段的getter方法
* @param setter 字段的setter方法
* @param valueSupplier 默认值提供方法
*/
public static void setValueIfBlank(Supplier<String> getter, Consumer<String> setter, Supplier<String> valueSupplier) {
if (StringUtil.isBlank(getter.get())) {
setter.accept(valueSupplier.get());
}
}
}

View File

@ -5,13 +5,12 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.7.1</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-core-transaction</artifactId>
<name>${project.artifactId}</name>
<version>${blade.tool.version}</version>
<packaging>jar</packaging>
<dependencies>
@ -19,12 +18,10 @@
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-mybatis</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
<version>${blade.tool.version}</version>
</dependency>
<!-- Cloud-->
<dependency>

129
pom.xml
View File

@ -2,10 +2,9 @@
<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>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.7.1</version>
<version>${revision}</version>
<packaging>pom</packaging>
<name>blade-tool</name>
<description>
@ -36,11 +35,12 @@
</scm>
<properties>
<blade.tool.version>3.7.1</blade.tool.version>
<!-- Blade-Tool Version -->
<revision>3.7.2.1</revision>
<!-- jdk Version -->
<java.version>1.8</java.version>
<maven.plugin.version>3.8.1</maven.plugin.version>
<maven-flatten.version>1.3.0</maven-flatten.version>
<knife4j.version>4.3.0</knife4j.version>
<mybatis.version>3.5.13</mybatis.version>
<mybatis.spring.version>2.1.1</mybatis.spring.version>
@ -64,6 +64,7 @@
</properties>
<modules>
<module>blade-core-bom</module>
<module>blade-core-boot</module>
<module>blade-core-cloud</module>
<module>blade-core-develop</module>
@ -120,6 +121,98 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-auto</artifactId>
<version>${mica.auto.version}</version>
<scope>provided</scope>
</dependency>
<!-- blade tool -->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-boot</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-cloud</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-tool</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-develop</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-secure</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-social</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-log</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-test</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-swagger</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-mybatis</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-oss</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-transaction</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-loadbalancer</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-report</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-datascope</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-crypto</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -159,7 +252,6 @@
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-auto</artifactId>
<version>${mica.auto.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
@ -191,6 +283,31 @@
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${maven-flatten.version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>oss</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>