Browse Source

实现 StpInterface

Gaokun Wang 1 month ago
parent
commit
8987c94ef0

+ 46 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/enums/DeviceType.java

@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.enums;
+
+
+/**
+ * @description DeviceType
+ *
+ * @author GaoKunW
+ * @date 2025/7/2 16:35
+ */
+public enum DeviceType {
+
+    /**
+     * pc端
+     */
+    PC("pc"),
+
+    /**
+     * app端
+     */
+    APP("app"),
+
+    /**
+     * 小程序端
+     */
+    XCX("xcx"),
+
+    /**
+     * social第三方端
+     */
+    SOCIAL("social");
+
+    private final String device;
+
+    DeviceType(String device) {
+        this.device = device;
+    }
+
+    public String getDevice() {
+        return device;
+    }
+}

+ 47 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/enums/UserType.java

@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.enums;
+
+
+import org.eco.vip.orm.utils.StrUtils;
+
+/**
+ * @description UserType
+ *
+ * @author GaoKunW
+ * @date 2025/7/2 16:42
+ */
+public enum UserType {
+
+    /**
+     * pc端
+     */
+    PC_USER("pc_user"),
+
+    /**
+     * app端
+     */
+    APP_USER("app_user");
+
+    private final String userType;
+
+    UserType(String userType) {
+        this.userType = userType;
+    }
+
+    public String getUserType() {
+        return userType;
+    }
+
+    public static UserType getUserType(String str) {
+        for (UserType value : values()) {
+            if (StrUtils.contains(str, value.getUserType())) {
+                return value;
+            }
+        }
+        throw new RuntimeException("'UserType' not found By " + str);
+    }
+}

+ 50 - 0
eco-common/com-security/src/main/java/org/eco/vip/security/core/SaPermissionImpl.java

@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.security.core;
+
+
+import cn.dev33.satoken.stp.StpInterface;
+import org.eco.vip.orm.enums.UserType;
+import org.eco.vip.orm.pojo.LoginUser;
+import org.eco.vip.orm.utils.ObjUtils;
+import org.eco.vip.security.utils.LoginHelper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @description SaPermissionImpl
+ *
+ * @author GaoKunW
+ * @date 2025/7/2 16:30
+ */
+public class SaPermissionImpl implements StpInterface {
+    @Override
+    public List<String> getPermissionList(Object o, String loginType) {
+        LoginUser loginUser = LoginHelper.getLoginUser();
+        UserType userType = null;
+        if (ObjUtils.isNotNull(loginUser)) {
+            userType = UserType.getUserType(loginUser.getUserType());
+        }
+        if (userType == UserType.PC_USER) {
+            return new ArrayList<>(loginUser.getMenuPermission());
+        }  // 其他
+        return new ArrayList<>();
+    }
+
+    @Override
+    public List<String> getRoleList(Object o, String loginType) {
+        LoginUser loginUser = LoginHelper.getLoginUser();
+        UserType userType = null;
+        if (ObjUtils.isNotNull(loginUser)) {
+            userType = UserType.getUserType(loginUser.getUserType());
+        }
+        if (userType == UserType.PC_USER) {
+            return new ArrayList<>(loginUser.getRolePermission());
+        }  // 其他
+        return new ArrayList<>();
+    }
+}

+ 12 - 0
eco-common/com-security/src/main/java/org/eco/vip/security/utils/LoginHelper.java

@@ -8,6 +8,7 @@ package org.eco.vip.security.utils;
 
 import cn.dev33.satoken.context.SaHolder;
 import cn.dev33.satoken.context.model.SaStorage;
+import cn.dev33.satoken.session.SaSession;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.dev33.satoken.stp.parameter.SaLoginParameter;
 import cn.hutool.core.util.ObjectUtil;
@@ -57,4 +58,15 @@ public class LoginHelper {
         );
         StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
     }
+
+    /**
+     * 获取用户(多级缓存)
+     */
+    public static LoginUser getLoginUser() {
+        SaSession session = StpUtil.getTokenSession();
+        if (ObjectUtil.isNull(session)) {
+            return null;
+        }
+        return (LoginUser) session.get(LOGIN_USER_KEY);
+    }
 }