Files
jsowell-charger-web/jsowell-common/src/main/java/com/jsowell/common/util/SecurityUtils.java

152 lines
4.1 KiB
Java
Raw Normal View History

2023-03-04 16:29:55 +08:00
package com.jsowell.common.util;
2023-05-13 17:13:24 +08:00
import com.alibaba.fastjson2.JSONObject;
2023-03-09 11:42:51 +08:00
import com.google.common.collect.Lists;
2023-03-04 16:29:55 +08:00
import com.jsowell.common.constant.HttpStatus;
2023-03-09 11:42:51 +08:00
import com.jsowell.common.core.domain.entity.SysDept;
import com.jsowell.common.core.domain.entity.SysRole;
import com.jsowell.common.core.domain.entity.SysUser;
2023-03-04 16:29:55 +08:00
import com.jsowell.common.core.domain.model.LoginUser;
2023-03-09 11:42:51 +08:00
import com.jsowell.common.core.domain.vo.AuthorizedDeptVO;
2023-03-04 16:29:55 +08:00
import com.jsowell.common.exception.ServiceException;
2023-05-13 17:10:52 +08:00
import lombok.extern.slf4j.Slf4j;
2023-03-09 11:42:51 +08:00
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.List;
2023-03-04 16:29:55 +08:00
/**
* 安全服务工具类
*
* @author jsowell
*/
2023-05-13 17:10:52 +08:00
@Slf4j
2023-03-04 16:29:55 +08:00
public class SecurityUtils {
/**
* 用户ID
**/
public static Long getUserId() {
try {
return getLoginUser().getUserId();
} catch (Exception e) {
throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
}
}
/**
* 获取部门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);
}
}
/**
* 获取用户
**/
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) {
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-03-09 11:42:51 +08:00
// 查询登录用户权限
public static AuthorizedDeptVO getAuthorizedMap() {
// 登录用户
LoginUser loginUser = getLoginUser();
if (loginUser == null) {
return null;
}
// 用户
SysUser user = loginUser.getUser();
if (user == null) {
return null;
}
AuthorizedDeptVO resultVO = AuthorizedDeptVO.builder().build();
// 登录用户所属部门(运营商或站点 根据父级id判断
SysDept dept = user.getDept();
if (dept == null) {
2023-05-13 17:10:52 +08:00
log.info("用户id:{}, 获取不到所属部门信息", user.getUserId());
2023-03-09 11:42:51 +08:00
return null;
}
String deptId = String.valueOf(dept.getDeptId());
resultVO.setDeptId(deptId);
2023-05-13 17:13:24 +08:00
log.info("用户id:{}, 所属部门信息:{}", user.getUserId(), JSONObject.toJSONString(dept));
2023-03-09 11:42:51 +08:00
// 登录用户角色
List<SysRole> roles = user.getRoles();
Long parentId = dept.getParentId();
2023-07-06 15:48:06 +08:00
resultVO.setParentId(String.valueOf(parentId));
2023-03-09 11:42:51 +08:00
List<String> merchantDeptIds = Lists.newArrayList();
List<String> stationDeptIds = Lists.newArrayList();
if (parentId == 0L) {
// 父级id是0表明是该账号挂在平台下面是系统管理员
} else if (parentId == 100L) {
// 父级id是100表明是该账号挂在运营商下面是运营商管理员
merchantDeptIds.add(deptId);
2023-03-09 11:42:51 +08:00
resultVO.setMerchantDeptIds(merchantDeptIds);
} else {
// 其他情况,表明是站点管理员
stationDeptIds.add(deptId);
2023-03-09 11:42:51 +08:00
resultVO.setStationDeptIds(stationDeptIds);
}
return resultVO;
}
2023-03-04 16:29:55 +08:00
}