Implementing Authorization Logic

1. Implementing Role-Based Access Control (RBAC)

ConceptDetail
UserHas roles
RoleHas permissions
PermissionAction on resource type
DecisionHas user role with permission?

Example: Spring Security check

@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(UUID id) { ... }

2. Implementing Attribute-Based Access Control (ABAC)

ElementDetail
Subject attrsDepartment, clearance
Resource attrsOwner, classification
Action attrsRead, write, approve
Environment attrsTime, IP, MFA status
Policy engineOPA, Cedar, XACML

3. Designing Permission Checking Logic

Example: Permission service

public boolean canEdit(UserId actor, OrderId order) {
    var o = orderRepo.findById(order).orElseThrow();
    return o.ownerId().equals(actor) || rbac.hasRole(actor, "ADMIN");
}

4. Implementing Resource-Level Authorization

PatternDetail
Ownership checkResource has owner field
Tenant scopingFilter by tenant ID always
ACLPer-resource grants
Spring@PostAuthorize("returnObject.ownerId == authentication.name")

5. Using Authorization Annotations

AnnotationUse
@PreAuthorizeBefore method, full SpEL
@PostAuthorizeAfter method, inspect result
@PreFilter/@PostFilterFilter collection elements
@SecuredSimple role check
@RolesAllowedJSR-250 standard

6. Implementing Policy-Based Authorization

Example: OPA Rego policy

package orders.allow
default allow = false
allow {
    input.action == "read"
    input.user.tenant == input.resource.tenant
}
allow {
    input.action == "delete"
    "admin" in input.user.roles
}

7. Implementing Hierarchical Roles

Example: Role hierarchy (Spring)

@Bean
RoleHierarchy roleHierarchy() {
    var h = new RoleHierarchyImpl();
    h.setHierarchy("""
        ROLE_SUPERADMIN > ROLE_ADMIN
        ROLE_ADMIN > ROLE_USER
        """);
    return h;
}

8. Handling Dynamic Permissions

SourceDetail
DatabaseMutable; cache with TTL
Feature flagPer-user/tenant overrides
Admin UIGrant/revoke at runtime

9. Implementing Method-Level Security

StepDetail
Enable@EnableMethodSecurity
ApplyService-layer methods
Why serviceCatch all entry points (web, MQ, jobs)

10. Implementing Claim-Based Authorization

Example: JWT claim check

@PreAuthorize("authentication.principal.claims['scope'].contains('orders:write')")
public Order place(...) { ... }

11. Implementing Permission Caching

ConcernStrategy
Hot readsCaffeine local cache
TTL1-5 min for permissions
InvalidationOn role change event
Negative cacheCache "denied" briefly

12. Implementing Authorization Audit Trail

FieldDetail
timestampUTC
actorUser ID + tenant
actionVerb + resource
decisionALLOW / DENY
reasonPolicy/rule that fired
request contextIP, correlation ID