ERPClimb logoERPClimb
CAP
BTP & Integrationintermediate 4 min read

BTP CAP Part 3: Securing CAP Services with XSUAA Role-Based Authorization

Preview · a free account unlocks the full lesson

Key takeaway

Secure CAP declaratively: @requires and @restrict annotations for roles and instance-based conditions, generating xs-security. json with cds compile, mocked users for local testing, XSUAA in production, and testing authorization in hybrid mode.

In this lessonShow sections

Visual overview

CAP SECURITYHow CAP authorizes a requestDeclarative checks before any handler runs1Request with tokenXSUAA token (or mocked user locally)JWT?Service @requiresRole present?No → 403 ForbiddenYes2Entity @restrictEvent allowed for role@restrict3Instance conditionwhere: country = $user.countryFilterHandler runsOnly authorized dataHandlerTip: keep authorization in CDS annotations — visible, testable and generatedinto XSUAA.

Explanation

Part 2 built CAP services; this part covers securing them with XSUAA-based, role-based authorization. The @requires annotation restricts an entire service or entity to specific roles, such as 'authenticated-user' or 'Admin'. The @restrict annotation goes further, defining exactly which grants apply per event — READ, WRITE, a custom action — and can add instance-level conditions through a where clause.

Locally, mocked users defined in package.json simulate roles during development, while in production those same roles come from real XSUAA tokens. Running cds add xsuaa, or cds compile, generates the xs-security.json file directly from these annotations, so the authorization model and the security configuration stay in sync automatically.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.