mirror of
https://github.com/chillzhuang/blade-tool
synced 2025-01-13 08:25:47 +08:00
⚡ 根据P3C优化代码
This commit is contained in:
parent
083b42c2fb
commit
912e0dd227
@ -24,6 +24,10 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
/**
|
||||
* 配置类
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({
|
||||
|
@ -33,6 +33,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* WEB配置
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
|
@ -27,6 +27,8 @@ import org.springframework.context.annotation.Profile;
|
||||
|
||||
/**
|
||||
* mybatisplus 配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan("org.springblade.**.mapper.**")
|
||||
|
@ -35,6 +35,8 @@ import java.time.Duration;
|
||||
|
||||
/**
|
||||
* RedisTemplate 配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
@ -43,6 +45,7 @@ public class RedisTemplateConfiguration {
|
||||
|
||||
/**
|
||||
* value 值 序列化
|
||||
*
|
||||
* @return RedisSerializer
|
||||
*/
|
||||
@Bean
|
||||
|
@ -24,6 +24,8 @@ import org.springframework.retry.interceptor.RetryOperationsInterceptor;
|
||||
|
||||
/**
|
||||
* 重试机制
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
|
@ -33,6 +33,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Blade控制器封装类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeController {
|
||||
|
||||
|
@ -26,6 +26,8 @@ import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* feign 传递Request header
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class BladeFeignRequestHeaderInterceptor implements RequestInterceptor {
|
||||
@ -40,16 +42,10 @@ public class BladeFeignRequestHeaderInterceptor implements RequestInterceptor {
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String name = headerNames.nextElement();
|
||||
String value = request.getHeader(name);
|
||||
/**
|
||||
* 遍历请求头里面的属性字段,将Authorization添加到新的请求头中转发到下游服务
|
||||
* */
|
||||
if ("Authorization".equals(name)) {
|
||||
log.debug("添加自定义请求头key:" + name + ",value:" + value);
|
||||
requestTemplate.header(name, value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.warn("FeignHeadConfiguration", "获取请求头失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,8 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 自定义Feign的隔离策略
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
||||
|
||||
|
@ -23,6 +23,10 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 上传文件封装
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeFile {
|
||||
/**
|
||||
* 上传文件在附件表中的id
|
||||
@ -74,7 +78,7 @@ public class BladeFile {
|
||||
|
||||
public BladeFile(MultipartFile file, String dir, String uploadPath, String uploadVirtualPath) {
|
||||
this(file, dir);
|
||||
if (null != uploadPath){
|
||||
if (null != uploadPath) {
|
||||
this.uploadPath = BladeFileUtil.formatUrl(uploadPath);
|
||||
this.uploadVirtualPath = BladeFileUtil.formatUrl(uploadVirtualPath);
|
||||
}
|
||||
@ -89,6 +93,7 @@ public class BladeFile {
|
||||
|
||||
/**
|
||||
* 图片上传
|
||||
*
|
||||
* @param compress 是否压缩
|
||||
*/
|
||||
public void transfer(boolean compress) {
|
||||
@ -98,6 +103,7 @@ public class BladeFile {
|
||||
|
||||
/**
|
||||
* 图片上传
|
||||
*
|
||||
* @param fileFactory 文件上传工厂类
|
||||
* @param compress 是否压缩
|
||||
*/
|
||||
@ -105,8 +111,8 @@ public class BladeFile {
|
||||
try {
|
||||
File file = new File(uploadPath);
|
||||
|
||||
if(null != fileFactory){
|
||||
String [] path = fileFactory.path(file, dir);
|
||||
if (null != fileFactory) {
|
||||
String[] path = fileFactory.path(file, dir);
|
||||
this.uploadPath = path[0];
|
||||
this.uploadVirtualPath = path[1];
|
||||
file = fileFactory.rename(file, path[0]);
|
||||
|
@ -18,12 +18,18 @@ package org.springblade.core.boot.file;
|
||||
import org.springblade.core.tool.constant.SystemConstant;
|
||||
import org.springblade.core.tool.date.DateUtil;
|
||||
import org.springblade.core.tool.utils.ImageUtil;
|
||||
import org.springblade.core.tool.utils.StringPool;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件代理类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeFileProxyFactory implements IFileProxy {
|
||||
|
||||
@Override
|
||||
@ -34,7 +40,7 @@ public class BladeFileProxyFactory implements IFileProxy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String [] path(File f, String dir) {
|
||||
public String[] path(File f, String dir) {
|
||||
//避免网络延迟导致时间不同步
|
||||
long time = System.nanoTime();
|
||||
|
||||
@ -48,21 +54,23 @@ public class BladeFileProxyFactory implements IFileProxy {
|
||||
.append(time)
|
||||
.append(getFileExt(f.getName()));
|
||||
|
||||
return new String [] {BladeFileUtil.formatUrl(uploadPath.toString()), BladeFileUtil.formatUrl(virtualPath.toString())};
|
||||
return new String[]{BladeFileUtil.formatUrl(uploadPath.toString()), BladeFileUtil.formatUrl(virtualPath.toString())};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件后缀
|
||||
*/
|
||||
public static String getFileExt(String fileName) {
|
||||
if (fileName.indexOf(".") == -1)
|
||||
if (!fileName.contains(StringPool.DOT)) {
|
||||
return ".jpg";
|
||||
else
|
||||
return fileName.substring(fileName.lastIndexOf('.'), fileName.length());
|
||||
} else {
|
||||
return fileName.substring(fileName.lastIndexOf(StringPool.DOT));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件保存地址
|
||||
*
|
||||
* @param saveDir
|
||||
* @return
|
||||
*/
|
||||
@ -77,9 +85,11 @@ public class BladeFileProxyFactory implements IFileProxy {
|
||||
|
||||
/**
|
||||
* 图片压缩
|
||||
*
|
||||
* @param path 文件地址
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void compress(String path) {
|
||||
try {
|
||||
ImageUtil.zoomScale(ImageUtil.readImage(path), new FileOutputStream(new File(path)), null, SystemConstant.me().getCompressScale(), SystemConstant.me().isCompressFlag());
|
||||
|
@ -20,12 +20,22 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeFileUtil {
|
||||
|
||||
// 定义允许上传的文件扩展名
|
||||
/**
|
||||
* 定义允许上传的文件扩展名
|
||||
*/
|
||||
private static HashMap<String, String> extMap = new HashMap<String, String>();
|
||||
// 图片扩展名
|
||||
private static String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
|
||||
|
||||
/**
|
||||
* 图片扩展名
|
||||
*/
|
||||
private static String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
|
||||
|
||||
static {
|
||||
extMap.put("image", ".gif,.jpg,.jpeg,.png,.bmp,.JPG,.JPEG,.PNG");
|
||||
@ -67,7 +77,12 @@ public class BladeFileUtil {
|
||||
public enum FileSort {
|
||||
size, type, name;
|
||||
|
||||
// 文本排序转换成枚举
|
||||
/**
|
||||
* 文本排序转换成枚举
|
||||
*
|
||||
* @param sort
|
||||
* @return
|
||||
*/
|
||||
public static FileSort of(String sort) {
|
||||
try {
|
||||
return FileSort.valueOf(sort);
|
||||
@ -78,6 +93,7 @@ public class BladeFileUtil {
|
||||
}
|
||||
|
||||
public static class NameComparator implements Comparator {
|
||||
@Override
|
||||
public int compare(Object a, Object b) {
|
||||
Hashtable hashA = (Hashtable) a;
|
||||
Hashtable hashB = (Hashtable) b;
|
||||
@ -92,6 +108,7 @@ public class BladeFileUtil {
|
||||
}
|
||||
|
||||
public static class SizeComparator implements Comparator {
|
||||
@Override
|
||||
public int compare(Object a, Object b) {
|
||||
Hashtable hashA = (Hashtable) a;
|
||||
Hashtable hashB = (Hashtable) b;
|
||||
@ -112,6 +129,7 @@ public class BladeFileUtil {
|
||||
}
|
||||
|
||||
public static class TypeComparator implements Comparator {
|
||||
@Override
|
||||
public int compare(Object a, Object b) {
|
||||
Hashtable hashA = (Hashtable) a;
|
||||
Hashtable hashB = (Hashtable) b;
|
||||
@ -134,64 +152,70 @@ public class BladeFileUtil {
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static BladeFile getFile(MultipartFile file){
|
||||
public static BladeFile getFile(MultipartFile file) {
|
||||
return getFile(file, "image", null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param file
|
||||
* @param dir
|
||||
* @return
|
||||
*/
|
||||
public static BladeFile getFile(MultipartFile file, String dir){
|
||||
public static BladeFile getFile(MultipartFile file, String dir) {
|
||||
return getFile(file, dir, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param file
|
||||
* @param dir
|
||||
* @param path
|
||||
* @param virtualPath
|
||||
* @return
|
||||
*/
|
||||
public static BladeFile getFile(MultipartFile file, String dir, String path, String virtualPath){
|
||||
public static BladeFile getFile(MultipartFile file, String dir, String path, String virtualPath) {
|
||||
return new BladeFile(file, dir, path, virtualPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files){
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files) {
|
||||
return getFiles(files, "image", null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param files
|
||||
* @param dir
|
||||
* @return
|
||||
*/
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files, String dir){
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files, String dir) {
|
||||
return getFiles(files, dir, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BladeFile封装类
|
||||
*
|
||||
* @param files
|
||||
* @param path
|
||||
* @param virtualPath
|
||||
* @return
|
||||
*/
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files, String dir, String path, String virtualPath){
|
||||
public static List<BladeFile> getFiles(List<MultipartFile> files, String dir, String path, String virtualPath) {
|
||||
List<BladeFile> list = new ArrayList<>();
|
||||
for (MultipartFile file : files){
|
||||
for (MultipartFile file : files) {
|
||||
list.add(new BladeFile(file, dir, path, virtualPath));
|
||||
}
|
||||
return list;
|
||||
|
@ -1,209 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
|
||||
* 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.boot.file;
|
||||
|
||||
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
import org.springblade.core.tool.utils.StringPool;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
public class FileMaker {
|
||||
private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
||||
|
||||
private File file;
|
||||
|
||||
private String fileName;
|
||||
|
||||
private HttpServletRequest request;
|
||||
|
||||
private HttpServletResponse response;
|
||||
|
||||
public static FileMaker init(HttpServletRequest request, HttpServletResponse response, File file) {
|
||||
return new FileMaker(request, response, file);
|
||||
}
|
||||
|
||||
public static FileMaker init(HttpServletRequest request, HttpServletResponse response, File file, String fileName) {
|
||||
return new FileMaker(request, response, file, fileName);
|
||||
}
|
||||
|
||||
private FileMaker(HttpServletRequest request, HttpServletResponse response, File file) {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("file can not be null.");
|
||||
}
|
||||
this.file = file;
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.fileName = file.getName();
|
||||
}
|
||||
|
||||
private FileMaker(HttpServletRequest request, HttpServletResponse response, File file, String fileName) {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("file can not be null.");
|
||||
}
|
||||
this.file = file;
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (file == null || !file.isFile()) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
// ---------
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setHeader("Content-disposition", "attachment; filename=" + encodeFileName(fileName));
|
||||
response.setContentType(DEFAULT_CONTENT_TYPE);
|
||||
|
||||
// ---------
|
||||
if (StringUtil.isBlank(request.getHeader("Range")))
|
||||
normalStart();
|
||||
else
|
||||
rangeStart();
|
||||
}
|
||||
|
||||
private String encodeFileName(String fileName) {
|
||||
try {
|
||||
return new String(fileName.getBytes(StringPool.GBK), StringPool.ISO_8859_1);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
||||
private void normalStart() {
|
||||
response.setHeader("Content-Length", String.valueOf(file.length()));
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
inputStream = new BufferedInputStream(new FileInputStream(file));
|
||||
outputStream = response.getOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
for (int len = -1; (len = inputStream.read(buffer)) != -1;) {
|
||||
outputStream.write(buffer, 0, len);
|
||||
}
|
||||
outputStream.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
if (inputStream != null)
|
||||
try {inputStream.close();} catch (IOException e) {}
|
||||
if (outputStream != null)
|
||||
try {outputStream.close();} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
private void rangeStart() {
|
||||
Long[] range = {null, null};
|
||||
processRange(range);
|
||||
|
||||
String contentLength = String.valueOf(range[1].longValue() - range[0].longValue() + 1);
|
||||
response.setHeader("Content-Length", contentLength);
|
||||
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // status = 206
|
||||
|
||||
// Content-Range: bytes 0-499/10000
|
||||
StringBuilder contentRange = new StringBuilder("bytes ").append(String.valueOf(range[0])).append("-").append(String.valueOf(range[1])).append("/").append(String.valueOf(file.length()));
|
||||
response.setHeader("Content-Range", contentRange.toString());
|
||||
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
long start = range[0];
|
||||
long end = range[1];
|
||||
inputStream = new BufferedInputStream(new FileInputStream(file));
|
||||
if (inputStream.skip(start) != start)
|
||||
throw new RuntimeException("File skip error");
|
||||
outputStream = response.getOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
long position = start;
|
||||
for (int len; position <= end && (len = inputStream.read(buffer)) != -1;) {
|
||||
if (position + len <= end) {
|
||||
outputStream.write(buffer, 0, len);
|
||||
position += len;
|
||||
}
|
||||
else {
|
||||
for (int i=0; i<len && position <= end; i++) {
|
||||
outputStream.write(buffer[i]);
|
||||
position++;
|
||||
}
|
||||
}
|
||||
}
|
||||
outputStream.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
if (inputStream != null)
|
||||
try {inputStream.close();} catch (IOException e) {}
|
||||
if (outputStream != null)
|
||||
try {outputStream.close();} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples of byte-ranges-specifier values (assuming an entity-body of length 10000):
|
||||
* The first 500 bytes (byte offsets 0-499, inclusive): bytes=0-499
|
||||
* The second 500 bytes (byte offsets 500-999, inclusive): bytes=500-999
|
||||
* The final 500 bytes (byte offsets 9500-9999, inclusive): bytes=-500
|
||||
* Or bytes=9500-
|
||||
*/
|
||||
private void processRange(Long[] range) {
|
||||
String rangeStr = request.getHeader("Range");
|
||||
int index = rangeStr.indexOf(',');
|
||||
if (index != -1)
|
||||
rangeStr = rangeStr.substring(0, index);
|
||||
rangeStr = rangeStr.replace("bytes=", "");
|
||||
|
||||
String[] arr = rangeStr.split("-", 2);
|
||||
if (arr.length < 2)
|
||||
throw new RuntimeException("Range error");
|
||||
|
||||
long fileLength = file.length();
|
||||
for (int i=0; i<range.length; i++) {
|
||||
if (StringUtil.isNotBlank(arr[i])) {
|
||||
range[i] = Long.parseLong(arr[i].trim());
|
||||
if (range[i] >= fileLength)
|
||||
range[i] = fileLength - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Range format like: 9500-
|
||||
if (range[0] != null && range[1] == null) {
|
||||
range[1] = fileLength - 1;
|
||||
}
|
||||
// Range format like: -500
|
||||
else if (range[0] == null && range[1] != null) {
|
||||
range[0] = fileLength - range[1];
|
||||
range[1] = fileLength - 1;
|
||||
}
|
||||
|
||||
// check final range
|
||||
if (range[0] == null || range[1] == null || range[0].longValue() > range[1].longValue())
|
||||
throw new RuntimeException("Range error");
|
||||
}
|
||||
}
|
@ -17,6 +17,11 @@ package org.springblade.core.boot.file;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 文件管理类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class FileProxyManager {
|
||||
private IFileProxy defaultFileProxyFactory = new BladeFileProxyFactory();
|
||||
|
||||
|
@ -17,18 +17,25 @@ package org.springblade.core.boot.file;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 文件代理接口
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface IFileProxy {
|
||||
|
||||
/**
|
||||
* 返回路径[物理路径][虚拟路径]
|
||||
*
|
||||
* @param file
|
||||
* @param dir
|
||||
* @return
|
||||
*/
|
||||
String [] path(File file, String dir);
|
||||
String[] path(File file, String dir);
|
||||
|
||||
/**
|
||||
* 文件重命名策略
|
||||
*
|
||||
* @param file
|
||||
* @param path
|
||||
* @return
|
||||
@ -37,6 +44,8 @@ public interface IFileProxy {
|
||||
|
||||
/**
|
||||
* 图片压缩
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
void compress(String path);
|
||||
|
||||
|
@ -40,9 +40,10 @@ public class RequestLogAspect {
|
||||
|
||||
/**
|
||||
* AOP 环切 控制器 R 返回值
|
||||
*
|
||||
* @param point JoinPoint
|
||||
* @throws Throwable 异常
|
||||
* @return Object
|
||||
* @throws Throwable 异常
|
||||
*/
|
||||
@Around(
|
||||
"execution(!static org.springblade.core.tool.api.R<*> *(..)) && " +
|
||||
|
@ -26,6 +26,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
/**
|
||||
* Token转化BladeUser
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
@ -28,6 +28,8 @@ import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 项目启动器,搞定环境变量问题
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeApplication {
|
||||
|
||||
|
@ -20,6 +20,8 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 系统启动完毕后执行
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Component
|
||||
public class BladeLineRunner implements CommandLineRunner {
|
||||
|
@ -1,3 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
|
||||
* 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.launch;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -11,6 +26,8 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* 项目启动事件通知
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
|
@ -31,6 +31,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Consul自定义注册规则
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnConsulEnabled
|
||||
|
@ -26,7 +26,11 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
|
||||
/**
|
||||
* 配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
|
@ -17,6 +17,8 @@ package org.springblade.core.launch.constant;
|
||||
|
||||
/**
|
||||
* 系统常量
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface AppConstant {
|
||||
|
||||
|
@ -25,6 +25,8 @@ import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
|
||||
|
||||
/**
|
||||
* Consul自定义注册规则
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeConsulServiceRegistry extends ConsulServiceRegistry {
|
||||
|
||||
|
@ -1,3 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
|
||||
* 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.launch.consul;
|
||||
|
||||
import org.springblade.core.launch.constant.AppConstant;
|
||||
@ -8,6 +23,8 @@ import java.util.Properties;
|
||||
|
||||
/**
|
||||
* consul启动拓展
|
||||
*
|
||||
* @author smallchil
|
||||
*/
|
||||
public class ConsulLauncherService implements LauncherService {
|
||||
|
||||
|
@ -25,6 +25,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 配置文件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@ConfigurationProperties("blade")
|
||||
public class BladeProperties {
|
||||
|
@ -20,6 +20,8 @@ import org.springframework.cloud.commons.util.InetUtils;
|
||||
|
||||
/**
|
||||
* 服务器信息
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ServerInfo {
|
||||
|
||||
|
@ -19,11 +19,14 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
/**
|
||||
* launcher 扩展 用于一些组件发现
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface LauncherService {
|
||||
|
||||
/**
|
||||
* 启动时 处理 SpringApplicationBuilder
|
||||
*
|
||||
* @param builder SpringApplicationBuilder
|
||||
* @param appName SpringApplicationAppName
|
||||
* @param profile SpringApplicationProfile
|
||||
|
@ -20,6 +20,8 @@ import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志注解
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
@ -25,6 +25,8 @@ import org.springblade.core.log.publisher.ApiLogPublisher;
|
||||
|
||||
/**
|
||||
* 操作日志使用spring event异步入库
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
|
@ -38,12 +38,14 @@ import javax.servlet.Servlet;
|
||||
|
||||
/**
|
||||
* 统一异常处理
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
@ConditionalOnWebApplication
|
||||
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
|
||||
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
|
||||
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
|
||||
public class BladeErrorMvcAutoConfiguration {
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
|
@ -31,6 +31,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 日志工具自动配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
|
@ -17,6 +17,8 @@ package org.springblade.core.log.constant;
|
||||
|
||||
/**
|
||||
* 事件常量
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface EventConstant {
|
||||
|
||||
|
@ -29,6 +29,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class BladeErrorAttributes extends DefaultErrorAttributes {
|
||||
|
@ -30,6 +30,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 更改html请求异常为ajax
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeErrorController extends BasicErrorController {
|
||||
|
||||
|
@ -51,10 +51,12 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* 全局异常处理,处理可预见的异常
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
|
||||
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||
@RestControllerAdvice
|
||||
public class BladeRestExceptionTranslator {
|
||||
|
@ -22,6 +22,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 系统日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ApiLogEvent extends ApplicationEvent {
|
||||
|
||||
|
@ -38,6 +38,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 异步监听日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
|
@ -22,6 +22,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 系统日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeLogEvent extends ApplicationEvent {
|
||||
|
||||
|
@ -37,6 +37,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 异步监听日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
|
@ -22,6 +22,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 错误日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ErrorLogEvent extends ApplicationEvent {
|
||||
|
||||
|
@ -36,6 +36,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 异步监听错误日志事件
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
|
@ -22,6 +22,8 @@ import org.springblade.core.tool.api.ResultCode;
|
||||
|
||||
/**
|
||||
* 业务异常
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ServiceException extends RuntimeException {
|
||||
private static final long serialVersionUID = 2359767895161832954L;
|
||||
@ -46,6 +48,7 @@ public class ServiceException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 提高性能
|
||||
*
|
||||
* @return Throwable
|
||||
*/
|
||||
@Override
|
||||
|
@ -26,6 +26,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* Feign接口类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@FeignClient(
|
||||
value = AppConstant.APPLICATION_LOG_NAME
|
||||
|
@ -22,6 +22,8 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* 日志工具类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class BladeLogger implements InitializingBean {
|
||||
|
@ -30,7 +30,7 @@ import java.time.LocalDateTime;
|
||||
/**
|
||||
* 实体类
|
||||
*
|
||||
* @author Blade
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@TableName("blade_log_api")
|
||||
|
@ -30,7 +30,7 @@ import java.time.LocalDateTime;
|
||||
/**
|
||||
* 实体类
|
||||
*
|
||||
* @author Blade
|
||||
* @author smallchill
|
||||
* @since 2018-10-12
|
||||
*/
|
||||
@Data
|
||||
|
@ -30,6 +30,8 @@ import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 服务 异常
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@TableName("blade_log_error")
|
||||
|
@ -30,6 +30,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* API日志信息事件发送
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ApiLogPublisher {
|
||||
|
||||
|
@ -28,6 +28,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* BLADE日志信息事件发送
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeLogPublisher {
|
||||
|
||||
|
@ -27,6 +27,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 异常信息事件发送
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ErrorLogPublisher {
|
||||
|
||||
@ -47,7 +49,7 @@ public class ErrorLogPublisher {
|
||||
logError.setLineNumber(element.getLineNumber());
|
||||
}
|
||||
}
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
Map<String, Object> event = new HashMap<>(16);
|
||||
event.put(EventConstant.EVENT_LOG, logError);
|
||||
event.put(EventConstant.EVENT_REQUEST, request);
|
||||
SpringUtil.publishEvent(new ErrorLogEvent(event));
|
||||
|
@ -21,6 +21,8 @@ import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
/**
|
||||
* mybatisplus自定义填充
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class BladeMetaObjectHandler implements MetaObjectHandler {
|
||||
|
@ -27,6 +27,11 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 基础实体类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
public class BaseEntity implements Serializable {
|
||||
/**
|
||||
|
@ -20,10 +20,17 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 基础业务接口
|
||||
*
|
||||
* @param <T>
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface BaseService<T> extends IService<T> {
|
||||
|
||||
/**
|
||||
* 逻辑删除
|
||||
*
|
||||
* @param ids id集合(逗号分隔)
|
||||
* @return
|
||||
*/
|
||||
|
@ -35,6 +35,7 @@ import java.util.List;
|
||||
*
|
||||
* @param <M> mapper
|
||||
* @param <T> model
|
||||
* @author smallchill
|
||||
*/
|
||||
@Validated
|
||||
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
|
||||
|
@ -23,6 +23,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 视图包装基类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public abstract class BaseEntityWrapper<E, V> {
|
||||
|
||||
|
@ -25,6 +25,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 分页工具
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class Condition {
|
||||
|
||||
|
@ -21,6 +21,8 @@ import lombok.Data;
|
||||
|
||||
/**
|
||||
* 分页工具
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(description = "查询条件")
|
||||
|
@ -21,6 +21,8 @@ import lombok.Data;
|
||||
|
||||
/**
|
||||
* AuthInfo
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(description = "认证信息")
|
||||
|
@ -15,12 +15,15 @@
|
||||
*/
|
||||
package org.springblade.core.secure;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户实体
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
public class BladeUser implements Serializable {
|
||||
@ -30,23 +33,27 @@ public class BladeUser implements Serializable {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@ApiModelProperty(hidden = true)
|
||||
private Integer userId;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
@ApiModelProperty(hidden = true)
|
||||
private String userName;
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@ApiModelProperty(hidden = true)
|
||||
private String account;
|
||||
/**
|
||||
* 角色id
|
||||
*/
|
||||
@ApiModelProperty(hidden = true)
|
||||
private String roleId;
|
||||
/**
|
||||
* 角色名
|
||||
*/
|
||||
@ApiModelProperty(hidden = true)
|
||||
private String roleName;
|
||||
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import java.lang.annotation.*;
|
||||
/**
|
||||
* 权限注解 用于检查权限 规定访问权限
|
||||
*
|
||||
* @author smallchill
|
||||
* @example @PreAuth("#userVO.id<10")
|
||||
* @example @PreAuth("hasRole(#test, #test1)")
|
||||
* @example @PreAuth("hasPermission(#test) and @PreAuth.hasPermission(#test)")
|
||||
|
@ -39,6 +39,8 @@ import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* AOP 鉴权
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Aspect
|
||||
public class AuthAspect implements ApplicationContextAware {
|
||||
@ -50,6 +52,7 @@ public class AuthAspect implements ApplicationContextAware {
|
||||
|
||||
/**
|
||||
* 切 方法 和 类上的 @PreAuth 注解
|
||||
*
|
||||
* @param point 切点
|
||||
* @return Object
|
||||
* @throws Throwable 没有权限的异常
|
||||
|
@ -23,6 +23,8 @@ import org.springblade.core.tool.utils.StringUtil;
|
||||
|
||||
/**
|
||||
* 权限判断
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class AuthFun {
|
||||
|
||||
|
@ -24,6 +24,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* secure模块api放行默认配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureBefore(SecureConfiguration.class)
|
||||
|
@ -26,6 +26,11 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* 配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Order
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
|
@ -21,6 +21,8 @@ import org.springblade.core.tool.api.ResultCode;
|
||||
|
||||
/**
|
||||
* Secure异常
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class SecureException extends RuntimeException {
|
||||
private static final long serialVersionUID = 2359767895161832954L;
|
||||
|
@ -32,6 +32,8 @@ import java.util.Objects;
|
||||
|
||||
/**
|
||||
* jwt拦截器校验
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Slf4j
|
||||
public class SecureInterceptor extends HandlerInterceptorAdapter {
|
||||
|
@ -23,6 +23,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* secure api放行配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
public class SecureRegistry {
|
||||
|
@ -36,6 +36,8 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Secure工具类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class SecureUtil {
|
||||
public static final String BLADE_USER_REQUEST_ATTR = "_BLADE_USER_REQUEST_ATTR_";
|
||||
@ -91,9 +93,9 @@ public class SecureUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Integer getUserId() {
|
||||
@ -102,6 +104,7 @@ public class SecureUtil {
|
||||
|
||||
/**
|
||||
* 获取用户id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Integer getUserId(HttpServletRequest request) {
|
||||
@ -110,6 +113,7 @@ public class SecureUtil {
|
||||
|
||||
/**
|
||||
* 获取用户账号
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getUserAccount() {
|
||||
@ -118,6 +122,7 @@ public class SecureUtil {
|
||||
|
||||
/**
|
||||
* 获取用户账号
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getUserAccount(HttpServletRequest request) {
|
||||
|
@ -39,6 +39,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* swagger配置
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@ -82,7 +84,7 @@ public class SwaggerAutoConfiguration {
|
||||
.build()
|
||||
.securitySchemes(Collections.singletonList(securitySchema()))
|
||||
.securityContexts(Collections.singletonList(securityContext()))
|
||||
.pathMapping("/" );
|
||||
.pathMapping("/");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,6 +25,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* SwaggerProperties
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@RefreshScope
|
||||
|
@ -19,11 +19,23 @@ import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 业务代码接口
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface IResultCode extends Serializable {
|
||||
|
||||
/**
|
||||
* 消息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getMessage();
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int getCode();
|
||||
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 统一API响应结果封装
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ -94,6 +96,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param data 数据
|
||||
*/
|
||||
public static <T> R<T> data(T data) {
|
||||
@ -102,6 +105,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param data 数据
|
||||
* @param msg 消息
|
||||
*/
|
||||
@ -111,6 +115,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param code 状态码
|
||||
* @param data 数据
|
||||
* @param msg 消息
|
||||
@ -121,6 +126,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param msg 消息
|
||||
*/
|
||||
public static <T> R<T> success(String msg) {
|
||||
@ -129,6 +135,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param resultCode 业务代码
|
||||
*/
|
||||
public static <T> R<T> success(IResultCode resultCode) {
|
||||
@ -137,6 +144,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param resultCode 业务代码
|
||||
*/
|
||||
public static <T> R<T> success(IResultCode resultCode, String msg) {
|
||||
@ -145,6 +153,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param msg 消息
|
||||
*/
|
||||
public static <T> R<T> failure(String msg) {
|
||||
@ -154,6 +163,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param code 状态码
|
||||
* @param msg 消息
|
||||
*/
|
||||
@ -163,6 +173,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param resultCode 业务代码
|
||||
*/
|
||||
public static <T> R<T> failure(IResultCode resultCode) {
|
||||
@ -171,6 +182,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param resultCode 业务代码
|
||||
*/
|
||||
public static <T> R<T> failure(IResultCode resultCode, String msg) {
|
||||
@ -179,6 +191,7 @@ public class R<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回R
|
||||
*
|
||||
* @param flag 成功状态
|
||||
*/
|
||||
public static R status(boolean flag) {
|
||||
|
@ -22,6 +22,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 业务代码枚举
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
|
@ -34,6 +34,11 @@ import java.time.ZoneId;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* Jackson配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
@AutoConfigureBefore(JacksonAutoConfiguration.class)
|
||||
|
@ -33,6 +33,11 @@ import javax.servlet.DispatcherType;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 消息配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
|
@ -23,7 +23,11 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
|
||||
/**
|
||||
* 工具配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Configuration
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class ToolConfiguration implements WebMvcConfigurer {
|
||||
|
@ -17,6 +17,8 @@ package org.springblade.core.tool.constant;
|
||||
|
||||
/**
|
||||
* 系统常量
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface BladeConstant {
|
||||
|
||||
|
@ -17,6 +17,8 @@ package org.springblade.core.tool.constant;
|
||||
|
||||
/**
|
||||
* 系统默认角色
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class RoleConstant {
|
||||
|
||||
|
@ -20,6 +20,8 @@ import lombok.Data;
|
||||
|
||||
/**
|
||||
* Blade系统配置类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
public class SystemConstant {
|
||||
|
@ -168,6 +168,7 @@ public class JsonUtil {
|
||||
|
||||
/**
|
||||
* 将json反序列化成List对象
|
||||
*
|
||||
* @param content content
|
||||
* @param valueTypeRef class
|
||||
* @param <T> T 泛型标记
|
||||
|
@ -24,7 +24,7 @@ import java.util.List;
|
||||
/**
|
||||
* 节点基类
|
||||
*
|
||||
* @author zhuangqian
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
public class BaseNode implements INode {
|
||||
|
@ -21,12 +21,17 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 森林节点类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class ForestNode extends BaseNode {
|
||||
|
||||
private Object content;//节点内容
|
||||
/**
|
||||
* 节点内容
|
||||
*/
|
||||
private Object content;
|
||||
|
||||
public ForestNode(Integer id, Integer parentId, Object content) {
|
||||
this.id = id;
|
||||
|
@ -21,7 +21,7 @@ import java.util.List;
|
||||
/**
|
||||
* 森林管理类
|
||||
*
|
||||
* @author zhuangqian
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ForestNodeManager<T extends INode> {
|
||||
|
||||
|
@ -20,7 +20,7 @@ import java.util.List;
|
||||
/**
|
||||
* 森林节点归并类
|
||||
*
|
||||
* @author zhuangqian
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ForestNodeMerger {
|
||||
|
||||
|
@ -20,7 +20,7 @@ import java.util.List;
|
||||
/**
|
||||
* Created by Blade.
|
||||
*
|
||||
* @author zhuangqian
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface INode {
|
||||
|
||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
/**
|
||||
* Created by Blade.
|
||||
*
|
||||
* @author zhuangqian
|
||||
* @author smallchill
|
||||
*/
|
||||
public class NodeTest {
|
||||
|
||||
|
@ -20,6 +20,8 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 树型节点类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
|
@ -5,6 +5,8 @@ import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Bean属性
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
|
@ -24,16 +24,19 @@ import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* 链式map
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class CMap extends CaseInsensitiveHashMap<String, Object> {
|
||||
|
||||
|
||||
private CMap(){
|
||||
private CMap() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建CMap
|
||||
*
|
||||
* @return CMap
|
||||
*/
|
||||
public static CMap init() {
|
||||
@ -46,6 +49,7 @@ public class CMap extends CaseInsensitiveHashMap<String, Object> {
|
||||
|
||||
/**
|
||||
* 设置列
|
||||
*
|
||||
* @param attr 属性
|
||||
* @param value 值
|
||||
* @return 本身
|
||||
@ -57,12 +61,13 @@ public class CMap extends CaseInsensitiveHashMap<String, Object> {
|
||||
|
||||
/**
|
||||
* 设置列,当键或值为null时忽略
|
||||
*
|
||||
* @param attr 属性
|
||||
* @param value 值
|
||||
* @return 本身
|
||||
*/
|
||||
public CMap setIgnoreNull(String attr, Object value) {
|
||||
if(null != attr && null != value) {
|
||||
if (null != attr && null != value) {
|
||||
set(attr, value);
|
||||
}
|
||||
return this;
|
||||
@ -82,7 +87,7 @@ public class CMap extends CaseInsensitiveHashMap<String, Object> {
|
||||
*/
|
||||
public <T> T get(String attr, T defaultValue) {
|
||||
final Object result = get(attr);
|
||||
return (T)(result != null ? result : defaultValue);
|
||||
return (T) (result != null ? result : defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,8 +17,14 @@ package org.springblade.core.tool.support;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class CaseInsensitiveHashMap<K,V> extends LinkedHashMap<String, Object> {
|
||||
/**
|
||||
* 大小写忽略Map拓展
|
||||
*
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @author smallchill
|
||||
*/
|
||||
public class CaseInsensitiveHashMap<K, V> extends LinkedHashMap<String, Object> {
|
||||
|
||||
private static final long serialVersionUID = 9178606903603606031L;
|
||||
|
||||
|
@ -19,6 +19,8 @@ import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* A factory for creating MultiOutputStream objects.
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public interface IMultiOutputStream {
|
||||
|
||||
@ -28,6 +30,6 @@ public interface IMultiOutputStream {
|
||||
* @param params the params
|
||||
* @return the output stream
|
||||
*/
|
||||
OutputStream buildOutputStream(Integer... params) ;
|
||||
OutputStream buildOutputStream(Integer... params);
|
||||
|
||||
}
|
||||
|
@ -15,38 +15,63 @@
|
||||
*/
|
||||
package org.springblade.core.tool.support;
|
||||
|
||||
/**
|
||||
* 图片操作类
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class ImagePosition {
|
||||
|
||||
/** 图片顶部. */
|
||||
/**
|
||||
* 图片顶部.
|
||||
*/
|
||||
public static final int TOP = 32;
|
||||
|
||||
/** 图片中部. */
|
||||
/**
|
||||
* 图片中部.
|
||||
*/
|
||||
public static final int MIDDLE = 16;
|
||||
|
||||
/** 图片底部. */
|
||||
/**
|
||||
* 图片底部.
|
||||
*/
|
||||
public static final int BOTTOM = 8;
|
||||
|
||||
/** 图片左侧. */
|
||||
/**
|
||||
* 图片左侧.
|
||||
*/
|
||||
public static final int LEFT = 4;
|
||||
|
||||
/** 图片居中. */
|
||||
/**
|
||||
* 图片居中.
|
||||
*/
|
||||
public static final int CENTER = 2;
|
||||
|
||||
/** 图片右侧. */
|
||||
/**
|
||||
* 图片右侧.
|
||||
*/
|
||||
public static final int RIGHT = 1;
|
||||
|
||||
/** 横向边距,靠左或靠右时和边界的距离. */
|
||||
/**
|
||||
* 横向边距,靠左或靠右时和边界的距离.
|
||||
*/
|
||||
private static final int PADDING_HORI = 6;
|
||||
|
||||
/** 纵向边距,靠上或靠底时和边界的距离. */
|
||||
/**
|
||||
* 纵向边距,靠上或靠底时和边界的距离.
|
||||
*/
|
||||
private static final int PADDING_VERT = 6;
|
||||
|
||||
|
||||
/** 图片中盒[左上角]的x坐标. */
|
||||
private int boxPosX ;
|
||||
/**
|
||||
* 图片中盒[左上角]的x坐标.
|
||||
*/
|
||||
private int boxPosX;
|
||||
|
||||
/** 图片中盒[左上角]的y坐标. */
|
||||
private int boxPosY ;
|
||||
/**
|
||||
* 图片中盒[左上角]的y坐标.
|
||||
*/
|
||||
private int boxPosY;
|
||||
|
||||
/**
|
||||
* Instantiates a new image position.
|
||||
@ -57,8 +82,8 @@ public class ImagePosition {
|
||||
* @param boxHeight the box height
|
||||
* @param style the style
|
||||
*/
|
||||
public ImagePosition(int width , int height , int boxWidth , int boxHeight, int style ) {
|
||||
switch(style & 7) {
|
||||
public ImagePosition(int width, int height, int boxWidth, int boxHeight, int style) {
|
||||
switch (style & 7) {
|
||||
case LEFT:
|
||||
boxPosX = PADDING_HORI;
|
||||
break;
|
||||
@ -67,14 +92,14 @@ public class ImagePosition {
|
||||
break;
|
||||
case CENTER:
|
||||
default:
|
||||
boxPosX = (width - boxWidth)/2;
|
||||
boxPosX = (width - boxWidth) / 2;
|
||||
}
|
||||
switch(style >> 3 << 3) {
|
||||
switch (style >> 3 << 3) {
|
||||
case TOP:
|
||||
boxPosY = PADDING_VERT;
|
||||
break;
|
||||
case MIDDLE:
|
||||
boxPosY = (height - boxHeight)/2;
|
||||
boxPosY = (height - boxHeight) / 2;
|
||||
break;
|
||||
case BOTTOM:
|
||||
default:
|
||||
|
@ -3,6 +3,11 @@ package org.springblade.core.tool.support;
|
||||
import org.springblade.core.tool.utils.Func;
|
||||
import org.springblade.core.tool.utils.StringPool;
|
||||
|
||||
/**
|
||||
* 字符串格式化
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class StrFormatter {
|
||||
|
||||
/**
|
||||
@ -13,6 +18,7 @@ public class StrFormatter {
|
||||
* 通常使用:format("this is {} for {}", "a", "b") =》 this is a for b<br>
|
||||
* 转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a<br>
|
||||
* 转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b<br>
|
||||
*
|
||||
* @param strPattern 字符串模板
|
||||
* @param argArray 参数列表
|
||||
* @return 结果
|
||||
@ -23,23 +29,44 @@ public class StrFormatter {
|
||||
}
|
||||
final int strPatternLength = strPattern.length();
|
||||
|
||||
//初始化定义好的长度以获得更好的性能
|
||||
/**
|
||||
* 初始化定义好的长度以获得更好的性能
|
||||
*/
|
||||
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
|
||||
|
||||
int handledPosition = 0;//记录已经处理到的位置
|
||||
int delimIndex;//占位符所在位置
|
||||
/**
|
||||
* 记录已经处理到的位置
|
||||
*/
|
||||
int handledPosition = 0;
|
||||
|
||||
/**
|
||||
* 占位符所在位置
|
||||
*/
|
||||
int delimIndex;
|
||||
for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
|
||||
delimIndex = strPattern.indexOf(StringPool.EMPTY_JSON, handledPosition);
|
||||
if (delimIndex == -1) {//剩余部分无占位符
|
||||
if (handledPosition == 0) { //不带占位符的模板直接返回
|
||||
/**
|
||||
* 剩余部分无占位符
|
||||
*/
|
||||
if (delimIndex == -1) {
|
||||
/**
|
||||
* 不带占位符的模板直接返回
|
||||
*/
|
||||
if (handledPosition == 0) {
|
||||
return strPattern;
|
||||
} else { //字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
|
||||
} else {
|
||||
sbuf.append(strPattern, handledPosition, strPatternLength);
|
||||
return sbuf.toString();
|
||||
}
|
||||
} else {
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == StringPool.BACK_SLASH) {//转义符
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == StringPool.BACK_SLASH) {//双转义符
|
||||
/**
|
||||
* 转义符
|
||||
*/
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == StringPool.BACK_SLASH) {
|
||||
/**
|
||||
* 双转义符
|
||||
*/
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == StringPool.BACK_SLASH) {
|
||||
//转义符之前还有一个转义符,占位符依旧有效
|
||||
sbuf.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbuf.append(Func.toStr(argArray[argIndex]));
|
||||
|
@ -11,12 +11,13 @@ import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 字符串切分器
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class StrSpliter {
|
||||
|
||||
//---------------------------------------------------------------------------------------------- Split by char
|
||||
|
||||
/**
|
||||
* 切分字符串路径,仅支持Unix分界符:/
|
||||
*
|
||||
@ -24,7 +25,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> splitPath(String str){
|
||||
public static List<String> splitPath(String str) {
|
||||
return splitPath(str, 0);
|
||||
}
|
||||
|
||||
@ -35,7 +36,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitPathToArray(String str){
|
||||
public static String[] splitPathToArray(String str) {
|
||||
return toArray(splitPath(str));
|
||||
}
|
||||
|
||||
@ -47,7 +48,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> splitPath(String str, int limit){
|
||||
public static List<String> splitPath(String str, int limit) {
|
||||
return split(str, StringPool.SLASH, limit, true, true);
|
||||
}
|
||||
|
||||
@ -59,7 +60,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitPathToArray(String str, int limit){
|
||||
public static String[] splitPathToArray(String str, int limit) {
|
||||
return toArray(splitPath(str, limit));
|
||||
}
|
||||
|
||||
@ -72,7 +73,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitTrim(String str, char separator, boolean ignoreEmpty){
|
||||
public static List<String> splitTrim(String str, char separator, boolean ignoreEmpty) {
|
||||
return split(str, separator, 0, true, ignoreEmpty);
|
||||
}
|
||||
|
||||
@ -86,7 +87,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, char separator, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> split(String str, char separator, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, 0, isTrim, ignoreEmpty);
|
||||
}
|
||||
|
||||
@ -100,7 +101,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> splitTrim(String str, char separator, int limit, boolean ignoreEmpty){
|
||||
public static List<String> splitTrim(String str, char separator, int limit, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, true, ignoreEmpty, false);
|
||||
}
|
||||
|
||||
@ -115,7 +116,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, isTrim, ignoreEmpty, false);
|
||||
}
|
||||
|
||||
@ -130,7 +131,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitIgnoreCase(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> splitIgnoreCase(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, isTrim, ignoreEmpty, true);
|
||||
}
|
||||
|
||||
@ -146,24 +147,24 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase){
|
||||
if(StringUtil.isEmpty(str)){
|
||||
public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase) {
|
||||
if (StringUtil.isEmpty(str)) {
|
||||
return new ArrayList<String>(0);
|
||||
}
|
||||
if(limit == 1){
|
||||
if (limit == 1) {
|
||||
return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty);
|
||||
}
|
||||
|
||||
final ArrayList<String> list = new ArrayList<>(limit > 0 ? limit : 16);
|
||||
int len = str.length();
|
||||
int start = 0;//切分后每个部分的起始
|
||||
for(int i = 0; i < len; i++){
|
||||
if(Func.equals(separator, str.charAt(i))){
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (Func.equals(separator, str.charAt(i))) {
|
||||
addToList(list, str.substring(start, i), isTrim, ignoreEmpty);
|
||||
start = i+1;//i+1同时将start与i保持一致
|
||||
start = i + 1;//i+1同时将start与i保持一致
|
||||
|
||||
//检查是否超出范围(最大允许limit-1个,剩下一个留给末尾字符串)
|
||||
if(limit > 0 && list.size() > limit-2){
|
||||
if (limit > 0 && list.size() > limit - 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -182,7 +183,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitToArray(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static String[] splitToArray(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return toArray(split(str, separator, limit, isTrim, ignoreEmpty));
|
||||
}
|
||||
|
||||
@ -198,7 +199,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, String separator, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> split(String str, String separator, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, -1, isTrim, ignoreEmpty, false);
|
||||
}
|
||||
|
||||
@ -211,7 +212,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitTrim(String str, String separator, boolean ignoreEmpty){
|
||||
public static List<String> splitTrim(String str, String separator, boolean ignoreEmpty) {
|
||||
return split(str, separator, true, ignoreEmpty);
|
||||
}
|
||||
|
||||
@ -226,7 +227,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, isTrim, ignoreEmpty, false);
|
||||
}
|
||||
|
||||
@ -240,7 +241,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitTrim(String str, String separator, int limit, boolean ignoreEmpty){
|
||||
public static List<String> splitTrim(String str, String separator, int limit, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, true, ignoreEmpty);
|
||||
}
|
||||
|
||||
@ -255,7 +256,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitIgnoreCase(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static List<String> splitIgnoreCase(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, isTrim, ignoreEmpty, true);
|
||||
}
|
||||
|
||||
@ -269,7 +270,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> splitTrimIgnoreCase(String str, String separator, int limit, boolean ignoreEmpty){
|
||||
public static List<String> splitTrimIgnoreCase(String str, String separator, int limit, boolean ignoreEmpty) {
|
||||
return split(str, separator, limit, true, ignoreEmpty, true);
|
||||
}
|
||||
|
||||
@ -285,17 +286,17 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase){
|
||||
if(StringUtil.isEmpty(str)){
|
||||
public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase) {
|
||||
if (StringUtil.isEmpty(str)) {
|
||||
return new ArrayList<String>(0);
|
||||
}
|
||||
if(limit == 1){
|
||||
if (limit == 1) {
|
||||
return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty);
|
||||
}
|
||||
|
||||
if(StringUtil.isEmpty(separator)){//分隔符为空时按照空白符切分
|
||||
if (StringUtil.isEmpty(separator)) {//分隔符为空时按照空白符切分
|
||||
return split(str, limit);
|
||||
}else if(separator.length() == 1){//分隔符只有一个字符长度时按照单分隔符切分
|
||||
} else if (separator.length() == 1) {//分隔符只有一个字符长度时按照单分隔符切分
|
||||
return split(str, separator.charAt(0), limit, isTrim, ignoreEmpty, ignoreCase);
|
||||
}
|
||||
|
||||
@ -304,17 +305,17 @@ public class StrSpliter {
|
||||
int separatorLen = separator.length();
|
||||
int start = 0;
|
||||
int i = 0;
|
||||
while(i < len){
|
||||
while (i < len) {
|
||||
i = StringUtil.indexOf(str, separator, start, ignoreCase);
|
||||
if(i > -1){
|
||||
if (i > -1) {
|
||||
addToList(list, str.substring(start, i), isTrim, ignoreEmpty);
|
||||
start = i + separatorLen;
|
||||
|
||||
//检查是否超出范围(最大允许limit-1个,剩下一个留给末尾字符串)
|
||||
if(limit > 0 && list.size() > limit-2){
|
||||
if (limit > 0 && list.size() > limit - 2) {
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -332,7 +333,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitToArray(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static String[] splitToArray(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return toArray(split(str, separator, limit, isTrim, ignoreEmpty));
|
||||
}
|
||||
|
||||
@ -347,24 +348,24 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, int limit){
|
||||
if(StringUtil.isEmpty(str)){
|
||||
public static List<String> split(String str, int limit) {
|
||||
if (StringUtil.isEmpty(str)) {
|
||||
return new ArrayList<String>(0);
|
||||
}
|
||||
if(limit == 1){
|
||||
if (limit == 1) {
|
||||
return addToList(new ArrayList<String>(1), str, true, true);
|
||||
}
|
||||
|
||||
final ArrayList<String> list = new ArrayList<>();
|
||||
int len = str.length();
|
||||
int start = 0;//切分后每个部分的起始
|
||||
for(int i = 0; i < len; i++){
|
||||
if(Func.isEmpty(str.charAt(i))){
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (Func.isEmpty(str.charAt(i))) {
|
||||
addToList(list, str.substring(start, i), true, true);
|
||||
start = i+1;//i+1同时将start与i保持一致
|
||||
start = i + 1;//i+1同时将start与i保持一致
|
||||
|
||||
//检查是否超出范围(最大允许limit-1个,剩下一个留给末尾字符串)
|
||||
if(limit > 0 && list.size() > limit-2){
|
||||
if (limit > 0 && list.size() > limit - 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -380,7 +381,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitToArray(String str, int limit){
|
||||
public static String[] splitToArray(String str, int limit) {
|
||||
return toArray(split(str, limit));
|
||||
}
|
||||
|
||||
@ -388,6 +389,7 @@ public class StrSpliter {
|
||||
|
||||
/**
|
||||
* 通过正则切分字符串
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param separatorPattern 分隔符正则{@link Pattern}
|
||||
* @param limit 限制分片数
|
||||
@ -396,15 +398,15 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static List<String> split(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
if(StringUtil.isEmpty(str)){
|
||||
public static List<String> split(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
if (StringUtil.isEmpty(str)) {
|
||||
return new ArrayList<String>(0);
|
||||
}
|
||||
if(limit == 1){
|
||||
if (limit == 1) {
|
||||
return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty);
|
||||
}
|
||||
|
||||
if(null == separatorPattern){//分隔符为空时按照空白符切分
|
||||
if (null == separatorPattern) {//分隔符为空时按照空白符切分
|
||||
return split(str, limit);
|
||||
}
|
||||
|
||||
@ -412,12 +414,12 @@ public class StrSpliter {
|
||||
final ArrayList<String> list = new ArrayList<>();
|
||||
int len = str.length();
|
||||
int start = 0;
|
||||
while(matcher.find()){
|
||||
while (matcher.find()) {
|
||||
addToList(list, str.substring(start, matcher.start()), isTrim, ignoreEmpty);
|
||||
start = matcher.end();
|
||||
|
||||
//检查是否超出范围(最大允许limit-1个,剩下一个留给末尾字符串)
|
||||
if(limit > 0 && list.size() > limit-2){
|
||||
if (limit > 0 && list.size() > limit - 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -435,7 +437,7 @@ public class StrSpliter {
|
||||
* @return 切分后的集合
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static String[] splitToArray(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty){
|
||||
public static String[] splitToArray(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty) {
|
||||
return toArray(split(str, separatorPattern, limit, isTrim, ignoreEmpty));
|
||||
}
|
||||
|
||||
@ -468,20 +470,22 @@ public class StrSpliter {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
* 将字符串加入List中
|
||||
*
|
||||
* @param list 列表
|
||||
* @param part 被加入的部分
|
||||
* @param isTrim 是否去除两端空白符
|
||||
* @param ignoreEmpty 是否略过空字符串(空字符串不做为一个元素)
|
||||
* @return 列表
|
||||
*/
|
||||
private static List<String> addToList(List<String> list, String part, boolean isTrim, boolean ignoreEmpty){
|
||||
private static List<String> addToList(List<String> list, String part, boolean isTrim, boolean ignoreEmpty) {
|
||||
part = part.toString();
|
||||
if(isTrim){
|
||||
if (isTrim) {
|
||||
part = part.trim();
|
||||
}
|
||||
if(false == ignoreEmpty || false == part.isEmpty()){
|
||||
if (false == ignoreEmpty || false == part.isEmpty()) {
|
||||
list.add(part);
|
||||
}
|
||||
return list;
|
||||
@ -489,10 +493,11 @@ public class StrSpliter {
|
||||
|
||||
/**
|
||||
* List转Array
|
||||
*
|
||||
* @param list List
|
||||
* @return Array
|
||||
*/
|
||||
private static String[] toArray(List<String> list){
|
||||
private static String[] toArray(List<String> list) {
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
//---------------------------------------------------------------------------------------------------------- Private method end
|
||||
|
@ -8,6 +8,8 @@ import java.util.function.Function;
|
||||
/**
|
||||
* 当 Lambda 遇上受检异常
|
||||
* https://segmentfault.com/a/1190000007832130
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class Try {
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
|
||||
* 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.support.xss;
|
||||
|
||||
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
|
||||
/**
|
||||
* SQL过滤
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class SqlFilter {
|
||||
|
||||
/**
|
||||
* SQL注入过滤
|
||||
*
|
||||
* @param str 待验证的字符串
|
||||
*/
|
||||
public static String sqlInject(String str) {
|
||||
if (StringUtil.isBlank(str)) {
|
||||
return null;
|
||||
}
|
||||
//去掉'|"|;|\字符
|
||||
str = str.replace("'", "");
|
||||
str = str.replace("\"", "");
|
||||
str = str.replace(";", "");
|
||||
str = str.replace("\\", "");
|
||||
|
||||
//转换成小写
|
||||
str = str.toLowerCase();
|
||||
|
||||
//非法字符
|
||||
String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alert", "drop"};
|
||||
|
||||
//判断是否包含非法字符
|
||||
for (String keyword : keywords) {
|
||||
if (str.indexOf(keyword) != -1) {
|
||||
throw new RuntimeException("包含非法字符");
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
@ -21,6 +21,8 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
* XSS过滤
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class XssFilter implements Filter {
|
||||
|
||||
@ -29,12 +31,13 @@ public class XssFilter implements Filter {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
ServletRequest requestWrapper = null;
|
||||
if(request instanceof HttpServletRequest) {
|
||||
if (request instanceof HttpServletRequest) {
|
||||
requestWrapper = new XssHttpServletRequestWrapper((HttpServletRequest) request);
|
||||
}
|
||||
if(requestWrapper == null) {
|
||||
if (requestWrapper == null) {
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
chain.doFilter(requestWrapper, response);
|
||||
|
@ -34,14 +34,24 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* XSS过滤处理
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
|
||||
|
||||
//没被包装过的HttpServletRequest(特殊场景,需要自己过滤)
|
||||
/**
|
||||
* 没被包装过的HttpServletRequest(特殊场景,需要自己过滤)
|
||||
*/
|
||||
HttpServletRequest orgRequest;
|
||||
//html过滤
|
||||
|
||||
/**
|
||||
* html过滤
|
||||
*/
|
||||
private final static HTMLFilter htmlFilter = new HTMLFilter();
|
||||
// 缓存报文,支持多次读取流
|
||||
|
||||
/**
|
||||
* 缓存报文,支持多次读取流
|
||||
*/
|
||||
private final byte[] body;
|
||||
|
||||
public XssHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
|
||||
|
@ -30,7 +30,8 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class AESUtil {
|
||||
|
||||
private AESUtil() {}
|
||||
private AESUtil() {
|
||||
}
|
||||
|
||||
public static String genAesKey() {
|
||||
return StringUtil.random(32);
|
||||
|
@ -21,6 +21,8 @@ import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
/**
|
||||
* 字符集工具类
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class Charsets {
|
||||
|
||||
@ -39,6 +41,7 @@ public class Charsets {
|
||||
|
||||
/**
|
||||
* 转换为Charset对象
|
||||
*
|
||||
* @param charsetName 字符集,为空则返回默认字符集
|
||||
* @return Charsets
|
||||
* @throws UnsupportedCharsetException 编码不支持
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user