|
@ -0,0 +1,55 @@ |
|
|
|
|
|
package com.example.demo.Util; |
|
|
|
|
|
|
|
|
|
|
|
import com.example.demo.domain.entity.Admin; |
|
|
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 安全服务工具类 |
|
|
|
|
|
*/ |
|
|
|
|
|
public class SecurityUtils { |
|
|
|
|
|
|
|
|
|
|
|
//用户ID |
|
|
|
|
|
public static Integer getUserId() { |
|
|
|
|
|
try { |
|
|
|
|
|
return getLoginUser().getId(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException("获取用户ID异常", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取用户地区信息 |
|
|
|
|
|
public static List<String> getMarketList() { |
|
|
|
|
|
try { |
|
|
|
|
|
return Arrays.stream(getLoginUser().getMarket().split(",")).toList(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException("获取部门ID异常", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取用户账户 |
|
|
|
|
|
public static String getUsername() { |
|
|
|
|
|
try { |
|
|
|
|
|
return getLoginUser().getUsername(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException("获取用户账户异常", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取当前登录用户 |
|
|
|
|
|
public static Admin getLoginUser() { |
|
|
|
|
|
try { |
|
|
|
|
|
return (Admin) getAuthentication().getPrincipal(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException("获取用户信息异常", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取Authentication |
|
|
|
|
|
public static Authentication getAuthentication() { |
|
|
|
|
|
return SecurityContextHolder.getContext().getAuthentication(); |
|
|
|
|
|
} |
|
|
|
|
|
} |