package com.ruoyi.common.utils; import cn.hutool.core.util.ObjectUtil; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.exception.ServiceException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * 安全服务工具类 * * @author ruoyi */ public class SecurityUtils { private static final ThreadLocal HOLDER_TENANT_ID = new ThreadLocal<>(); /** * 用户ID **/ public static Long getUserId() { try { return getLoginUser().getUserId(); } catch (Exception e) { throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED); } } /** * 获取公司ID **/ public static Long getCompanyId() { try { return getLoginUser().getUser().getCompanyId(); } catch (Exception e) { throw new ServiceException("获取用户归属公司ID异常", HttpStatus.UNAUTHORIZED); } } /** * 获取关联公司列表 **/ public static List getCompanyIds() { return Arrays.asList(getCompanyId()); } public static List getUserCompanyIds() { try { SysUser user = getLoginUser().getUser(); List companySetArr; //处理关联归属公司 String companySet = user.getCompanySet(); if (ObjectUtil.isEmpty(companySet)) { companySetArr = new ArrayList<>(); } else { companySetArr = Arrays.asList(companySet.split(",")) .stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()); } companySetArr.add(user.getCompanyId());//添加当前用户公司 return companySetArr; } catch (Exception e) { throw new ServiceException("获取用户关联公司列表异常"); } } /** * 获取部门ID **/ public static Long getDeptId() { try { return getLoginUser().getDeptId(); } catch (Exception e) { throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED); } } /** * 获取用户账户 **/ public static String getUsername() { try { return getLoginUser().getUsername(); } catch (Exception e) { throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED); } } /** * 获取租户id **/ public static String getTenantId() { try { String tenantId = HOLDER_TENANT_ID.get(); if (ObjectUtil.isNotEmpty(tenantId)) { return tenantId; } return getLoginUser().getUser().getTenantId(); } catch (Exception e) { throw new ServiceException("获取租户id异常", HttpStatus.UNAUTHORIZED); } } /** * 临时设置租户Id(只在当前线程有效) */ public static void setTenantId(String tenantId) { HOLDER_TENANT_ID.set(tenantId); } /** * 清除临时设置的租户Id */ public static void clearTenantId() { HOLDER_TENANT_ID.remove(); } /** * 获取用户 **/ public static LoginUser getLoginUser() { try { return (LoginUser) getAuthentication().getPrincipal(); } catch (Exception e) { throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED); } } /** * 获取Authentication */ public static Authentication getAuthentication() { return SecurityContextHolder.getContext().getAuthentication(); } /** * 生成BCryptPasswordEncoder密码 * * @param password 密码 * @return 加密字符串 */ public static String encryptPassword(String password) { // //密码强度校验 // Pattern p = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?!.*([a-zA-Z0-9])\\1{2}).{8,16}$"); // Matcher m = p.matcher(password); // if(!m.find()){ // throw new ServiceException("密码需要包含 非连续的 大写英文字母 小写英文字母 数字"); // } 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); } /** * 是否为管理员 * * @param userId 用户ID * @return 结果 */ public static boolean isAdmin(Long userId) { return userId != null && 1L == userId; } }