package com.jcdm.common.utils; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.util.PatternMatchUtils; import com.jcdm.common.constant.Constants; import com.jcdm.common.constant.HttpStatus; import com.jcdm.common.core.domain.entity.SysRole; import com.jcdm.common.core.domain.model.LoginUser; import com.jcdm.common.exception.ServiceException; /** * 安全æœåŠ¡å·¥å…·ç±» * * @author jc */ 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; } /** * 验è¯ç”¨æˆ·æ˜¯å¦å…·å¤‡æŸæƒé™ * * @param permission æƒé™å—符串 * @return 用户是å¦å…·å¤‡æŸæƒé™ */ public static boolean hasPermi(String permission) { return hasPermi(getLoginUser().getPermissions(), permission); } /** * 判æ–是å¦åŒ…å«æƒé™ * * @param authorities æƒé™åˆ—表 * @param permission æƒé™å—符串 * @return 用户是å¦å…·å¤‡æŸæƒé™ */ public static boolean hasPermi(Collection<String> authorities, String permission) { return authorities.stream().filter(StringUtils::hasText) .anyMatch(x -> Constants.ALL_PERMISSION.equals(x) || PatternMatchUtils.simpleMatch(x, permission)); } /** * 验è¯ç”¨æˆ·æ˜¯å¦æ‹¥æœ‰æŸä¸ªè§’色 * * @param role è§’è‰²æ ‡è¯† * @return 用户是å¦å…·å¤‡æŸè§’色 */ public static boolean hasRole(String role) { List<SysRole> roleList = getLoginUser().getUser().getRoles(); Collection<String> roles = roleList.stream().map(SysRole::getRoleKey).collect(Collectors.toSet()); return hasRole(roles, role); } /** * 判æ–是å¦åŒ…å«è§’色 * * @param roles 角色列表 * @param role 角色 * @return 用户是å¦å…·å¤‡æŸè§’色æƒé™ */ public static boolean hasRole(Collection<String> roles, String role) { return roles.stream().filter(StringUtils::hasText) .anyMatch(x -> Constants.SUPER_ADMIN.equals(x) || PatternMatchUtils.simpleMatch(x, role)); } }