diff --git a/src/main/java/com/example/demo/Util/SecurityUtils.java b/src/main/java/com/example/demo/Util/SecurityUtils.java new file mode 100644 index 0000000..bb69883 --- /dev/null +++ b/src/main/java/com/example/demo/Util/SecurityUtils.java @@ -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 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(); + } +} diff --git a/src/main/java/com/example/demo/annotation/DataScope.java b/src/main/java/com/example/demo/annotation/DataScope.java new file mode 100644 index 0000000..7ac2f58 --- /dev/null +++ b/src/main/java/com/example/demo/annotation/DataScope.java @@ -0,0 +1,16 @@ +package com.example.demo.annotation; + +import java.lang.annotation.*; + +/** + * 数据权限过滤注解 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface DataScope { + /** + * 表别名 + */ + String tableAlias() default ""; +}