Browse Source

自定义注解

wangyetao/feature-20250715095107-地区数据权限
wangyetao 1 month ago
parent
commit
272d88657b
  1. 55
      src/main/java/com/example/demo/Util/SecurityUtils.java
  2. 16
      src/main/java/com/example/demo/annotation/DataScope.java

55
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<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();
}
}

16
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 "";
}
Loading…
Cancel
Save