首次提交后端接口
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package com.nanxiislet.admin.config;
|
||||
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.nanxiislet.admin.entity.SysMenu;
|
||||
import com.nanxiislet.admin.entity.SysUser;
|
||||
import com.nanxiislet.admin.service.AuthService;
|
||||
import com.nanxiislet.admin.service.SysMenuService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sa-Token 权限认证接口实现
|
||||
* 用于获取当前用户的角色和权限列表
|
||||
*
|
||||
* @author NanxiIslet
|
||||
* @since 2024-01-08
|
||||
*/
|
||||
@Component
|
||||
public class StpInterfaceImpl implements StpInterface {
|
||||
|
||||
@Resource
|
||||
private AuthService authService;
|
||||
|
||||
@Resource
|
||||
private SysMenuService menuService;
|
||||
|
||||
/**
|
||||
* 获取用户权限列表
|
||||
*/
|
||||
@Override
|
||||
public List<String> getPermissionList(Object loginId, String loginType) {
|
||||
// 获取用户信息
|
||||
Long userId = Long.parseLong(loginId.toString());
|
||||
SysUser user = authService.getById(userId);
|
||||
if (user == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
String roleCode = user.getRole();
|
||||
|
||||
// 超级管理员拥有所有权限
|
||||
if ("super_admin".equals(roleCode)) {
|
||||
return List.of("*");
|
||||
}
|
||||
|
||||
// 根据角色获取菜单权限
|
||||
List<SysMenu> menus = menuService.getMenusByRoleCode(roleCode);
|
||||
if (menus == null || menus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return menus.stream()
|
||||
.map(SysMenu::getCode)
|
||||
.distinct()
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<String> getRoleList(Object loginId, String loginType) {
|
||||
Long userId = Long.parseLong(loginId.toString());
|
||||
SysUser user = authService.getById(userId);
|
||||
if (user == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
List<String> roles = new ArrayList<>();
|
||||
String roleCode = user.getRole();
|
||||
|
||||
if (roleCode != null && !roleCode.isEmpty()) {
|
||||
roles.add(roleCode);
|
||||
|
||||
// 超级管理员同时拥有 admin 角色
|
||||
if ("super_admin".equals(roleCode)) {
|
||||
roles.add("admin");
|
||||
}
|
||||
}
|
||||
|
||||
return roles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user