疯狂的狮子li
2021-08-17 d3abd6c96e99e32ccfc030230e06ce4b529f2bbd
ruoyi-common/src/main/java/com/ruoyi/common/utils/SecurityUtils.java
@@ -1,19 +1,49 @@
package com.ruoyi.common.utils;
import cn.hutool.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.ServiceException;
/**
 * 安全服务工具类
 *
 *
 * @author ruoyi
 */
public class SecurityUtils
{
    /**
     * 用户ID
     **/
    public static Long getUserId()
    {
        try
        {
            return getLoginUser().getUserId();
        }
        catch (Exception e)
        {
            throw new ServiceException("获取用户ID异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
    /**
     * 获取部门ID
     **/
    public static Long getDeptId()
    {
        try
        {
            return getLoginUser().getDeptId();
        }
        catch (Exception e)
        {
            throw new ServiceException("获取部门ID异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
    /**
     * 获取用户账户
     **/
@@ -25,7 +55,7 @@
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
            throw new ServiceException("获取用户账户异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
@@ -40,7 +70,7 @@
        }
        catch (Exception e)
        {
            throw new CustomException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
            throw new ServiceException("获取用户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
@@ -79,7 +109,7 @@
    /**
     * 是否为管理员
     *
     *
     * @param userId 用户ID
     * @return 结果
     */