123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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<String> 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<Long> getCompanyIds() {
- return Arrays.asList(getCompanyId());
- }
- public static List<Long> getUserCompanyIds() {
- try {
- SysUser user = getLoginUser().getUser();
- List<Long> 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;
- }
- }
|