commit:权限模块新增分配详情功能

This commit is contained in:
Jerry
2020-11-21 11:35:22 +08:00
parent fcce270119
commit ab470fa0d3
55 changed files with 607 additions and 870 deletions

View File

@@ -74,7 +74,7 @@ public class CacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager manager = new SimpleCacheManager();
//把各个cache注册到cacheManager中CaffeineCache实现了org.springframework.cache.Cache接口
// 把各个cache注册到cacheManager中CaffeineCache实现了org.springframework.cache.Cache接口
ArrayList<CaffeineCache> caches = new ArrayList<>();
for (CacheEnum c : CacheEnum.values()) {
caches.add(new CaffeineCache(c.name(),

View File

@@ -1,6 +1,13 @@
package com.orange.demo.common.core.cache;
import com.orange.demo.common.core.exception.MapCacheAccessException;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
/**
@@ -11,6 +18,7 @@ import java.util.function.Function;
* @author Jerry
* @date 2020-09-24
*/
@Slf4j
public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
/**
@@ -21,6 +29,14 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* 获取字典主键数据的函数对象。
*/
protected Function<V, K> idGetter;
/**
* 由于大部分场景是读取操作,所以使用读写锁提高并发的伸缩性。
*/
protected ReadWriteLock lock = new ReentrantReadWriteLock();
/**
* 超时时长。单位毫秒。
*/
protected static final long TIMEOUT = 2000L;
/**
* 当前对象的构造器函数。
@@ -52,10 +68,27 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @return 全部字段数据列表。
*/
@Override
public synchronized List<V> getAll() {
public List<V> getAll() {
List<V> resultList = new LinkedList<>();
for (Map.Entry<K, V> entry : dataMap.entrySet()) {
resultList.add(entry.getValue());
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
for (Map.Entry<K, V> entry : dataMap.entrySet()) {
resultList.add(entry.getValue());
}
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return resultList;
}
@@ -67,14 +100,31 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @return 对象列表。
*/
@Override
public synchronized List<V> getInList(Set<K> keys) {
public List<V> getInList(Set<K> keys) {
List<V> resultList = new LinkedList<>();
keys.forEach(key -> {
V object = dataMap.get(key);
if (object != null) {
resultList.add(object);
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
keys.forEach(key -> {
V object = dataMap.get(key);
if (object != null) {
resultList.add(object);
}
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
});
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return resultList;
}
@@ -84,14 +134,31 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @param dataList 待缓存的数据列表。
*/
@Override
public synchronized void putAll(List<V> dataList) {
public void putAll(List<V> dataList) {
if (dataList == null) {
return;
}
dataList.forEach(dataObj -> {
K id = idGetter.apply(dataObj);
dataMap.put(id, dataObj);
});
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
dataList.forEach(dataObj -> {
K id = idGetter.apply(dataObj);
dataMap.put(id, dataObj);
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
@@ -101,12 +168,32 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @param force true则强制刷新如果false当缓存中存在数据时不刷新。
*/
@Override
public synchronized void reload(List<V> dataList, boolean force) {
public void reload(List<V> dataList, boolean force) {
if (!force && this.getCount() > 0) {
return;
}
this.invalidateAll();
this.putAll(dataList);
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
dataMap.clear();
dataList.forEach(dataObj -> {
K id = idGetter.apply(dataObj);
dataMap.put(id, dataObj);
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
@@ -116,8 +203,30 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @return 获取到的数据如果没有返回null。
*/
@Override
public synchronized V get(K id) {
return id == null ? null : dataMap.get(id);
public V get(K id) {
if (id == null) {
return null;
}
V data;
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
data = dataMap.get(id);
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return data;
}
/**
@@ -127,8 +236,25 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @param object 字典数据对象。
*/
@Override
public synchronized void put(K id, V object) {
dataMap.put(id, object);
public void put(K id, V object) {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
dataMap.put(id, object);
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
@@ -137,7 +263,7 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @return 返回缓存的数据数量。
*/
@Override
public synchronized int getCount() {
public int getCount() {
return dataMap.size();
}
@@ -148,8 +274,30 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @return 返回被删除的对象如果主键不存在返回null。
*/
@Override
public synchronized V invalidate(K id) {
return id == null ? null : dataMap.remove(id);
public V invalidate(K id) {
if (id == null) {
return null;
}
String exceptionMessage;
V data;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
data = dataMap.remove(id);
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return data;
}
/**
@@ -158,19 +306,53 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
* @param keys 待删除数据的主键集合。
*/
@Override
public synchronized void invalidateSet(Set<K> keys) {
keys.forEach(id -> {
if (id != null) {
dataMap.remove(id);
public void invalidateSet(Set<K> keys) {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
keys.forEach(id -> {
if (id != null) {
dataMap.remove(id);
}
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
});
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
* 清空缓存。
*/
@Override
public synchronized void invalidateAll() {
dataMap.clear();
public void invalidateAll() {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
dataMap.clear();
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
}

View File

@@ -1,9 +1,13 @@
package com.orange.demo.common.core.cache;
import com.orange.demo.common.core.exception.MapCacheAccessException;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
/**
@@ -14,6 +18,7 @@ import java.util.function.Function;
* @author Jerry
* @date 2020-09-24
*/
@Slf4j
public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
/**
@@ -61,8 +66,27 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
* @param parentId 父主键Id。
* @return 子数据列表。
*/
public synchronized List<V> getListByParentId(K parentId) {
return new LinkedList<>(allTreeMap.get(parentId));
public List<V> getListByParentId(K parentId) {
List<V> resultList = new LinkedList<>();
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
resultList.addAll(allTreeMap.get(parentId));
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return resultList;
}
/**
@@ -71,16 +95,33 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
* @param dataList 待缓存的数据列表。
*/
@Override
public synchronized void putAll(List<V> dataList) {
public void putAll(List<V> dataList) {
if (dataList == null) {
return;
}
super.putAll(dataList);
dataList.forEach(data -> {
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
allTreeMap.put(parentId, data);
});
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
super.putAll(dataList);
dataList.forEach(data -> {
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
allTreeMap.put(parentId, data);
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
@@ -90,11 +131,28 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
* @param data 字典数据对象。
*/
@Override
public synchronized void put(K id, V data) {
super.put(id, data);
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
allTreeMap.put(parentId, data);
public void put(K id, V data) {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
super.put(id, data);
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
allTreeMap.put(parentId, data);
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
@@ -104,11 +162,29 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
* @return 返回被删除的对象如果主键不存在返回null。
*/
@Override
public synchronized V invalidate(K id) {
V v = super.invalidate(id);
if (v != null) {
K parentId = parentIdGetter.apply(v);
allTreeMap.remove(parentId, v);
public V invalidate(K id) {
V v;
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
v = super.invalidate(id);
if (v != null) {
K parentId = parentIdGetter.apply(v);
allTreeMap.remove(parentId, v);
}
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
return v;
}
@@ -119,24 +195,58 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
* @param keys 待删除数据的主键集合。
*/
@Override
public synchronized void invalidateSet(Set<K> keys) {
keys.forEach(id -> {
if (id != null) {
V data = dataMap.remove(id);
if (data != null) {
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
public void invalidateSet(Set<K> keys) {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
keys.forEach(id -> {
if (id != null) {
V data = dataMap.remove(id);
if (data != null) {
K parentId = parentIdGetter.apply(data);
allTreeMap.remove(parentId, data);
}
}
});
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
});
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
/**
* 清空缓存。
*/
@Override
public synchronized void invalidateAll() {
super.invalidateAll();
allTreeMap.clear();
public void invalidateAll() {
String exceptionMessage;
try {
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
try {
super.invalidateAll();
allTreeMap.clear();
} finally {
lock.readLock().unlock();
}
} else {
throw new TimeoutException();
}
} catch (Exception e) {
exceptionMessage = String.format(
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
e.getClass().getSimpleName());
log.warn(exceptionMessage);
throw new MapCacheAccessException(exceptionMessage, e);
}
}
}

View File

@@ -8,6 +8,10 @@ package com.orange.demo.common.core.constant;
*/
public final class ApplicationConstant {
/**
* 为字典表数据缓存时,缓存名称的固定后缀。
*/
public static final String DICT_CACHE_NAME_SUFFIX = "-DICT";
/**
* 图片文件上传的父目录。
*/

View File

@@ -37,7 +37,7 @@ public enum ErrorCodeEnum {
INVALID_USER_STATUS("用户状态错误,请刷新后重试!"),
HAS_CHILDREN_DATA("数据验证失败,子数据存在,请刷新后重试!"),
DATA_VALIDATAED_FAILED("数据验证失败,请核对!"),
DATA_VALIDATED_FAILED("数据验证失败,请核对!"),
UPLOAD_FILE_FAILED("文件上传失败,请联系管理员!"),
DATA_SAVE_FAILED("数据保存失败,请联系管理员!"),
DATA_ACCESS_FAILED("数据访问失败,请联系管理员!"),

View File

@@ -0,0 +1,20 @@
package com.orange.demo.common.core.exception;
/**
* 内存缓存访问失败。比如:获取分布式数据锁超时、等待线程中断等。
*
* @author Jerry
* @date 2020-09-24
*/
public class MapCacheAccessException extends RuntimeException {
/**
* 构造函数。
*
* @param msg 错误信息。
* @param cause 原始异常。
*/
public MapCacheAccessException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -20,6 +20,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.util.*;
/**
@@ -45,6 +46,7 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
classSet.add(Double.class);
classSet.add(Boolean.class);
classSet.add(Byte.class);
classSet.add(BigDecimal.class);
classSet.add(Character.class);
}
@@ -100,7 +102,7 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
}
// 获取参数类型。
Class<?> parameterType = parameter.getParameterType();
//基本类型
// 基本类型
if (parameterType.isPrimitive()) {
return parsePrimitive(parameterType.getName(), value);
}
@@ -196,6 +198,12 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
return number.doubleValue();
} else if (parameterType == Byte.class) {
return number.byteValue();
} else if (parameterType == BigDecimal.class) {
if (value instanceof Double || value instanceof Float) {
return BigDecimal.valueOf(number.doubleValue());
} else {
return BigDecimal.valueOf(number.longValue());
}
}
} else if (parameterType == Boolean.class) {
return value.toString();

View File

@@ -76,6 +76,9 @@ public class MyGroupParam extends ArrayList<MyGroupParam.GroupInfo> {
private static GroupBaseData parseGroupBaseData(GroupInfo groupInfo, Class<?> modelClazz) {
GroupBaseData baseData = new GroupBaseData();
if (StringUtils.isBlank(groupInfo.fieldName)) {
throw new IllegalArgumentException("GroupInfo.fieldName can't be EMPTY");
}
String[] stringArray = StringUtils.split(groupInfo.fieldName,'.');
if (stringArray.length == 1) {
baseData.modelName = modelClazz.getSimpleName();

View File

@@ -110,11 +110,11 @@ public class LocalUpDownloader extends BaseUpDownloader {
try {
byte[] bytes = uploadFile.getBytes();
Path path = Paths.get(uploadPath + responseInfo.getFilename());
//如果没有files文件夹则创建
// 如果没有files文件夹则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(uploadPath));
}
//文件写入指定路径
// 文件写入指定路径
Files.write(path, bytes);
} catch (IOException e) {
log.error("Failed to write uploaded file [" + uploadFile.getOriginalFilename() + " ].", e);

View File

@@ -6,6 +6,9 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Map;
/**
* Spring 系统启动应用感知对象主要用于获取Spring Bean的上下文对象后续的代码中可以直接查找系统中加载的Bean对象。
*
@@ -62,6 +65,19 @@ public class ApplicationContextHolder implements ApplicationContextAware {
return applicationContext.getBean(beanType);
}
/**
* 根据Bean的ClassType获取Bean对象列表。
*
* @param beanType Bean的Class类型。。
* @param <T> 返回的Bean类型。
* @return Bean对象列表。
*/
public static <T> Collection<T> getBeanListOfType(Class<T> beanType) {
assertApplicationContext();
Map<String, T> beanMap = applicationContext.getBeansOfType(beanType);
return beanMap == null ? null : beanMap.values();
}
private static void assertApplicationContext() {
if (ApplicationContextHolder.applicationContext == null) {
throw new MyRuntimeException("applicaitonContext属性为null,请检查是否注入了ApplicationContextHolder!");

View File

@@ -31,29 +31,29 @@ public class IpUtil {
*/
public static String getRemoteIpAddress(HttpServletRequest request) {
String ip = null;
//X-Forwarded-ForSquid 服务代理
// X-Forwarded-ForSquid 服务代理
String ipAddresses = request.getHeader("X-Forwarded-For");
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
//Proxy-Client-IPapache 服务代理
// Proxy-Client-IPapache 服务代理
ipAddresses = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
//WL-Proxy-Client-IPweblogic 服务代理
// WL-Proxy-Client-IPweblogic 服务代理
ipAddresses = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
//HTTP_CLIENT_IP有些代理服务器
// HTTP_CLIENT_IP有些代理服务器
ipAddresses = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
//X-Real-IPnginx服务代理
// X-Real-IPnginx服务代理
ipAddresses = request.getHeader("X-Real-IP");
}
//有些网络通过多层代理那么获取到的ip就会有多个一般都是通过逗号,分割开来并且第一个ip为客户端的真实IP
// 有些网络通过多层代理那么获取到的ip就会有多个一般都是通过逗号,分割开来并且第一个ip为客户端的真实IP
if (StringUtils.isNotBlank(ipAddresses)) {
ip = ipAddresses.split(",")[0];
}
//还是不能获取到最后再通过request.getRemoteAddr();获取
// 还是不能获取到最后再通过request.getRemoteAddr();获取
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
ip = request.getRemoteAddr();
}

View File

@@ -50,6 +50,59 @@ public class MyModelUtil {
*/
private static Map<String, Tuple2<String, Integer>> cachedColumnInfoMap = new ConcurrentHashMap<>();
/**
* 拷贝源类型的集合数据到目标类型的集合中,其中源类型和目标类型中的对象字段类型完全相同。
* NOTE: 该函数主要应用于框架中Dto和Model之间的copy特别针对一对一关联的深度copy。
* 在Dto中一对一对象可以使用Map来表示而不需要使用从表对象的Dto。
*
* @param sourceCollection 源类型集合。
* @param targetClazz 目标类型的Class对象。
* @param <S> 源类型。
* @param <T> 目标类型。
* @return copy后的目标类型对象集合。
*/
public static <S, T> List<T> copyCollectionTo(Collection<S> sourceCollection, Class<T> targetClazz) {
List<T> targetList = new LinkedList<>();
if (CollectionUtils.isNotEmpty(sourceCollection)) {
for (S source : sourceCollection) {
try {
T target = targetClazz.newInstance();
BeanUtil.copyProperties(source, target);
targetList.add(target);
} catch (Exception e) {
log.error("Failed to call MyModelUtil.copyCollectionTo", e);
return Collections.emptyList();
}
}
}
return targetList;
}
/**
* 拷贝源类型的对象数据到目标类型的对象中,其中源类型和目标类型中的对象字段类型完全相同。
* NOTE: 该函数主要应用于框架中Dto和Model之间的copy特别针对一对一关联的深度copy。
* 在Dto中一对一对象可以使用Map来表示而不需要使用从表对象的Dto。
*
* @param source 源类型对象。
* @param targetClazz 目标类型的Class对象。
* @param <S> 源类型。
* @param <T> 目标类型。
* @return copy后的目标类型对象。
*/
public static <S, T> T copyTo(S source, Class<T> targetClazz) {
if (source == null) {
return null;
}
try {
T target = targetClazz.newInstance();
BeanUtil.copyProperties(source, target);
return target;
} catch (Exception e) {
log.error("Failed to call MyModelUtil.copyTo", e);
return null;
}
}
/**
* 映射Model对象的字段反射对象获取与该字段对应的数据库列名称。
*

View File

@@ -46,9 +46,9 @@ public class RsaUtil {
// 得到私钥字符串
String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
// 将公钥和私钥保存到Map
//0表示公钥
// 0表示公钥
keyMap.put(0, publicKeyString);
//1表示私钥
// 1表示私钥
keyMap.put(1, privateKeyString);
}
@@ -61,11 +61,11 @@ public class RsaUtil {
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(String str, String publicKey) throws Exception {
//base64编码的公钥
// base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密。后面这个更安全但是SonarQube始终report安全漏洞。"RSA/ECB/PKCS1Padding"
//而浏览器自带的Javascript加密功能目前safari不支持而且用的人也不太多。所以暂时都不考虑了。
// RSA加密。后面这个更安全但是SonarQube始终report安全漏洞。"RSA/ECB/PKCS1Padding"
// 而浏览器自带的Javascript加密功能目前safari不支持而且用的人也不太多。所以暂时都不考虑了。
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
@@ -80,12 +80,12 @@ public class RsaUtil {
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey) throws Exception {
//64位解码加密后的字符串
// 64位解码加密后的字符串
byte[] inputByte = Base64.getDecoder().decode(str);
//base64编码的私钥
// base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
// RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
@@ -93,9 +93,9 @@ public class RsaUtil {
public static void main(String[] args) throws Exception {
long temp = System.currentTimeMillis();
//生成公钥和私钥
// 生成公钥和私钥
genKeyPair();
//加密字符串
// 加密字符串
System.out.println("公钥:" + keyMap.get(0));
System.out.println("私钥:" + keyMap.get(1));
System.out.println("生成密钥消耗时间:" + (System.currentTimeMillis() - temp) / 1000.0 + "");

View File

@@ -9,6 +9,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
* @date 2020-09-24
*/
@EnableConfigurationProperties({IdGeneratorProperties.class})
public class IdGeneratorAutoConfigure {
public class IdGeneratorAutoConfig {
}

View File

@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.orange.demo.common.sequence.config.IdGeneratorAutoConfigure
com.orange.demo.common.sequence.config.IdGeneratorAutoConfig

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>common</artifactId>
<groupId>com.orange.demo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-swagger</artifactId>
<version>1.0.0</version>
<name>common-swagger</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<dependency>
<groupId>com.orange.demo</groupId>
<artifactId>common-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -1,56 +0,0 @@
package com.orange.demo.common.swagger.config;
import com.orange.demo.common.core.annotation.MyRequestBody;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 自动加载bean的配置对象。
*
* @author Jerry
* @date 2020-09-24
*/
@EnableSwagger2
@EnableKnife4j
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
public class SwaggerAutoConfiguration {
@Bean
public Docket upmsDocket(SwaggerProperties properties) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("1. 用户权限分组接口")
.ignoredParameterTypes(MyRequestBody.class)
.apiInfo(apiInfo(properties))
.select()
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage() + ".upms.controller"))
.paths(PathSelectors.any()).build();
}
@Bean
public Docket bizDocket(SwaggerProperties properties) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("2. 业务应用分组接口")
.ignoredParameterTypes(MyRequestBody.class)
.apiInfo(apiInfo(properties))
.select()
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage() + ".app.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(SwaggerProperties properties) {
return new ApiInfoBuilder()
.title(properties.getTitle())
.description(properties.getDescription())
.version(properties.getVersion()).build();
}
}

View File

@@ -1,44 +0,0 @@
package com.orange.demo.common.swagger.config;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
/**
* 配置参数对象。
*
* @author Jerry
* @date 2020-09-24
*/
@Data
@ConfigurationProperties("swagger")
public class SwaggerProperties {
/**
* 是否开启Swagger。
*/
private Boolean enabled;
/**
* Swagger解析的基础包路径。
**/
private String basePackage = "";
/**
* ApiInfo中的标题。
**/
private String title = "";
/**
* ApiInfo中的描述信息。
**/
private String description = "";
/**
* ApiInfo中的版本信息。
**/
private String version = "";
}

View File

@@ -1,85 +0,0 @@
package com.orange.demo.common.swagger.plugin;
import cn.hutool.core.lang.Assert;
import com.orange.demo.common.core.annotation.MyRequestBody;
import com.github.xiaoymin.knife4j.core.conf.Consts;
import javassist.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import springfox.documentation.service.ResolvedMethodParameter;
import java.util.List;
/**
* 通过字节码方式动态创建接口参数封装对象。
*
* @author Jerry
* @date 2020-09-24
*/
@Slf4j
public class ByteBodyUtils {
private static final ClassPool CLASS_POOL = ClassPool.getDefault();
public static Class<?> createDynamicModelClass(String name, List<ResolvedMethodParameter> parameters) {
String clazzName = Consts.BASE_PACKAGE_PREFIX + name;
try {
CtClass tmp = CLASS_POOL.getCtClass(clazzName);
if (tmp != null) {
tmp.detach();
}
} catch (NotFoundException e) {
// 需要吃掉这个异常。
}
CtClass ctClass = CLASS_POOL.makeClass(clazzName);
try {
int fieldCount = 0;
for (ResolvedMethodParameter dynamicParameter : parameters) {
// 因为在调用这个方法之前这些参数都包含MyRequestBody注解。
MyRequestBody myRequestBody =
dynamicParameter.findAnnotation(MyRequestBody.class).orNull();
Assert.notNull(myRequestBody);
String fieldName = dynamicParameter.defaultName().isPresent()
? dynamicParameter.defaultName().get() : "parameter";
if (StringUtils.isNotBlank(myRequestBody.value())) {
fieldName = myRequestBody.value();
}
ctClass.addField(createField(dynamicParameter, fieldName, ctClass));
fieldCount++;
}
if (fieldCount > 0) {
return ctClass.toClass();
}
} catch (Throwable e) {
log.error(e.getMessage());
}
return null;
}
private static CtField createField(ResolvedMethodParameter parameter, String parameterName, CtClass ctClass)
throws NotFoundException, CannotCompileException {
CtField field = new CtField(getFieldType(parameter.getParameterType().getErasedType()), parameterName, ctClass);
field.setModifiers(Modifier.PUBLIC);
return field;
}
private static CtClass getFieldType(Class<?> propetyType) {
CtClass fieldType = null;
try {
if (!propetyType.isAssignableFrom(Void.class)) {
fieldType = CLASS_POOL.get(propetyType.getName());
} else {
fieldType = CLASS_POOL.get(String.class.getName());
}
} catch (NotFoundException e) {
//抛异常
ClassClassPath path = new ClassClassPath(propetyType);
CLASS_POOL.insertClassPath(path);
try {
fieldType = CLASS_POOL.get(propetyType.getName());
} catch (NotFoundException e1) {
log.error(e1.getMessage(), e1);
}
}
return fieldType;
}
}

View File

@@ -1,61 +0,0 @@
package com.orange.demo.common.swagger.plugin;
import com.orange.demo.common.core.annotation.MyRequestBody;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.CaseFormat;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationModelsProviderPlugin;
import springfox.documentation.spi.service.contexts.RequestMappingContext;
import java.util.List;
import java.util.stream.Collectors;
/**
* 生成参数包装类的插件。
*
* @author Jerry
* @date 2020-09-24
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 200)
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
public class DynamicBodyModelPlugin implements OperationModelsProviderPlugin {
private final TypeResolver typeResolver;
public DynamicBodyModelPlugin(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
@Override
public void apply(RequestMappingContext context) {
List<ResolvedMethodParameter> parameterTypes = context.getParameters();
if (CollectionUtils.isEmpty(parameterTypes)) {
return;
}
List<ResolvedMethodParameter> bodyParameter = parameterTypes.stream()
.filter(p -> p.hasParameterAnnotation(MyRequestBody.class)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(bodyParameter)) {
return;
}
String groupName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, context.getGroupName());
String clazzName = groupName + StringUtils.capitalize(context.getName());
Class<?> clazz = ByteBodyUtils.createDynamicModelClass(clazzName, bodyParameter);
if (clazz != null) {
context.operationModelsBuilder().addInputParam(typeResolver.resolve(clazz));
}
}
@Override
public boolean supports(DocumentationType delimiter) {
//支持2.0版本
return delimiter == DocumentationType.SWAGGER_2;
}
}

View File

@@ -1,64 +0,0 @@
package com.orange.demo.common.swagger.plugin;
import com.orange.demo.common.core.annotation.MyRequestBody;
import com.google.common.base.CaseFormat;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spi.service.contexts.ParameterContext;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 构建操作接口参数对象的插件。
*
* @author Jerry
* @date 2020-09-24
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 102)
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
public class DynamicBodyParameterBuilder implements OperationBuilderPlugin {
@Override
public void apply(OperationContext context) {
List<ResolvedMethodParameter> methodParameters = context.getParameters();
List<Parameter> parameters = new ArrayList<>();
if (CollectionUtils.isNotEmpty(methodParameters)) {
List<ResolvedMethodParameter> bodyParameter = methodParameters.stream()
.filter(p -> p.hasParameterAnnotation(MyRequestBody.class)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(bodyParameter)) {
//构造model
String groupName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, context.getGroupName());
String clazzName = groupName + StringUtils.capitalize(context.getName());
ResolvedMethodParameter methodParameter = bodyParameter.get(0);
ParameterContext parameterContext = new ParameterContext(methodParameter,
new ParameterBuilder(),
context.getDocumentationContext(),
context.getGenericsNamingStrategy(),
context);
Parameter parameter = parameterContext.parameterBuilder()
.parameterType("body").modelRef(new ModelRef(clazzName)).name(clazzName).build();
parameters.add(parameter);
}
}
context.operationBuilder().parameters(parameters);
}
@Override
public boolean supports(DocumentationType delimiter) {
return delimiter == DocumentationType.SWAGGER_2;
}
}

View File

@@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.orange.demo.common.swagger.config.SwaggerAutoConfiguration

View File

@@ -14,6 +14,5 @@
<modules>
<module>common-core</module>
<module>common-sequence</module>
<module>common-swagger</module>
</modules>
</project>