com.github.javaclub.base.utils.ConfigUtils Maven / Gradle / Ivy
package com.github.javaclub.base.utils;
import java.io.Serializable;
import com.github.javaclub.sword.algorithm.UniqueCoder;
import com.github.javaclub.sword.algorithm.crypt.MD5;
import com.github.javaclub.sword.core.Strings;
import com.github.javaclub.toolbox.cache.redis.RedisKeys;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
public class ConfigUtils {
static final String SALT_DEFAULT = "$Kjd40pDOogZ$%sK";
static final int FIXED_LENGTH = 6; // 数字ID转换成字符编码的长度
// {appname}:usertoken:{sysId}:{uid}:tk_{token}
static final String USER_TOKEN_KEY = RedisKeys.custom("usertoken").dir("{}").dir("{}").end("tk_{}").build();
public static final String getAccountTokenKey(String sysIdentity, Serializable uid, String token) {
return Strings.format(USER_TOKEN_KEY, sysIdentity, uid, token);
}
public static final String getAccountTokenKeyPattern(String sysIdentity, Serializable uid) {
return Strings.format(USER_TOKEN_KEY, sysIdentity, uid, "*");
}
public static String generateIdCode(Long idValue) {
return UniqueCoder.INSTANCE.generate(idValue, FIXED_LENGTH);
}
public static Long parseFromIdCode(String idcode) {
if (Strings.isBlank(idcode) || Strings.equalsIgnoreCase("undefined", idcode)
|| Strings.equalsIgnoreCase("null", idcode)
|| FIXED_LENGTH != idcode.length() ) {
return null;
}
try {
return UniqueCoder.INSTANCE.converseAsId(idcode);
} catch (Exception e) {
return null;
}
}
public static String generatePasswordMD5(String passwordRaw) {
String md5Pwd = MD5.getMd5(passwordRaw + getAuthSalt());
return md5Pwd;
}
public static String getTokenSalt() {
return CompositeAppConfigProperties.getInstance().getValueOrDefault("system.configs.auth-token-salt", SALT_DEFAULT);
}
public static boolean isLoginUserLoadFromDb() {
return CompositeAppConfigProperties.getInstance().boolValue("system.configs.login-user-load-from-db");
}
public static int getLoginExpiredHours(String sysId) {
String expireHoursKey = Strings.format("system.configs.login-expired-hours-{}", sysId);
return CompositeAppConfigProperties.getInstance().intValue(expireHoursKey, 24);
}
public static boolean isWxmpLoginShouldAuthorizedMobile() {
String key = "system.configs.login-wxmp-authorized-mobile";
return CompositeAppConfigProperties.getInstance().boolValue(key, true);
}
static String getAuthSalt() {
return CompositeAppConfigProperties.getInstance().getValueOrDefault("system.configs.auth-password-salt", SALT_DEFAULT);
}
}