first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.starry.admin.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.starry.admin.modules.system.entity.SysDictDataEntity;
|
||||
import com.starry.common.constant.CacheConstants;
|
||||
import com.starry.common.redis.RedisCache;
|
||||
import com.starry.common.utils.SpringUtils;
|
||||
import com.starry.common.utils.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
public class DictUtils {
|
||||
|
||||
/**
|
||||
* 获取 cache key
|
||||
*
|
||||
* @param key 参数键
|
||||
* @return 缓存键key
|
||||
*/
|
||||
public static String getCacheKey(String key) {
|
||||
return CacheConstants.SYS_DICT_KEY + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典缓存
|
||||
*
|
||||
* @param key 参数键
|
||||
* @return dictDatas 字典数据列表
|
||||
*/
|
||||
public static List<SysDictDataEntity> getDictCache(String key) {
|
||||
JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
|
||||
if (StringUtils.isNotNull(arrayCache)) {
|
||||
return arrayCache.toList(SysDictDataEntity.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字典缓存
|
||||
*
|
||||
* @param key 参数键
|
||||
* @param dataList 字典数据列表
|
||||
*/
|
||||
public static void setDictCache(String key, List<SysDictDataEntity> dataList) {
|
||||
SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dataList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空字典缓存
|
||||
*/
|
||||
public static void clearDictCache() {
|
||||
Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*");
|
||||
SpringUtils.getBean(RedisCache.class).deleteObject(keys);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.starry.admin.utils;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
public class ExcelUtils {
|
||||
|
||||
public static List<?> importEasyExcel(InputStream is, Class<?> pojoClass) {
|
||||
return EasyExcel.read(is).head(pojoClass).sheet().doReadSync();
|
||||
}
|
||||
|
||||
public static void exportEasyExcel(HttpServletResponse response, Class<?> pojoClass, List<?> list, String sheetName) {
|
||||
try {
|
||||
EasyExcel.write(response.getOutputStream(), pojoClass).sheet(sheetName).doWrite(list);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.starry.admin.utils;
|
||||
|
||||
|
||||
import com.starry.admin.common.domain.LoginUser;
|
||||
import com.starry.admin.common.security.entity.JwtUser;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
/**
|
||||
* 安全服务工具类
|
||||
*
|
||||
* @author admin
|
||||
*/
|
||||
public class SecurityUtils {
|
||||
/**
|
||||
* 用户ID
|
||||
**/
|
||||
public static String getUserId() {
|
||||
try {
|
||||
return getLoginUser().getUser().getUserId();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取用户ID异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户账户
|
||||
**/
|
||||
public static String getUsername() {
|
||||
try {
|
||||
return getLoginUser().getUsername();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取用户账户异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户
|
||||
**/
|
||||
public static JwtUser getOldLoginUser() {
|
||||
try {
|
||||
return (JwtUser) getAuthentication().getPrincipal();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取用户信息异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户
|
||||
**/
|
||||
public static LoginUser getLoginUser() {
|
||||
try {
|
||||
return (LoginUser) getAuthentication().getPrincipal();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取用户信息异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Authentication
|
||||
*/
|
||||
public static Authentication getAuthentication() {
|
||||
return SecurityContextHolder.getContext().getAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否登录
|
||||
**/
|
||||
public static boolean isLogin() {
|
||||
return getAuthentication() != null && getAuthentication().getPrincipal() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为超级管理员
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean isAdmin(String userId) {
|
||||
return "6dcb2da45fef4768a6511f9c14e18072".equals(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成BCryptPasswordEncoder密码
|
||||
*
|
||||
* @param password 密码
|
||||
* @return 加密字符串
|
||||
*/
|
||||
public static String encryptPassword(String password) {
|
||||
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
return passwordEncoder.encode(password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断密码是否相同
|
||||
*
|
||||
* @param rawPassword 真实密码
|
||||
* @param encodedPassword 加密后字符
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean matchesPassword(String rawPassword, String encodedPassword) {
|
||||
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
return passwordEncoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取租户ID
|
||||
*/
|
||||
public static Long getTenantId() {
|
||||
return com.starry.common.context.SecurityContextHolder.getTenantId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置租户ID
|
||||
*/
|
||||
public static void setTenantId(String tenantId) {
|
||||
com.starry.common.context.SecurityContextHolder.setTenantId(tenantId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user