在當今的數位環境中,有效的存取管理對於保護資源和資料至關重要。基於角色的存取控制 (RBAC) 系統提供了一種結構化方法來管理使用者權限和角色。本部落格概述了針對不同應用程式需求量身定制的 RBAC 系統的兩種變體:通用業務應用程式和企業業務應用程式。
為了說明這些概念,我們將提供管理存取控制的服務的演示程式碼片段,以及 RBAC 系統中使用的每個表的詳細說明。
對於大多數常見的業務應用程序,可以簡化 RBAC 系統以有效地管理角色和權限,而不會增加複雜性。關鍵組件是:
使用者表
角色表
模組表
模組_權限表
角色_權限表
使用者角色表
企業業務應用程式可能需要額外的元件來處理更複雜的存取控制需求。其中包括:
政策表
角色_策略表
使用者策略表
策略_條件表
上下文_權限表
Temporal_Constraint 表
代表團表
審核日誌表
這是 Java 中 AccessControlService 的範例實現,示範如何在動態 RBAC 系統中管理存取控制。此範例涵蓋了基本元件並說明如何處理權限和策略。
import java.time.LocalDateTime; import java.util.List; @Service @Transactional public class AccessControlService { @Autowired private UserRepository userRepository; @Autowired private RoleRepository roleRepository; @Autowired private ModulePermissionRepository modulePermissionRepository; @Autowired private RolePermissionRepository rolePermissionRepository; @Autowired private UserRoleRepository userRoleRepository; @Autowired private PolicyRepository policyRepository; @Autowired private UserPolicyRepository userPolicyRepository; @Autowired private RolePolicyRepository rolePolicyRepository; @Autowired private PolicyConditionRepository policyConditionRepository; @Autowired private ContextualPermissionRepository contextualPermissionRepository; @Autowired private TemporalConstraintRepository temporalConstraintRepository; @Autowired private DelegationRepository delegationRepository; public boolean hasAccess(String username, Long moduleId, String permissionType) { // Fetch user User user = userRepository.findByUsername(username); if (user == null) { return false; } // Check if user has any delegations boolean hasDelegatedAccess = checkDelegatedAccess(user.getUserId(), moduleId, permissionType); if (hasDelegatedAccess) { return true; } // Check if user has direct access via roles List<UserRole> userRoles = userRoleRepository.findByUserId(user.getUserId()); for (UserRole userRole : userRoles) { List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleId(userRole.getRoleId()); for (RolePermission rolePermission : rolePermissions) { ModulePermission modulePermission = modulePermissionRepository.findById(rolePermission.getModulePermissionId()).orElse(null); if (modulePermission != null && modulePermission.getModuleId().equals(moduleId) && modulePermission.getPermissionType().equals(permissionType)) { // Check if role has any associated policies if (hasPolicyAccess(user.getUserId(), moduleId, permissionType, modulePermission.getModuleId())) { return true; } } } } return false; } private boolean checkDelegatedAccess(Long userId, Long moduleId, String permissionType) { List<Delegation> delegations = delegationRepository.findByDelegateUserId(userId); LocalDateTime now = LocalDateTime.now(); for (Delegation delegation : delegations) { // Check if delegation is expired if (delegation.getExpirationDate() != null && delegation.getExpirationDate().isBefore(now)) { continue; } List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleId(delegation.getRoleId()); for (RolePermission rolePermission : rolePermissions) { ModulePermission modulePermission = modulePermissionRepository.findById(rolePermission.getModulePermissionId()).orElse(null); if (modulePermission != null && modulePermission.getModuleId().equals(moduleId) && modulePermission.getPermissionType().equals(permissionType)) { return true; } } } return false; } private boolean hasPolicyAccess(Long userId, Long moduleId, String permissionType, Long modulePermissionId) { // Check policies assigned directly to the user List<UserPolicy> userPolicies = userPolicyRepository.findByUserId(userId); for (UserPolicy userPolicy : userPolicies) { if (isPolicyValid(userPolicy.getPolicyId(), moduleId, permissionType)) { return true; } } // Check policies assigned to roles List<UserRole> userRoles = userRoleRepository.findByUserId(userId); for (UserRole userRole : userRoles) { List<RolePolicy> rolePolicies = rolePolicyRepository.findByRoleId(userRole.getRoleId()); for (RolePolicy rolePolicy : rolePolicies) { if (isPolicyValid(rolePolicy.getPolicyId(), moduleId, permissionType)) { return true; } } } return false; } private boolean isPolicyValid(Long policyId, Long moduleId, String permissionType) { // Check policy conditions List<PolicyCondition> conditions = policyConditionRepository.findByPolicyId(policyId); for (PolicyCondition condition : conditions) { // Add logic to evaluate conditions based on conditionType and conditionValue // e.g., Check if context or attribute matches the condition } // Check contextual permissions List<ContextualPermission> contextualPermissions = contextualPermissionRepository.findByPolicyId(policyId); for (ContextualPermission contextualPermission : contextualPermissions) { // Add logic to evaluate contextual permissions // e.g., Check if current context matches the contextualPermission } // Check temporal constraints List<TemporalConstraint> temporalConstraints = temporalConstraintRepository.findByPolicyId(policyId); for (TemporalConstraint temporalConstraint : temporalConstraints) { LocalDateTime now = LocalDateTime.now(); if (now.isBefore(temporalConstraint.getStartTime()) || now.isAfter(temporalConstraint.getEndTime())) { return false; } } return true; } }
透過區分普通業務應用和企業業務應用,您可以自訂您的RBAC系統
以上是為企業應用程式實施動態 RBAC 系統 - 簡化的詳細內容。更多資訊請關注PHP中文網其他相關文章!