权限审计: 所有权限的授予和使用都应该被记录和审计,以确保系统的安全性和合规性。
组织表(Organizations)
字段名 | 数据类型 | 描述 |
---|---|---|
organization_id | INT | 主键,自增 |
name | VARCHAR | 组织名称 |
parent_id | INT | 父组织ID,用于层级关系 |
description | TEXT | 组织描述 |
用户表(Users)
字段名 | 数据类型 | 描述 |
---|---|---|
user_id | INT | 主键,自增 |
username | VARCHAR | 用户名 |
password_hash | VARCHAR | 加密密码 |
organization_id | INT | 所属组织ID |
VARCHAR | 电子邮件 | |
created_at | DATETIME | 创建时间 |
updated_at | DATETIME | 更新时间 |
角色表(Roles)
字段名 | 数据类型 | 描述 |
---|---|---|
role_id | INT | 主键,自增 |
name | VARCHAR | 角色名称 |
description | TEXT | 角色描述 |
权限表(Permissions)
字段名 | 数据类型 | 描述 |
---|---|---|
permission_id | INT | 主键,自增 |
name | VARCHAR | 权限名称 |
description | TEXT | 权限描述 |
角色权限关联表(Role_Permissions)
字段名 | 数据类型 | 描述 |
---|---|---|
role_id | INT | 角色ID,外键 |
permission_id | INT | 权限ID,外键 |
用户角色关联表(User_Roles)
字段名 | 数据类型 | 描述 |
---|---|---|
user_id | INT | 用户ID,外键 |
role_id | INT | 角色ID,外键 |
组织权限关联表(Organization_Permissions)
字段名 | 数据类型 | 描述 |
---|---|---|
organization_id | INT | 组织ID,外键 |
permission_id | INT | 权限ID,外键 |
limit | INT | 允许的操作数量限制 |
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface PermissionRequired { String[] value(); }定义角色注解(@RoleRequired)
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface RoleRequired { String[] value(); }定义数量限制注解(@LimitRequired)
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface LimitRequired { String value(); }使用示例
@RestController @RequestMapping("/api") public class ApiController { @PermissionRequired("READ_DATA") @GetMapping("/data") public ResponseEntity<?> getData() { // 方法逻辑 } @RoleRequired({"ADMIN", "MANAGER"}) @PostMapping("/config") public ResponseEntity<?> updateConfig() { // 堆代码 duidaima.com // 方法逻辑 } @LimitRequired("REQUEST_LIMIT") @GetMapping("/request") public ResponseEntity<?> makeRequest() { // 方法逻辑 } }在这个示例中,getData 方法需要用户拥有 READ_DATA 权限,updateConfig 方法需要用户拥有 ADMIN 或 MANAGER 角色,而 makeRequest 方法需要检查 REQUEST_LIMIT 类型的数量限制。
代理(Proxy): 代理是AOP框架创建的,它包裹了目标对象,并在执行连接点之前或之后执行通知。
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class PermissionAspect { @Pointcut("@annotation(permissionRequired)") public void permissionPointcut(PermissionRequired permissionRequired) {} @Before("permissionPointcut(permissionRequired)") public void checkPermission(JoinPoint joinPoint, PermissionRequired permissionRequired) { // 获取当前用户 User currentUser = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); // 检查用户是否拥有所需的权限 for (String permission : permissionRequired.value()) { if (!permissionService.hasPermission(currentUser, permission)) { throw new AccessDeniedException("Access Denied: No permission to perform this operation."); } } } // 其他方法,如角色检查、数量限制检查等 }组织权限检查
public boolean hasOrganizationPermission(User currentUser, String permission) { // 查询数据库,检查用户所属组织是否有权限 }个人权限检查
public boolean hasUserPermission(User currentUser, String permission) { // 查询数据库,检查用户是否有权限 }数量限制检查
public boolean isLimitExceeded(User currentUser, String limitType) { // 查询数据库,检查是否达到数量限制 }特殊角色检查
public boolean hasSpecialRole(User currentUser) { // 查询数据库,检查用户是否拥有特殊角色 }综合检查逻辑
@Service public class UserService { @Autowired private UserRepository userRepository; public User getUserById(Long userId) { return userRepository.findById(userId).orElse(null); } public Set<String> getUserPermissions(Long userId) { User user = getUserById(userId); if (user != null) { Set<Role> roles = user.getRoles(); Set<String> permissions = new HashSet<>(); for (Role role : roles) { permissions.addAll(role.getPermissions()); } return permissions; } return Collections.emptySet(); } // 其他用户相关业务逻辑 }角色权限服务(RoleService)
@Service public class RoleService { @Autowired private RoleRepository roleRepository; public Role getRoleById(Long roleId) { return roleRepository.findById(roleId).orElse(null); } public Set<String> getRolePermissions(Long roleId) { Role role = getRoleById(roleId); if (role != null) { return role.getPermissions(); } return Collections.emptySet(); } // 堆代码 duidaima.com // 其他角色相关业务逻辑 }权限检查服务(PermissionService)
@Service public class PermissionService { @Autowired private UserService userService; @Autowired private RoleService roleService; public boolean hasPermission(User user, String permission) { if (user == null) { return false; } Set<String> userPermissions = userService.getUserPermissions(user.getId()); return userPermissions.contains(permission); } public boolean hasRole(User user, String role) { if (user == null) { return false; } Set<Role> userRoles = user.getRoles(); for (Role userRole : userRoles) { if (userRole.getName().equals(role)) { return true; } } return false; } public boolean isLimitExceeded(User user, String limitType) { // 实现检查用户或组织是否超出了操作数量限制 // 这可能涉及查询数据库中的计数器或使用缓存 return false; } // 特殊角色检查 public boolean hasSpecialRole(User user) { // 实现检查用户是否拥有特殊角色 return hasRole(user, "ADMIN"); // 示例:假设"ADMIN"是特殊角色 } }业务逻辑实现说明
安全性: 通过细致的权限控制,可以确保系统的安全性,防止未授权访问。
安全性依赖: 系统的安全性高度依赖于权限控制的实现,任何实现上的漏洞都可能导致安全风险。