jhprjv2/ruoyi-common/src/main/java/com/ruoyi/common/utils/SecurityUtils.java

162 lines
4.0 KiB
Java
Raw Normal View History

2023-08-10 21:09:49 +08:00
package com.ruoyi.common.utils;
2023-10-11 23:24:45 +08:00
import com.ruoyi.common.core.domain.entity.SysRole;
2023-08-10 21:09:49 +08:00
import org.springframework.security.core.Authentication;
2024-08-09 23:48:40 +08:00
import org.springframework.security.core.context.SecurityContext;
2023-08-10 21:09:49 +08:00
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.ServiceException;
2023-10-11 23:24:45 +08:00
import java.util.List;
2023-08-10 21:09:49 +08:00
/**
*
*
* @author ruoyi
*/
public class SecurityUtils
{
/**
* ID
**/
public static Long getUserId()
{
try
{
2023-10-11 23:24:45 +08:00
return getLoginUser().getUserId();
2023-08-10 21:09:49 +08:00
}
catch (Exception e)
{
throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
}
}
2023-10-11 23:24:45 +08:00
public static long getRoleId(){
try
{
List<SysRole> list= getLoginUser().getUser().getRoles();
long roleId= list.get(0).getRoleId();
for(SysRole role :list){
if(role.getRoleId()<roleId){
roleId=role.getRoleId();
}
}
return roleId;
}
catch (Exception e)
{
2023-10-12 23:34:17 +08:00
return 0;
2023-10-11 23:24:45 +08:00
}
}
2023-08-10 21:09:49 +08:00
/**
* ID
**/
public static Long getDeptId()
{
try
{
return getLoginUser().getDeptId();
}
catch (Exception e)
{
throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED);
}
}
/**
*
**/
public static String getUsername()
{
try
{
2024-08-09 23:48:40 +08:00
LoginUser loginUser=getLoginUser();
return loginUser!=null?loginUser.getUsername():null;
2023-08-10 21:09:49 +08:00
}
catch (Exception e)
{
throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
}
}
/**
*
**/
public static LoginUser getLoginUser()
{
try
{
2024-08-09 23:48:40 +08:00
Authentication auth=getAuthentication();
if(auth!=null) {
return (LoginUser) auth.getPrincipal();
}
return null;
2023-08-10 21:09:49 +08:00
}
catch (Exception e)
{
throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
}
}
/**
* Authentication
*/
public static Authentication getAuthentication()
{
2024-08-09 23:48:40 +08:00
SecurityContext ctx=SecurityContextHolder.getContext();
return ctx!=null? SecurityContextHolder.getContext().getAuthentication():null;
2023-08-10 21:09:49 +08:00
}
/**
* 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);
}
/**
*
*
* @param userId ID
* @return
*/
public static boolean isAdmin(Long userId)
{
return userId != null && 1L == userId;
}
2023-10-11 23:24:45 +08:00
2024-09-13 00:16:17 +08:00
/**
*
* @return role
*/
public Long getUserFirstRole(){
return getLoginUser().getUser().getRoles().stream().mapToLong(r->r.getRoleId()).min().getAsLong();
}
2023-10-11 23:24:45 +08:00
public static boolean isUserB() {
long roleId=getRoleId();
2024-04-06 01:31:05 +08:00
return 5==roleId || 6==roleId || 7==roleId || 99==roleId || 15==roleId || 16==roleId || 17==roleId;
2023-10-11 23:24:45 +08:00
}
2023-08-10 21:09:49 +08:00
}