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