大致权限控制完成,新增优化权限控制代码
parent
d61a2422b6
commit
99128aa40c
@ -0,0 +1,51 @@
|
||||
package cn.zyp.stusystem.common;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.NotPermissionException;
|
||||
import cn.dev33.satoken.exception.NotRoleException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.naming.AuthenticationException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
// 处理SaToken未登录异常
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
public Result<Void> handleNotLoginException(NotLoginException e) {
|
||||
return Result.error(401, "未登录或登录已过期");
|
||||
}
|
||||
|
||||
// 处理SaToken权限不足异常
|
||||
@ExceptionHandler(NotPermissionException.class)
|
||||
public Result<Void> handleNotPermissionException(NotPermissionException e) {
|
||||
return Result.error(403, "无权限访问");
|
||||
}
|
||||
|
||||
// 处理SaToken角色不足异常
|
||||
@ExceptionHandler(NotRoleException.class)
|
||||
public Result<Void> handleNotRoleException(NotRoleException e) {
|
||||
return Result.error(403, "无角色权限");
|
||||
}
|
||||
|
||||
// 处理其他认证异常
|
||||
@ExceptionHandler(AuthenticationException.class)
|
||||
public Result<Void> handleException(AuthenticationException e){
|
||||
return Result.error(401, e.getMessage());
|
||||
}
|
||||
|
||||
// 处理其他权限不足异常
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public Result<Void> handleAccessDeniedException(AccessDeniedException e) {
|
||||
return Result.error(403, e.getMessage());
|
||||
}
|
||||
|
||||
// 处理其他未知异常
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Result<Void> handleException(Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error(500, "服务器内部错误");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package cn.zyp.stusystem.common;
|
||||
|
||||
|
||||
public enum ResultCode {
|
||||
SUCCESS(200,"成功"),
|
||||
ERROR(500,"错误"),
|
||||
PARAM_ERROR(400,"参数错误");
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
ResultCode(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package cn.zyp.stusystem.config;
|
||||
|
||||
import cn.dev33.satoken.filter.SaServletFilter;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SaTokenConfig {
|
||||
|
||||
@Bean
|
||||
public SaServletFilter getSaServletFilter() {
|
||||
return new SaServletFilter()
|
||||
.addInclude("/**")
|
||||
.addExclude("/favicon.ico")
|
||||
.setAuth(obj -> {
|
||||
// 这里不需要写权限校验,让注解来处理
|
||||
// SaRouter.match("/**").notMatch("/api/login").check(StpUtil::checkLogin);
|
||||
})
|
||||
.setError(e -> {
|
||||
return SaResult.error(e.getMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package cn.zyp.stusystem.config;
|
||||
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class StpInterfaceImpl implements StpInterface {
|
||||
|
||||
@Override
|
||||
public List<String> getPermissionList(Object loginId, String loginType) {
|
||||
// 从Session获取权限
|
||||
return (List<String>) StpUtil.getSessionByLoginId(loginId).get("permissions");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRoleList(Object loginId, String loginType) {
|
||||
String role = (String) StpUtil.getSessionByLoginId(loginId).get("role");
|
||||
return Arrays.asList(role);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package cn.zyp.stusystem.constant;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// constant/PermissionConstants.java
|
||||
public class PermissionConstants {
|
||||
public static final String ROLE_ADMIN = "admin";
|
||||
public static final String ROLE_HEAD_TEACHER = "head_teacher";
|
||||
public static final String ROLE_TEACHER = "teacher";
|
||||
|
||||
public static final String CLASS_VIEW = "class:view";
|
||||
public static final String CLASS_ADD = "class:add";
|
||||
public static final String CLASS_EDIT = "class:edit";
|
||||
public static final String CLASS_DELETE = "class:delete";
|
||||
|
||||
public static final String STUDENT_VIEW = "student:view";
|
||||
public static final String STUDENT_ADD = "student:add";
|
||||
public static final String STUDENT_EDIT = "student:edit";
|
||||
public static final String STUDENT_DELETE = "student:delete";
|
||||
|
||||
public static final Map<String, List<String>> ROLE_PERMISSIONS = new HashMap<>();
|
||||
|
||||
static {
|
||||
ROLE_PERMISSIONS.put(ROLE_ADMIN, Arrays.asList(
|
||||
CLASS_VIEW, CLASS_ADD, CLASS_EDIT, CLASS_DELETE,
|
||||
STUDENT_VIEW, STUDENT_ADD, STUDENT_EDIT, STUDENT_DELETE
|
||||
));
|
||||
|
||||
ROLE_PERMISSIONS.put(ROLE_HEAD_TEACHER, Arrays.asList(
|
||||
CLASS_VIEW,
|
||||
STUDENT_VIEW, STUDENT_ADD, STUDENT_EDIT, STUDENT_DELETE
|
||||
));
|
||||
|
||||
ROLE_PERMISSIONS.put(ROLE_TEACHER, Arrays.asList(
|
||||
CLASS_VIEW,
|
||||
STUDENT_VIEW
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package cn.zyp.stusystem.controller;
|
||||
|
||||
import cn.zyp.stusystem.common.Result;
|
||||
import cn.zyp.stusystem.entity.Permission;
|
||||
import cn.zyp.stusystem.service.PermissionService;
|
||||
import cn.zyp.stusystem.vo.PermissionVO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/permission")
|
||||
public class PermissionController {
|
||||
|
||||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
/**
|
||||
* 获取所有权限列表
|
||||
*/
|
||||
@GetMapping("/all")
|
||||
public Result<List<PermissionVO>> getAllPermissions() {
|
||||
List<Permission> permissions = permissionService.getAllEnabledPermissions();
|
||||
List<PermissionVO> voList = permissions.stream()
|
||||
.map(permission -> {
|
||||
PermissionVO vo = new PermissionVO();
|
||||
BeanUtils.copyProperties(permission, vo);
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(voList);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package cn.zyp.stusystem.controller;
|
||||
|
||||
import cn.zyp.stusystem.common.Result;
|
||||
import cn.zyp.stusystem.dto.SaveRolePermissionsDTO;
|
||||
import cn.zyp.stusystem.service.RolePermissionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/role")
|
||||
public class RolePermissionController {
|
||||
|
||||
@Autowired
|
||||
private RolePermissionService rolePermissionService;
|
||||
|
||||
/**
|
||||
* 获取角色的权限列表
|
||||
*/
|
||||
@GetMapping("/{roleId}/permissions")
|
||||
public Result<List<String>> getRolePermissions(@PathVariable Long roleId) {
|
||||
List<String> permissionCodes = rolePermissionService.getPermissionCodesByRoleId(roleId);
|
||||
return Result.success(permissionCodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存角色的权限配置
|
||||
*/
|
||||
@PostMapping("/{roleId}/permissions")
|
||||
public Result<Boolean> saveRolePermissions(
|
||||
@PathVariable Long roleId,
|
||||
@RequestBody SaveRolePermissionsDTO dto) {
|
||||
rolePermissionService.saveRolePermissions(roleId, dto.getPermissions());
|
||||
return Result.success(true);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cn.zyp.stusystem.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginDTO {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cn.zyp.stusystem.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SaveRolePermissionsDTO {
|
||||
private List<String> permissions; // 权限码列表
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package cn.zyp.stusystem.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("role_permissions")
|
||||
public class RolePermission {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private Long roleId; // 角色ID
|
||||
private String permissionCode; // 权限码
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cn.zyp.stusystem.mapper;
|
||||
|
||||
import cn.zyp.stusystem.entity.Permission;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface PermissionMapper extends BaseMapper<Permission> {
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cn.zyp.stusystem.mapper;
|
||||
|
||||
import cn.zyp.stusystem.entity.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package cn.zyp.stusystem.service;
|
||||
|
||||
import cn.zyp.stusystem.entity.Permission;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PermissionService extends IService<Permission> {
|
||||
/**
|
||||
* 获取所有启用的权限
|
||||
*/
|
||||
List<Permission> getAllEnabledPermissions();
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cn.zyp.stusystem.service;
|
||||
|
||||
import cn.zyp.stusystem.entity.Role;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
public interface RoleService extends IService<Role> {
|
||||
/**
|
||||
* 根据角色代码获取角色ID
|
||||
*/
|
||||
Long getRoleIdByRoleCode(String roleCode);
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package cn.zyp.stusystem.service.impl;
|
||||
|
||||
import cn.zyp.stusystem.entity.Permission;
|
||||
import cn.zyp.stusystem.mapper.PermissionMapper;
|
||||
import cn.zyp.stusystem.service.PermissionService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
|
||||
implements PermissionService {
|
||||
|
||||
@Override
|
||||
public List<Permission> getAllEnabledPermissions() {
|
||||
return list(new LambdaQueryWrapper<Permission>()
|
||||
.eq(Permission::getStatus, 1)
|
||||
.orderByAsc(Permission::getModule, Permission::getSortOrder)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.zyp.stusystem.service.impl;
|
||||
|
||||
import cn.zyp.stusystem.entity.Role;
|
||||
import cn.zyp.stusystem.mapper.RoleMapper;
|
||||
import cn.zyp.stusystem.service.RoleService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
|
||||
|
||||
@Override
|
||||
public Long getRoleIdByRoleCode(String roleCode) {
|
||||
Role role = getOne(new LambdaQueryWrapper<Role>()
|
||||
.eq(Role::getRoleCode, roleCode)
|
||||
.eq(Role::getStatus, 1)
|
||||
);
|
||||
return role != null ? role.getId() : null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package cn.zyp.stusystem.service.impl;
|
||||
|
||||
import cn.zyp.stusystem.entity.User;
|
||||
import cn.zyp.stusystem.mapper.UserMapper;
|
||||
import cn.zyp.stusystem.service.UserService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||
@Override
|
||||
public User getByUsername(String username) {
|
||||
return lambdaQuery().eq(User::getUsername,username).one();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package cn.zyp.stusystem.vo;
|
||||
|
||||
import cn.zyp.stusystem.vo.UserVO;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginResult {
|
||||
private String token;
|
||||
private UserVO userInfo;
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cn.zyp.stusystem.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PermissionVO {
|
||||
private String code; // 权限码
|
||||
private String name; // 权限名称
|
||||
private String module; // 所属模块
|
||||
private String moduleName; // 模块名称
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package cn.zyp.stusystem.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserVO {
|
||||
private Long id;
|
||||
private String username;
|
||||
private String role;
|
||||
private String name;
|
||||
private List<String> permissions;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue