Role-Based Access Control (RBAC) and Authorization

Interview Preparation Hub for Backend and Cloud-Native Engineering Roles

1. Introduction

Role-Based Access Control (RBAC) is a widely adopted model for managing authorization in enterprise applications. It simplifies access management by assigning permissions to roles rather than individual users. Users are then assigned roles, inheriting the associated permissions.

This guide covers everything from fundamentals to advanced topics: RBAC architecture, implementation in Spring Security, hierarchical roles, attribute-based access control (ABAC) vs RBAC, microservices integration, best practices, common mistakes, and interview notes. By the end, you will have mastered RBAC and authorization.

2. Fundamentals of RBAC

RBAC is based on three core concepts:

  • Users: Individuals who need access.
  • Roles: Collections of permissions representing job functions.
  • Permissions: Access rights to resources or actions.
Diagram: RBAC Model

User → Assigned Role → Role → Permissions → Resource Access

3. RBAC Architecture

RBAC architecture typically includes:

  • Role Hierarchies: Roles can inherit permissions from other roles.
  • Constraints: Separation of duties, mutually exclusive roles.
  • Sessions: Users activate a subset of roles during a session.

Example: An "Admin" role may inherit permissions from "Manager" and "User" roles.

4. Implementation in Spring Security

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .antMatchers("/manager/**").hasAnyRole("MANAGER", "ADMIN")
        .antMatchers("/user/**").hasRole("USER")
        .anyRequest().authenticated()
      .and()
      .formLogin();
  }
}
    

Roles are typically stored in a database and loaded via UserDetailsService.

5. Advanced RBAC Concepts

  • Hierarchical Roles: Roles inherit permissions.
  • Dynamic Role Assignment: Roles assigned based on attributes.
  • Contextual Authorization: Permissions depend on context (time, location).

Example: A "Doctor" role may only access patient records during working hours.

6. RBAC vs ABAC

Attribute-Based Access Control (ABAC) uses attributes (user, resource, environment) to determine access. RBAC is simpler but less flexible. Many systems combine RBAC and ABAC.

RBACABAC
Role-basedAttribute-based
Simpler to implementMore flexible
Good for enterprisesGood for dynamic environments

7. RBAC in Microservices

In microservices, RBAC can be centralized using an identity provider (Keycloak, Okta) or decentralized with each service managing roles. JWT tokens often carry role information.

@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/secure-data")
public String getSecureData() {
  return "Sensitive information";
}
    

8. Best Practices

  • Define roles based on job functions, not individuals.
  • Use least privilege principle.
  • Implement role hierarchies for scalability.
  • Regularly review and update roles.
  • Combine RBAC with ABAC for flexibility.
  • Log and monitor authorization decisions.

9. Common Mistakes

  • Creating too many roles, leading to complexity.
  • Assigning roles directly to users without abstraction.
  • Ignoring separation of duties.
  • Not auditing role assignments.
  • Hardcoding roles in code instead of externalizing.

10. Interview Notes

  • Be ready to explain RBAC fundamentals.
  • Discuss role hierarchies and constraints.
  • Explain implementation in Spring Security.
  • Compare RBAC and ABAC.
  • Know best practices and common mistakes.
Diagram: Interview Prep Map

Fundamentals → Architecture → Implementation → Advanced RBAC → RBAC vs ABAC → Microservices → Best Practices → Pitfalls

11. Final Mastery Summary

Mastery of RBAC means understanding not only the fundamentals of roles, permissions, and users, but also how to implement them effectively in frameworks like Spring Security. It requires balancing simplicity with flexibility, ensuring that roles reflect real organizational responsibilities while avoiding unnecessary complexity.

In modern enterprise systems, RBAC is often combined with Attribute-Based Access Control (ABAC) to handle dynamic scenarios. For example, RBAC may grant a "Manager" role access to reports, while ABAC adds conditions such as "only during business hours" or "only for their department." This hybrid approach ensures both scalability and fine-grained control.

In microservices architectures, RBAC is typically enforced through centralized identity providers (like Keycloak, Okta, or Azure AD) and propagated via tokens (JWTs) containing role claims. Each service validates the token and applies role-based rules locally, ensuring consistent authorization across distributed systems.

Best practices include defining roles based on job functions, applying the principle of least privilege, regularly auditing role assignments, and externalizing role definitions for easier updates. Common mistakes, such as creating too many roles or hardcoding them in code, can lead to maintenance headaches and security gaps.

For interviews, emphasize your ability to explain RBAC fundamentals, role hierarchies, implementation in Spring Security, comparisons with ABAC, and integration into microservices. Demonstrating awareness of best practices and pitfalls shows that you can design secure, scalable authorization systems.

Diagram: Mastery Roadmap

Fundamentals → Architecture → Implementation → Advanced RBAC → RBAC vs ABAC → Microservices → Best Practices → Interview Prep → Mastery