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

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

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

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

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

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

@ -26,6 +26,8 @@ import lombok.extern.slf4j.Slf4j;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.enums.CacheType;
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.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
@ -79,6 +81,9 @@ public class CacheUtil {
/** 热点数据前缀 */
private static String PREFIX_NAME;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
static {
try {
// 读取配置信息
@ -88,9 +93,13 @@ public class CacheUtil {
/***
*
* @return
* @return String
*/
public static String getPrefixName() {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return PREFIX_NAME;
}
@ -102,6 +111,10 @@ public class CacheUtil {
* @return Object
*/
public static Object getTimed(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型
return CacheUtil.get(key, false, false);
}
@ -113,6 +126,10 @@ public class CacheUtil {
* @return Object
*/
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);
}
@ -124,6 +141,10 @@ public class CacheUtil {
* @return <V>
*/
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);
}
@ -135,7 +156,12 @@ public class CacheUtil {
* @param isSaveLocal
* @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);
}
@ -146,6 +172,10 @@ public class CacheUtil {
* @return Object
*/
public static Object getEden(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 转换数据泛型
return CacheUtil.get(key, true, false);
}
@ -157,6 +187,10 @@ public class CacheUtil {
* @return Object
*/
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);
}
@ -168,6 +202,10 @@ public class CacheUtil {
* @return <V>
*/
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);
}
@ -179,7 +217,12 @@ public class CacheUtil {
* @param isSaveLocal
* @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);
}
@ -192,7 +235,12 @@ public class CacheUtil {
* @param isSaveLocal
* @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);
// 转换数据泛型
@ -207,6 +255,10 @@ public class CacheUtil {
* @return Object
*/
private static Object get(final String key, final boolean isEden, final boolean isSaveLocal){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 缓存 Key
String cacheKey = CacheUtil.handleUsualKey(key, isEden);
@ -252,6 +304,10 @@ public class CacheUtil {
* @return <V>
*/
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);
// 转换数据泛型
@ -266,7 +322,12 @@ public class CacheUtil {
* @param vClass Class
* @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);
// 转换数据泛型
@ -280,6 +341,10 @@ public class CacheUtil {
* @return Object
*/
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);
}
@ -290,7 +355,12 @@ public class CacheUtil {
* @param isSaveLocal
* @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 {
// 缓存 Key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -333,6 +403,10 @@ public class CacheUtil {
* @return Object
*/
public static Map<String, Object> getHashAll(final String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 缓存 Key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -376,6 +450,10 @@ public class CacheUtil {
* @return boolean
*/
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);
}
@ -387,6 +465,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean put(final String key, final Object value, final boolean isEden) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 自动处理 key
@ -430,6 +512,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean putHash(final String key, final String field, final Object value) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 处理 key
String cacheKey = CacheUtil.handleKey(CacheType.EDEN_HASH, key);
@ -456,6 +542,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean del(final String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 计数器
int count = 0;
@ -509,6 +599,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean delHash(final String key, final String field) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 计数器
int count = 2;
@ -545,6 +639,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean putNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -564,6 +662,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean delNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -584,6 +686,10 @@ public class CacheUtil {
* @return boolean
*/
public static boolean hasNilFlag(String key) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
// 处理缓存 key
String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key);
@ -604,6 +710,10 @@ public class CacheUtil {
* @return String
*/
public static String handleKey(String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return CacheUtil.handleKey(CacheType.TIMED, key);
}
@ -614,6 +724,10 @@ public class CacheUtil {
* @return String
*/
public static String handleKey(CacheType cacheType, String key){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return PREFIX_NAME + cacheType.getName() + ":" +
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
public void init(CacheProperties cacheProperties){
public void init(CacheProperties cacheProperties,
RedisPlugin redisPlugin,
EhCachePlugin ehCachePlugin){
if(cacheProperties != null){
// 获得 超级管理员
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.opsli.common.exception.TokenException;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
@ -64,6 +65,9 @@ public class CaptchaUtil {
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
static {
CAPTCHA_STRATEGY_LIST = Lists.newArrayListWithCapacity(3);
CAPTCHA_STRATEGY_LIST.add(new CaptchaStrategyBySpec());
@ -77,6 +81,10 @@ public class CaptchaUtil {
* @param uuid UUID
*/
public static void createCaptcha(String uuid, OutputStream out) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (StringUtils.isBlank(uuid)) {
throw new RuntimeException("uuid不能为空");
}
@ -105,6 +113,10 @@ public class CaptchaUtil {
* @param code
*/
public static void validate(String uuid, String code) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断UUID 是否为空
if (StringUtils.isEmpty(uuid)) {
throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_UUID_NULL);
@ -136,6 +148,10 @@ public class CaptchaUtil {
* @return boolean
*/
public static boolean delCaptcha(String uuid) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (StringUtils.isEmpty(uuid)) {
return false;
}
@ -144,15 +160,6 @@ public class CaptchaUtil {
return redisPlugin.del(CacheUtil.getPrefixName() + PREFIX + uuid);
}
// ==========================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
CaptchaUtil.redisPlugin = redisPlugin;
}
// ======================
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 org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redisson.RedissonLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
@ -44,12 +45,19 @@ public class DistributedLockUtil {
/** Redisson 分布式锁 */
private static RedissonLock REDISSON_LOCK;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
*
* @param lockName
* @return boolean
*/
public static boolean lock(String lockName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
boolean isLock = true;
// 分布式上锁
if(REDISSON_LOCK != null){
@ -63,6 +71,10 @@ public class DistributedLockUtil {
* @param lockName
*/
public static void unlock(String lockName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 释放锁
if(REDISSON_LOCK != null){
REDISSON_LOCK.unlockByThread(CacheUtil.getPrefixName() + lockName);
@ -73,11 +85,12 @@ public class DistributedLockUtil {
/**
*
* @param redissonLock
*/
@Autowired(required = false)
public void init(RedissonLock 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.TokenTypeConstants;
import org.opsli.common.exception.JwtException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.JwtMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
@ -35,7 +36,6 @@ import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@Component
public class JwtUtil {
/**
* 120
*/
@ -46,6 +46,8 @@ public class JwtUtil {
*/
private static String ENCRYPT_JWT_INITIAL_SECRET;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* JWT
@ -53,6 +55,10 @@ public class JwtUtil {
* @return boolean
*/
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);
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm).build();
@ -67,6 +73,10 @@ public class JwtUtil {
* @return java.lang.String
*/
public static String getClaim(String token, String claim) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim(claim).asString();
@ -86,6 +96,10 @@ public class JwtUtil {
* @return java.lang.String Token
*/
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();
// 帐号加JWT私钥加密
@ -114,11 +128,9 @@ public class JwtUtil {
/**
*
* @param globalProperties
*/
@Autowired
public void init(GlobalProperties globalProperties){
//
if(globalProperties != null && globalProperties.getAuth() != null
&& globalProperties.getAuth().getToken() != null
){
@ -130,6 +142,8 @@ public class JwtUtil {
JwtUtil.ENCRYPT_JWT_INITIAL_SECRET = globalProperties.getAuth()
.getToken().getSecret();
}
IS_INIT = true;
}
// ==================

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

@ -48,13 +48,19 @@ public class OrgUtil {
/** 用户 Api */
private static UserApi userApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* userId
* @param userId
* @return
* @param userId ID
* @return model
*/
public static UserOrgRefModel getOrgByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_CODE + userId;
@ -113,10 +119,14 @@ public class OrgUtil {
/**
* -
* @param userId
* @return
* @param userId ID
* @return boolean
*/
public static boolean refreshOrg(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){
return true;
}
@ -149,13 +159,16 @@ public class OrgUtil {
}
// =====================================
/**
*
*/
@Autowired
public void setUserApi(UserApi userApi) {
public void init(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.opsli.api.wrapper.system.user.UserModel;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
@ -44,15 +45,17 @@ import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@Lazy(false)
public class SearchHisUtil {
/** 搜索历史缓存数据KEY */
private static final int DEFAULT_COUNT = 10;
/** 缓存前缀 */
private static final String CACHE_PREFIX = "his:username:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
*
@ -60,6 +63,10 @@ public class SearchHisUtil {
* @param 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)){
return null;
}
@ -78,10 +85,14 @@ public class SearchHisUtil {
/**
*
* @param request
* @param request request
* @param keys key
*/
public static void putSearchHis(HttpServletRequest request, List<String> keys) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(request == null || CollUtil.isEmpty(keys)){
return;
}
@ -108,9 +119,14 @@ public class SearchHisUtil {
// ===================================
/**
*
*/
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
public void init(RedisPlugin redisPlugin) {
SearchHisUtil.redisPlugin = redisPlugin;
IS_INIT = true;
}
}

@ -48,13 +48,19 @@ public class TenantUtil {
/** 租户 Api */
private static TenantApi tenantApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* tenantId
* @param tenantId
* @return
* @param tenantId ID
* @return model
*/
public static TenantModel getTenant(String tenantId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_CODE + tenantId;
@ -113,10 +119,14 @@ public class TenantUtil {
/**
* -
* @param tenantId
* @return
* @param tenantId ID
* @return boolean
*/
public static boolean refreshTenant(String tenantId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(tenantId)){
return true;
}
@ -150,13 +160,16 @@ public class TenantUtil {
}
// =====================================
/**
*
*/
@Autowired
public void setTenantApi(TenantApi tenantApi) {
public void init(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.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
@ -74,7 +75,8 @@ public class UserTokenUtil {
public static GlobalProperties.Auth.Login LOGIN_PROPERTIES;
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* user Token
@ -82,6 +84,11 @@ public class UserTokenUtil {
* @return UserTokenUtil.TokenRet
*/
public static ResultVo<UserTokenUtil.TokenRet> createToken(UserModel user) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if (user == null) {
// 生成Token失败
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR);
@ -152,6 +159,11 @@ public class UserTokenUtil {
* @return String
*/
public static String getUserIdByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return null;
}
@ -168,6 +180,11 @@ public class UserTokenUtil {
* @return String
*/
public static String getUserNameByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return null;
}
@ -185,6 +202,11 @@ public class UserTokenUtil {
* @param token token
*/
public static void logout(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return;
}
@ -217,6 +239,11 @@ public class UserTokenUtil {
* @param token token
*/
public static boolean verify(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return false;
}
@ -260,6 +287,11 @@ public class UserTokenUtil {
* @param username
*/
public static void verifyLockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断账号是否临时锁定
Long loseTimeMillis = (Long) redisPlugin.get(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_LOCK_PREFIX + username);
@ -290,6 +322,11 @@ public class UserTokenUtil {
* @param username
*/
public static TokenMsg lockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 如果失败次数 超过阈值 则锁定账号
Long slipNum = redisPlugin.increment(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -317,6 +354,11 @@ public class UserTokenUtil {
* @param username
*/
public static long getSlipCount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
long count = 0L;
Object obj = redisPlugin.get(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -334,6 +376,11 @@ public class UserTokenUtil {
* @param username
*/
public static void clearLockAccount(String username){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 删除失败次数记录
redisPlugin.del(
CacheUtil.getPrefixName() + ACCOUNT_SLIP_COUNT_PREFIX + username);
@ -349,6 +396,10 @@ public class UserTokenUtil {
* token
*/
public static String getRequestToken(HttpServletRequest httpRequest){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
//从header中获取token
String token = httpRequest.getHeader(TOKEN_NAME);
@ -363,7 +414,6 @@ public class UserTokenUtil {
/**
*
* @param globalProperties
*/
@Autowired
public void init(GlobalProperties globalProperties, RedisPlugin redisPlugin){
@ -376,6 +426,8 @@ public class UserTokenUtil {
// Redis 插件
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.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
@ -67,11 +68,18 @@ public class UserUtil {
/** 超级管理员 */
public static String SUPER_ADMIN;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
*
* @return UserModel
*/
public static UserModel getUser(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
String token = TokenThreadLocal.get();
// 如果还是没获取到token 则抛出异常
@ -95,6 +103,9 @@ public class UserUtil {
* @return UserModel
*/
public static UserModel getUser(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID + userId;
@ -161,6 +172,10 @@ public class UserUtil {
* @return UserModel
*/
public static UserModel getUserByUserName(String userName){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_USERNAME + userName;
@ -220,6 +235,10 @@ public class UserUtil {
* @return List
*/
public static List<String> getUserRolesByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_ROLES + userId;
@ -284,6 +303,9 @@ public class UserUtil {
* @return List
*/
public static List<String> getUserAllPermsByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_PERMISSIONS + userId;
@ -348,6 +370,9 @@ public class UserUtil {
* @return List
*/
public static List<MenuModel> getMenuListByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_MENUS + userId;
@ -415,6 +440,10 @@ public class UserUtil {
* @return boolean
*/
public static boolean refreshUser(UserModel user){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(user == null || StringUtils.isEmpty(user.getId())){
return true;
}
@ -475,6 +504,10 @@ public class UserUtil {
* @return boolean
*/
public static boolean refreshUserRoles(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_ROLES + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ROLES + userId);
@ -509,6 +542,11 @@ public class UserUtil {
* @return boolean
*/
public static boolean refreshUserAllPerms(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_PERMISSIONS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId);
@ -544,6 +582,11 @@ public class UserUtil {
* @return boolean
*/
public static boolean refreshUserMenus(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
Object obj = CacheUtil.getTimed(PREFIX_ID_MENUS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId);
@ -577,6 +620,10 @@ public class UserUtil {
* @return String
*/
public static String getTenantId(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 判断权限 如果是 admin 超级管理员 则租户ID清空 且findList 不做处理 否则默认都会做处理
// 如果表中 没有 tenant_id 字段 则不进行多租户处理
@ -594,6 +641,10 @@ public class UserUtil {
* @return String
*/
public static String getRealTenantId(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
UserModel user = getUser();
return user.getTenantId();
}
@ -604,6 +655,11 @@ public class UserUtil {
* @return boolean
*/
public static boolean isHasUpdateTenantPerms(final UserModel currUser){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 排除超级管理员
if(UserUtil.SUPER_ADMIN.equals(currUser.getUsername())){
return true;
@ -622,29 +678,31 @@ public class UserUtil {
* @return String
*/
public static String handlePassword(String password, String secretKey){
return new Md5Hash(password, secretKey).toHex();
}
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// =====================================
@Autowired
public void setUserApi(UserApi userApi) {
UserUtil.userApi = userApi;
return new Md5Hash(password, secretKey).toHex();
}
// =====================================
/**
*
* @param globalProperties
*/
@Autowired
public void init(GlobalProperties globalProperties){
public void init(GlobalProperties globalProperties, UserApi userApi){
if(globalProperties != null && globalProperties.getAuth() != null
&& globalProperties.getAuth().getToken() != null
){
// 获得 超级管理员
UserUtil.SUPER_ADMIN = globalProperties.getAuth().getSuperAdmin();
}
UserUtil.userApi = userApi;
IS_INIT = true;
}
}

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

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

Loading…
Cancel
Save