工具类增加初始化异常,防止在未初始化前使用

v1.4.1
hiparker 5 years ago
parent 534b538f6e
commit 6c40e279dd

@ -33,8 +33,8 @@ public enum CacheType {
private final String desc; private final String desc;
public static CacheType getCacheType(String cacheType) { public static CacheType getCacheType(String cacheType) {
CacheType[] var1 = values(); CacheType[] types = values();
for (CacheType type : var1) { for (CacheType type : types) {
if (type.name.equalsIgnoreCase(cacheType)) { if (type.name.equalsIgnoreCase(cacheType)) {
return type; return type;
} }

@ -34,8 +34,8 @@ public enum CryptoAsymmetricType {
private final String desc; private final String desc;
public static CryptoAsymmetricType getCryptoType(String code) { public static CryptoAsymmetricType getCryptoType(String code) {
CryptoAsymmetricType[] var1 = values(); CryptoAsymmetricType[] types = values();
for (CryptoAsymmetricType type : var1) { for (CryptoAsymmetricType type : types) {
if (type.code.equalsIgnoreCase(code)) { if (type.code.equalsIgnoreCase(code)) {
return type; return type;
} }

@ -64,8 +64,8 @@ public enum DictType {
* @return DictType * @return DictType
*/ */
public static DictType getDict(String type, String value) { public static DictType getDict(String type, String value) {
DictType[] var1 = values(); DictType[] types = values();
for (DictType dict : var1) { for (DictType dict : types) {
if(dict.type.equals(type) && if(dict.type.equals(type) &&
dict.value.equalsIgnoreCase(value) dict.value.equalsIgnoreCase(value)
){ ){
@ -82,8 +82,8 @@ public enum DictType {
* @return boolean * @return boolean
*/ */
public static boolean hasDict(String type, String value) { public static boolean hasDict(String type, String value) {
DictType[] var1 = values(); DictType[] types = values();
for (DictType dict : var1) { for (DictType dict : types) {
if(dict.type.equals(type) && if(dict.type.equals(type) &&
dict.value.equalsIgnoreCase(value)){ dict.value.equalsIgnoreCase(value)){
return true; return true;

@ -23,8 +23,8 @@ public class CheckStrength {
private final String desc; private final String desc;
public static LEVEL getLevel(String code) { public static LEVEL getLevel(String code) {
LEVEL[] var1 = values(); LEVEL[] types = values();
for (LEVEL type : var1) { for (LEVEL type : types) {
if (type.code.equalsIgnoreCase(code)) { if (type.code.equalsIgnoreCase(code)) {
return type; return type;
} }

@ -26,6 +26,8 @@ import lombok.extern.slf4j.Slf4j;
import org.opsli.common.constants.CacheConstants; import org.opsli.common.constants.CacheConstants;
import org.opsli.common.enums.CacheType; import org.opsli.common.enums.CacheType;
import org.opsli.core.autoconfigure.properties.CacheProperties; import org.opsli.core.autoconfigure.properties.CacheProperties;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.ThrowExceptionUtil;
import org.opsli.plugins.cache.EhCachePlugin; import org.opsli.plugins.cache.EhCachePlugin;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -79,6 +81,9 @@ public class CacheUtil {
/** 热点数据前缀 */ /** 热点数据前缀 */
private static String PREFIX_NAME; private static String PREFIX_NAME;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
static { static {
try { try {
// 读取配置信息 // 读取配置信息
@ -88,9 +93,13 @@ public class CacheUtil {
/*** /***
* *
* @return * @return String
*/ */
public static String getPrefixName() { public static String getPrefixName() {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return PREFIX_NAME; return PREFIX_NAME;
} }
@ -102,6 +111,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Object getTimed(final String key){ public static Object getTimed(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(key, false, false); return CacheUtil.get(key, false, false);
} }
@ -113,6 +126,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Object getTimed(final String key, final boolean isSaveLocal){ public static Object getTimed(final String key, final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(key, false, isSaveLocal); return CacheUtil.get(key, false, isSaveLocal);
} }
@ -124,6 +141,10 @@ public class CacheUtil {
* @return <V> * @return <V>
*/ */
public static <V> V getTimed(final Class<V> vClass, final String key){ public static <V> V getTimed(final Class<V> vClass, final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(vClass, key, false, false); return CacheUtil.get(vClass, key, false, false);
} }
@ -135,7 +156,12 @@ public class CacheUtil {
* @param isSaveLocal * @param isSaveLocal
* @return <V> * @return <V>
*/ */
public static <V> V getTimed(final Class<V> vClass, final String key, final boolean isSaveLocal){ public static <V> V getTimed(final Class<V> vClass, final String key,
final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(vClass, key, false, isSaveLocal); return CacheUtil.get(vClass, key, false, isSaveLocal);
} }
@ -146,6 +172,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Object getEden(final String key){ public static Object getEden(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(key, true, false); return CacheUtil.get(key, true, false);
} }
@ -157,6 +187,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Object getEden(final String key, final boolean isSaveLocal){ public static Object getEden(final String key, final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(key, true, isSaveLocal); return CacheUtil.get(key, true, isSaveLocal);
} }
@ -168,6 +202,10 @@ public class CacheUtil {
* @return <V> * @return <V>
*/ */
public static <V> V getEden(final String key, final Class<V> vClass){ public static <V> V getEden(final String key, final Class<V> vClass){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(vClass, key, true, false); return CacheUtil.get(vClass, key, true, false);
} }
@ -179,7 +217,12 @@ public class CacheUtil {
* @param isSaveLocal * @param isSaveLocal
* @return <V> * @return <V>
*/ */
public static <V> V getEden(final String key, final boolean isSaveLocal, final Class<V> vClass){ public static <V> V getEden(final String key, final boolean isSaveLocal,
final Class<V> vClass){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型 // 转换数据泛型
return CacheUtil.get(vClass, key, true, isSaveLocal); return CacheUtil.get(vClass, key, true, isSaveLocal);
} }
@ -192,7 +235,12 @@ public class CacheUtil {
* @param isSaveLocal * @param isSaveLocal
* @return <V> * @return <V>
*/ */
private static <V> V get(final Class<V> vClass, final String key, final boolean isEden, final boolean isSaveLocal){ private static <V> V get(final Class<V> vClass, final String key, final boolean isEden,
final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 获得缓存数据 // 获得缓存数据
Object cacheObj = CacheUtil.get(key, isEden, isSaveLocal); Object cacheObj = CacheUtil.get(key, isEden, isSaveLocal);
// 转换数据泛型 // 转换数据泛型
@ -207,6 +255,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
private static Object get(final String key, final boolean isEden, final boolean isSaveLocal){ private static Object get(final String key, final boolean isEden, final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 缓存 Key // 缓存 Key
String cacheKey = CacheUtil.handleUsualKey(key, isEden); String cacheKey = CacheUtil.handleUsualKey(key, isEden);
@ -252,6 +304,10 @@ public class CacheUtil {
* @return <V> * @return <V>
*/ */
public static <V> V getHash(final Class<V> vClass, final String key, final String field){ public static <V> V getHash(final Class<V> vClass, final String key, final String field){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 获得缓存数据 // 获得缓存数据
Object cacheObj = CacheUtil.getHash(key, field, false); Object cacheObj = CacheUtil.getHash(key, field, false);
// 转换数据泛型 // 转换数据泛型
@ -266,7 +322,12 @@ public class CacheUtil {
* @param vClass Class * @param vClass Class
* @return <V> * @return <V>
*/ */
public static <V> V getHash(final Class<V> vClass, final String key, final String field, final boolean isSaveLocal){ public static <V> V getHash(final Class<V> vClass, final String key,
final String field, final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 获得缓存数据 // 获得缓存数据
Object cacheObj = CacheUtil.getHash(key, field, isSaveLocal); Object cacheObj = CacheUtil.getHash(key, field, isSaveLocal);
// 转换数据泛型 // 转换数据泛型
@ -280,6 +341,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Object getHash(final String key, final String field){ public static Object getHash(final String key, final String field){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return CacheUtil.getHash(key, field, false); return CacheUtil.getHash(key, field, false);
} }
@ -290,7 +355,12 @@ public class CacheUtil {
* @param isSaveLocal * @param isSaveLocal
* @return Object * @return Object
*/ */
public static Object getHash(final String key, final String field, final boolean isSaveLocal){ public static Object getHash(final String key, final String field,
final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 缓存 Key // 缓存 Key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key); String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -333,6 +403,10 @@ public class CacheUtil {
* @return Object * @return Object
*/ */
public static Map<String, Object> getHashAll(final String key){ public static Map<String, Object> getHashAll(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 缓存 Key // 缓存 Key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key); String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -376,6 +450,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean put(final String key, final Object value) { public static boolean put(final String key, final Object value) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return CacheUtil.put(key, value, false); return CacheUtil.put(key, value, false);
} }
@ -387,6 +465,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean put(final String key, final Object value, final boolean isEden) { public static boolean put(final String key, final Object value, final boolean isEden) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 自动处理 key // 自动处理 key
@ -430,6 +512,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean putHash(final String key, final String field, final Object value) { public static boolean putHash(final String key, final String field, final Object value) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 处理 key // 处理 key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key); String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -456,6 +542,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean del(final String key) { public static boolean del(final String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 计数器 // 计数器
int count = 0; int count = 0;
@ -509,6 +599,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean delHash(final String key, final String field) { public static boolean delHash(final String key, final String field) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 计数器 // 计数器
int count = 2; int count = 2;
@ -545,6 +639,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean putNilFlag(String key) { public static boolean putNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 处理缓存 key // 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -564,6 +662,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean delNilFlag(String key) { public static boolean delNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 处理缓存 key // 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -584,6 +686,10 @@ public class CacheUtil {
* @return boolean * @return boolean
*/ */
public static boolean hasNilFlag(String key) { public static boolean hasNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
// 处理缓存 key // 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -604,6 +710,10 @@ public class CacheUtil {
* @return String * @return String
*/ */
public static String handleKey(String key){ public static String handleKey(String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return CacheUtil.handleKey(CacheType.TIMED, key); return CacheUtil.handleKey(CacheType.TIMED, key);
} }
@ -614,6 +724,10 @@ public class CacheUtil {
* @return String * @return String
*/ */
public static String handleKey(CacheType cacheType, String key){ public static String handleKey(CacheType cacheType, String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return PREFIX_NAME + cacheType.getName() + ":" + return PREFIX_NAME + cacheType.getName() + ":" +
key; key;
} }
@ -674,26 +788,22 @@ public class CacheUtil {
} }
} }
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
CacheUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setEhCachePlugin(EhCachePlugin ehCachePlugin) {
CacheUtil.ehCachePlugin = ehCachePlugin;
}
/** /**
* *
* @param cacheProperties
*/ */
@Autowired @Autowired
public void init(CacheProperties cacheProperties){ public void init(CacheProperties cacheProperties,
RedisPlugin redisPlugin,
EhCachePlugin ehCachePlugin){
if(cacheProperties != null){ if(cacheProperties != null){
// 获得 超级管理员 // 获得 超级管理员
CacheUtil.PREFIX_NAME = Convert.toStr(cacheProperties.getPrefix(), "opsli") + ":"; CacheUtil.PREFIX_NAME = Convert.toStr(cacheProperties.getPrefix(), "opsli") + ":";
} }
CacheUtil.redisPlugin = redisPlugin;
CacheUtil.ehCachePlugin = ehCachePlugin;
IS_INIT = true;
} }
} }

@ -24,6 +24,7 @@ import com.wf.captcha.base.Captcha;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.TokenMsg; import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -64,6 +65,9 @@ public class CaptchaUtil {
/** Redis插件 */ /** Redis插件 */
private static RedisPlugin redisPlugin; private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
static { static {
CAPTCHA_STRATEGY_LIST = Lists.newArrayListWithCapacity(3); CAPTCHA_STRATEGY_LIST = Lists.newArrayListWithCapacity(3);
CAPTCHA_STRATEGY_LIST.add(new CaptchaStrategyBySpec()); CAPTCHA_STRATEGY_LIST.add(new CaptchaStrategyBySpec());
@ -77,6 +81,10 @@ public class CaptchaUtil {
* @param uuid UUID * @param uuid UUID
*/ */
public static void createCaptcha(String uuid, OutputStream out) { public static void createCaptcha(String uuid, OutputStream out) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (StringUtils.isBlank(uuid)) { if (StringUtils.isBlank(uuid)) {
throw new RuntimeException("uuid不能为空"); throw new RuntimeException("uuid不能为空");
} }
@ -105,6 +113,10 @@ public class CaptchaUtil {
* @param code * @param code
*/ */
public static void validate(String uuid, String code) { public static void validate(String uuid, String code) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断UUID 是否为空 // 判断UUID 是否为空
if (StringUtils.isEmpty(uuid)) { if (StringUtils.isEmpty(uuid)) {
throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_UUID_NULL); throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_UUID_NULL);
@ -136,6 +148,10 @@ public class CaptchaUtil {
* @return boolean * @return boolean
*/ */
public static boolean delCaptcha(String uuid) { public static boolean delCaptcha(String uuid) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (StringUtils.isEmpty(uuid)) { if (StringUtils.isEmpty(uuid)) {
return false; return false;
} }
@ -144,15 +160,6 @@ public class CaptchaUtil {
return redisPlugin.del(CacheUtil.getPrefixName() + PREFIX + uuid); return redisPlugin.del(CacheUtil.getPrefixName() + PREFIX + uuid);
} }
// ==========================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
CaptchaUtil.redisPlugin = redisPlugin;
}
// ====================== // ======================
public interface CaptchaStrategy{ public interface CaptchaStrategy{
@ -202,4 +209,16 @@ public class CaptchaUtil {
} }
} }
// ==========================
/**
*
*/
@Autowired
public void init(RedisPlugin redisPlugin) {
CaptchaUtil.redisPlugin = redisPlugin;
IS_INIT = true;
}
} }

@ -17,6 +17,7 @@ package org.opsli.core.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redisson.RedissonLock; import org.opsli.plugins.redisson.RedissonLock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
@ -44,12 +45,19 @@ public class DistributedLockUtil {
/** Redisson 分布式锁 */ /** Redisson 分布式锁 */
private static RedissonLock REDISSON_LOCK; private static RedissonLock REDISSON_LOCK;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* *
* @param lockName * @param lockName
* @return boolean * @return boolean
*/ */
public static boolean lock(String lockName){ public static boolean lock(String lockName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
boolean isLock = true; boolean isLock = true;
// 分布式上锁 // 分布式上锁
if(REDISSON_LOCK != null){ if(REDISSON_LOCK != null){
@ -63,6 +71,10 @@ public class DistributedLockUtil {
* @param lockName * @param lockName
*/ */
public static void unlock(String lockName){ public static void unlock(String lockName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 释放锁 // 释放锁
if(REDISSON_LOCK != null){ if(REDISSON_LOCK != null){
REDISSON_LOCK.unlockByThread(CacheUtil.getPrefixName() + lockName); REDISSON_LOCK.unlockByThread(CacheUtil.getPrefixName() + lockName);
@ -73,11 +85,12 @@ public class DistributedLockUtil {
/** /**
* *
* @param redissonLock
*/ */
@Autowired(required = false) @Autowired(required = false)
public void init(RedissonLock redissonLock){ public void init(RedissonLock redissonLock){
DistributedLockUtil.REDISSON_LOCK = redissonLock; DistributedLockUtil.REDISSON_LOCK = redissonLock;
IS_INIT = true;
} }

@ -14,6 +14,7 @@ import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.common.constants.SignConstants; import org.opsli.common.constants.SignConstants;
import org.opsli.common.constants.TokenTypeConstants; import org.opsli.common.constants.TokenTypeConstants;
import org.opsli.common.exception.JwtException; import org.opsli.common.exception.JwtException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.JwtMsg; import org.opsli.core.msg.JwtMsg;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
@ -35,7 +36,6 @@ import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@Component @Component
public class JwtUtil { public class JwtUtil {
/** /**
* 120 * 120
*/ */
@ -46,6 +46,8 @@ public class JwtUtil {
*/ */
private static String ENCRYPT_JWT_INITIAL_SECRET; private static String ENCRYPT_JWT_INITIAL_SECRET;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* JWT * JWT
@ -53,6 +55,10 @@ public class JwtUtil {
* @return boolean * @return boolean
*/ */
public static boolean verify(String token) { public static boolean verify(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
String secret = getClaim(token, SignConstants.ACCOUNT) + Base64.decodeStr(ENCRYPT_JWT_INITIAL_SECRET); String secret = getClaim(token, SignConstants.ACCOUNT) + Base64.decodeStr(ENCRYPT_JWT_INITIAL_SECRET);
Algorithm algorithm = Algorithm.HMAC256(secret); Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm).build(); JWTVerifier verifier = JWT.require(algorithm).build();
@ -67,6 +73,10 @@ public class JwtUtil {
* @return java.lang.String * @return java.lang.String
*/ */
public static String getClaim(String token, String claim) { public static String getClaim(String token, String claim) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try { try {
DecodedJWT jwt = JWT.decode(token); DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim(claim).asString(); return jwt.getClaim(claim).asString();
@ -86,6 +96,10 @@ public class JwtUtil {
* @return java.lang.String Token * @return java.lang.String Token
*/ */
public static String sign(String tokenType, String account, String userId, boolean isExpire) { public static String sign(String tokenType, String account, String userId, boolean isExpire) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 时间戳 // 时间戳
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
// 帐号加JWT私钥加密 // 帐号加JWT私钥加密
@ -114,11 +128,9 @@ public class JwtUtil {
/** /**
* *
* @param globalProperties
*/ */
@Autowired @Autowired
public void init(GlobalProperties globalProperties){ public void init(GlobalProperties globalProperties){
//
if(globalProperties != null && globalProperties.getAuth() != null if(globalProperties != null && globalProperties.getAuth() != null
&& globalProperties.getAuth().getToken() != null && globalProperties.getAuth().getToken() != null
){ ){
@ -130,6 +142,8 @@ public class JwtUtil {
JwtUtil.ENCRYPT_JWT_INITIAL_SECRET = globalProperties.getAuth() JwtUtil.ENCRYPT_JWT_INITIAL_SECRET = globalProperties.getAuth()
.getToken().getSecret(); .getToken().getSecret();
} }
IS_INIT = true;
} }
// ================== // ==================

@ -48,13 +48,19 @@ public class MenuUtil {
/** 菜单 Api */ /** 菜单 Api */
private static MenuApi menuApi; private static MenuApi menuApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* *
* @param permissions * @param permissions
* @return * @return model
*/ */
public static MenuModel getMenuByPermissions(String permissions){ public static MenuModel getMenuByPermissions(String permissions){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_CODE + permissions; String cacheKey = PREFIX_CODE + permissions;
@ -113,10 +119,14 @@ public class MenuUtil {
/** /**
* - * -
* @param menu * @param menu model
* @return * @return boolean
*/ */
public static boolean refreshMenu(MenuModel menu){ public static boolean refreshMenu(MenuModel menu){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(menu == null || StringUtils.isEmpty(menu.getPermissions())){ if(menu == null || StringUtils.isEmpty(menu.getPermissions())){
return true; return true;
} }
@ -154,9 +164,14 @@ public class MenuUtil {
// ===================================== // =====================================
/**
*
*/
@Autowired @Autowired
public void setMenuApi(MenuApi menuApi) { public void init(MenuApi menuApi) {
MenuUtil.menuApi = menuApi; MenuUtil.menuApi = menuApi;
IS_INIT = true;
} }
} }

@ -48,13 +48,19 @@ public class OrgUtil {
/** 用户 Api */ /** 用户 Api */
private static UserApi userApi; private static UserApi userApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* userId * userId
* @param userId * @param userId ID
* @return * @return model
*/ */
public static UserOrgRefModel getOrgByUserId(String userId){ public static UserOrgRefModel getOrgByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_CODE + userId; String cacheKey = PREFIX_CODE + userId;
@ -113,10 +119,14 @@ public class OrgUtil {
/** /**
* - * -
* @param userId * @param userId ID
* @return * @return boolean
*/ */
public static boolean refreshOrg(String userId){ public static boolean refreshOrg(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){ if(StringUtils.isEmpty(userId)){
return true; return true;
} }
@ -149,13 +159,16 @@ public class OrgUtil {
} }
// ===================================== // =====================================
/**
*
*/
@Autowired @Autowired
public void setUserApi(UserApi userApi) { public void init(UserApi userApi) {
OrgUtil.userApi = userApi; OrgUtil.userApi = userApi;
IS_INIT = true;
} }
} }

@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
@ -44,15 +45,17 @@ import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@Lazy(false) @Lazy(false)
public class SearchHisUtil { public class SearchHisUtil {
/** 搜索历史缓存数据KEY */ /** 搜索历史缓存数据KEY */
private static final int DEFAULT_COUNT = 10; private static final int DEFAULT_COUNT = 10;
/** 缓存前缀 */
private static final String CACHE_PREFIX = "his:username:"; private static final String CACHE_PREFIX = "his:username:";
/** Redis插件 */ /** Redis插件 */
private static RedisPlugin redisPlugin; private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* *
@ -60,6 +63,10 @@ public class SearchHisUtil {
* @param count * @param count
*/ */
public static Set<Object> getSearchHis(HttpServletRequest request, String key, Integer count) { public static Set<Object> getSearchHis(HttpServletRequest request, String key, Integer count) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(request == null || StringUtils.isEmpty(key)){ if(request == null || StringUtils.isEmpty(key)){
return null; return null;
} }
@ -78,10 +85,14 @@ public class SearchHisUtil {
/** /**
* *
* @param request * @param request request
* @param keys key * @param keys key
*/ */
public static void putSearchHis(HttpServletRequest request, List<String> keys) { public static void putSearchHis(HttpServletRequest request, List<String> keys) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(request == null || CollUtil.isEmpty(keys)){ if(request == null || CollUtil.isEmpty(keys)){
return; return;
} }
@ -108,9 +119,14 @@ public class SearchHisUtil {
// =================================== // ===================================
/**
*
*/
@Autowired @Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) { public void init(RedisPlugin redisPlugin) {
SearchHisUtil.redisPlugin = redisPlugin; SearchHisUtil.redisPlugin = redisPlugin;
IS_INIT = true;
} }
} }

@ -48,13 +48,19 @@ public class TenantUtil {
/** 租户 Api */ /** 租户 Api */
private static TenantApi tenantApi; private static TenantApi tenantApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* tenantId * tenantId
* @param tenantId * @param tenantId ID
* @return * @return model
*/ */
public static TenantModel getTenant(String tenantId){ public static TenantModel getTenant(String tenantId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_CODE + tenantId; String cacheKey = PREFIX_CODE + tenantId;
@ -113,10 +119,14 @@ public class TenantUtil {
/** /**
* - * -
* @param tenantId * @param tenantId ID
* @return * @return boolean
*/ */
public static boolean refreshTenant(String tenantId){ public static boolean refreshTenant(String tenantId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(tenantId)){ if(StringUtils.isEmpty(tenantId)){
return true; return true;
} }
@ -150,13 +160,16 @@ public class TenantUtil {
} }
// ===================================== // =====================================
/**
*
*/
@Autowired @Autowired
public void setTenantApi(TenantApi tenantApi) { public void init(TenantApi tenantApi) {
TenantUtil.tenantApi = tenantApi; TenantUtil.tenantApi = tenantApi;
IS_INIT = true;
} }
} }

@ -35,6 +35,7 @@ import org.opsli.common.enums.LoginLimitRefuse;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.core.autoconfigure.properties.GlobalProperties; import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.TokenMsg; import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -74,7 +75,8 @@ public class UserTokenUtil {
public static GlobalProperties.Auth.Login LOGIN_PROPERTIES; public static GlobalProperties.Auth.Login LOGIN_PROPERTIES;
/** Redis插件 */ /** Redis插件 */
private static RedisPlugin redisPlugin; private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* user Token * user Token
@ -82,6 +84,11 @@ public class UserTokenUtil {
* @return UserTokenUtil.TokenRet * @return UserTokenUtil.TokenRet
*/ */
public static ResultVo<UserTokenUtil.TokenRet> createToken(UserModel user) { public static ResultVo<UserTokenUtil.TokenRet> createToken(UserModel user) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (user == null) { if (user == null) {
// 生成Token失败 // 生成Token失败
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR); throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR);
@ -152,6 +159,11 @@ public class UserTokenUtil {
* @return String * @return String
*/ */
public static String getUserIdByToken(String token) { public static String getUserIdByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){ if(StringUtils.isEmpty(token)){
return null; return null;
} }
@ -168,6 +180,11 @@ public class UserTokenUtil {
* @return String * @return String
*/ */
public static String getUserNameByToken(String token) { public static String getUserNameByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){ if(StringUtils.isEmpty(token)){
return null; return null;
} }
@ -185,6 +202,11 @@ public class UserTokenUtil {
* @param token token * @param token token
*/ */
public static void logout(String token) { public static void logout(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){ if(StringUtils.isEmpty(token)){
return; return;
} }
@ -217,6 +239,11 @@ public class UserTokenUtil {
* @param token token * @param token token
*/ */
public static boolean verify(String token) { public static boolean verify(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){ if(StringUtils.isEmpty(token)){
return false; return false;
} }
@ -260,6 +287,11 @@ public class UserTokenUtil {
* @param username * @param username
*/ */
public static void verifyLockAccount(String username){ public static void verifyLockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断账号是否临时锁定 // 判断账号是否临时锁定
Long loseTimeMillis = (Long) redisPlugin.get( Long loseTimeMillis = (Long) redisPlugin.get(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_LOCK_PREFIX + username); CacheUtil.getPrefixName() + ACCOUNT_SLIP_LOCK_PREFIX + username);
@ -290,6 +322,11 @@ public class UserTokenUtil {
* @param username * @param username
*/ */
public static TokenMsg lockAccount(String username){ public static TokenMsg lockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 如果失败次数 超过阈值 则锁定账号 // 如果失败次数 超过阈值 则锁定账号
Long slipNum = redisPlugin.increment( Long slipNum = redisPlugin.increment(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username); CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -317,6 +354,11 @@ public class UserTokenUtil {
* @param username * @param username
*/ */
public static long getSlipCount(String username){ public static long getSlipCount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
long count = 0L; long count = 0L;
Object obj = redisPlugin.get( Object obj = redisPlugin.get(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username); CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -334,6 +376,11 @@ public class UserTokenUtil {
* @param username * @param username
*/ */
public static void clearLockAccount(String username){ public static void clearLockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 删除失败次数记录 // 删除失败次数记录
redisPlugin.del( redisPlugin.del(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username); CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -349,6 +396,10 @@ public class UserTokenUtil {
* token * token
*/ */
public static String getRequestToken(HttpServletRequest httpRequest){ public static String getRequestToken(HttpServletRequest httpRequest){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
//从header中获取token //从header中获取token
String token = httpRequest.getHeader(TOKEN_NAME); String token = httpRequest.getHeader(TOKEN_NAME);
@ -363,7 +414,6 @@ public class UserTokenUtil {
/** /**
* *
* @param globalProperties
*/ */
@Autowired @Autowired
public void init(GlobalProperties globalProperties, RedisPlugin redisPlugin){ public void init(GlobalProperties globalProperties, RedisPlugin redisPlugin){
@ -376,6 +426,8 @@ public class UserTokenUtil {
// Redis 插件 // Redis 插件
UserTokenUtil.redisPlugin = redisPlugin; UserTokenUtil.redisPlugin = redisPlugin;
IS_INIT = true;
} }

@ -24,6 +24,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi; import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.wrapper.system.menu.MenuModel; import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.api.TokenThreadLocal; import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.core.autoconfigure.properties.GlobalProperties; import org.opsli.core.autoconfigure.properties.GlobalProperties;
@ -67,11 +68,18 @@ public class UserUtil {
/** 超级管理员 */ /** 超级管理员 */
public static String SUPER_ADMIN; public static String SUPER_ADMIN;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/** /**
* *
* @return UserModel * @return UserModel
*/ */
public static UserModel getUser(){ public static UserModel getUser(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
String token = TokenThreadLocal.get(); String token = TokenThreadLocal.get();
// 如果还是没获取到token 则抛出异常 // 如果还是没获取到token 则抛出异常
@ -95,6 +103,9 @@ public class UserUtil {
* @return UserModel * @return UserModel
*/ */
public static UserModel getUser(String userId){ public static UserModel getUser(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_ID + userId; String cacheKey = PREFIX_ID + userId;
@ -161,6 +172,10 @@ public class UserUtil {
* @return UserModel * @return UserModel
*/ */
public static UserModel getUserByUserName(String userName){ public static UserModel getUserByUserName(String userName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_USERNAME + userName; String cacheKey = PREFIX_USERNAME + userName;
@ -220,6 +235,10 @@ public class UserUtil {
* @return List * @return List
*/ */
public static List<String> getUserRolesByUserId(String userId){ public static List<String> getUserRolesByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_ID_ROLES + userId; String cacheKey = PREFIX_ID_ROLES + userId;
@ -284,6 +303,9 @@ public class UserUtil {
* @return List * @return List
*/ */
public static List<String> getUserAllPermsByUserId(String userId){ public static List<String> getUserAllPermsByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_ID_PERMISSIONS + userId; String cacheKey = PREFIX_ID_PERMISSIONS + userId;
@ -348,6 +370,9 @@ public class UserUtil {
* @return List * @return List
*/ */
public static List<MenuModel> getMenuListByUserId(String userId){ public static List<MenuModel> getMenuListByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key // 缓存Key
String cacheKey = PREFIX_ID_MENUS + userId; String cacheKey = PREFIX_ID_MENUS + userId;
@ -415,6 +440,10 @@ public class UserUtil {
* @return boolean * @return boolean
*/ */
public static boolean refreshUser(UserModel user){ public static boolean refreshUser(UserModel user){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(user == null || StringUtils.isEmpty(user.getId())){ if(user == null || StringUtils.isEmpty(user.getId())){
return true; return true;
} }
@ -475,6 +504,10 @@ public class UserUtil {
* @return boolean * @return boolean
*/ */
public static boolean refreshUserRoles(String userId){ public static boolean refreshUserRoles(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_ROLES + userId); Object obj = CacheUtil.getTimed(PREFIX_ID_ROLES + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ROLES + userId); boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ROLES + userId);
@ -509,6 +542,11 @@ public class UserUtil {
* @return boolean * @return boolean
*/ */
public static boolean refreshUserAllPerms(String userId){ public static boolean refreshUserAllPerms(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_PERMISSIONS + userId); Object obj = CacheUtil.getTimed(PREFIX_ID_PERMISSIONS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId); boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId);
@ -544,6 +582,11 @@ public class UserUtil {
* @return boolean * @return boolean
*/ */
public static boolean refreshUserMenus(String userId){ public static boolean refreshUserMenus(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_MENUS + userId); Object obj = CacheUtil.getTimed(PREFIX_ID_MENUS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId); boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId);
@ -577,6 +620,10 @@ public class UserUtil {
* @return String * @return String
*/ */
public static String getTenantId(){ public static String getTenantId(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断权限 如果是 admin 超级管理员 则租户ID清空 且findList 不做处理 否则默认都会做处理 // 判断权限 如果是 admin 超级管理员 则租户ID清空 且findList 不做处理 否则默认都会做处理
// 如果表中 没有 tenant_id 字段 则不进行多租户处理 // 如果表中 没有 tenant_id 字段 则不进行多租户处理
@ -594,6 +641,10 @@ public class UserUtil {
* @return String * @return String
*/ */
public static String getRealTenantId(){ public static String getRealTenantId(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
UserModel user = getUser(); UserModel user = getUser();
return user.getTenantId(); return user.getTenantId();
} }
@ -604,6 +655,11 @@ public class UserUtil {
* @return boolean * @return boolean
*/ */
public static boolean isHasUpdateTenantPerms(final UserModel currUser){ public static boolean isHasUpdateTenantPerms(final UserModel currUser){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 排除超级管理员 // 排除超级管理员
if(UserUtil.SUPER_ADMIN.equals(currUser.getUsername())){ if(UserUtil.SUPER_ADMIN.equals(currUser.getUsername())){
return true; return true;
@ -622,29 +678,31 @@ public class UserUtil {
* @return String * @return String
*/ */
public static String handlePassword(String password, String secretKey){ public static String handlePassword(String password, String secretKey){
return new Md5Hash(password, secretKey).toHex(); // 判断 工具类是否初始化完成
} ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// =====================================
@Autowired return new Md5Hash(password, secretKey).toHex();
public void setUserApi(UserApi userApi) {
UserUtil.userApi = userApi;
} }
// =====================================
/** /**
* *
* @param globalProperties
*/ */
@Autowired @Autowired
public void init(GlobalProperties globalProperties){ public void init(GlobalProperties globalProperties, UserApi userApi){
if(globalProperties != null && globalProperties.getAuth() != null if(globalProperties != null && globalProperties.getAuth() != null
&& globalProperties.getAuth().getToken() != null && globalProperties.getAuth().getToken() != null
){ ){
// 获得 超级管理员 // 获得 超级管理员
UserUtil.SUPER_ADMIN = globalProperties.getAuth().getSuperAdmin(); UserUtil.SUPER_ADMIN = globalProperties.getAuth().getSuperAdmin();
} }
UserUtil.userApi = userApi;
IS_INIT = true;
} }
} }

@ -36,8 +36,8 @@ public enum EmailType {
private final String desc; private final String desc;
public static EmailType getType(String cacheType) { public static EmailType getType(String cacheType) {
EmailType[] var1 = values(); EmailType[] types = values();
for (EmailType type : var1) { for (EmailType type : types) {
if (type.code.equalsIgnoreCase(cacheType)) { if (type.code.equalsIgnoreCase(cacheType)) {
return type; return type;
} }

@ -37,15 +37,12 @@ public enum RedissonType {
private final String desc; private final String desc;
public static RedissonType getType(String type) { public static RedissonType getType(String type) {
RedissonType[] var1 = values(); RedissonType[] types = values();
int var2 = var1.length; for (RedissonType e : types) {
for (RedissonType e : var1) {
if (e.type.equalsIgnoreCase(type)) { if (e.type.equalsIgnoreCase(type)) {
return e; return e;
} }
} }
return null; return null;
} }

Loading…
Cancel
Save