Skip to main content

How to: authorize REST API access with roles

jEAP supports two role models for authorizing access to a REST API: simple roles and semantic roles. Pick the model your application uses, then follow the matching steps below.

Steps: authorize with simple roles​

  1. Complete the basic integration described in Authentication and authorization for REST APIs – Integration. Leave jeap.security.oauth2.resourceserver.system-name unset — setting it activates authorization with semantic roles instead.

  2. Check a role declaratively in the SpEL expressions of the @PreAuthorize and @PostAuthorize annotations:

    @PreAuthorize("hasRole('role')")
    public Data getData(...) {
    ...

    @PreAuthorize("hasRoleForPartner('role', #partnerId)")
    public Partner getPartner(String partnerId) {
    ...

    Note: Unfortunately, IntelliJ cannot recognize these additional methods. Auto-completion etc. is therefore not available inside the SpEL string (see the bug report in IntelliJ).

  3. Or, when the check cannot be expressed declaratively — for example because the data has to be loaded first — check a role programmatically through the ServletSimpleAuthorization (WebMvc) Spring bean:

    import ch.admin.bit.jeap.security.resource.authentication.ServletSimpleAuthorization;
    ...

    private ServletSimpleAuthorization jeapAuthorization;
    ...

    public Partner findPartner(...) {
    Partner partner = ...
    //Throw an exception if the user is not allowed to access the partner
    if (!jeapAuthorization.hasRoleForPartner("role", partner.getPartnerId())) {
    throw new AccessDeniedException("Missing role for partner with id '" + partner.getPartnerId());
    }
    return partner;
    }

For the full set of available query methods and a complete worked example, see Authorization with simple roles.

Steps: authorize with semantic roles​

  1. Complete the basic integration described in Authentication and authorization for REST APIs – Integration, and set jeap.security.oauth2.resourceserver.system-name — it is required for authorization with semantic roles.

  2. Check roles declaratively in the SpEL expressions of the @PreAuthorize and @PostAuthorize annotations (MVC and WebFlux):

    @PreAuthorize("hasRole('resource', 'operation')")
    public Data getData(...) {
    ...

    @PreAuthorize("hasRoleForPartner('resource', 'operation', #partnerId)")
    public Partner getPartner(String partnerId) {
    ...

    Note: Unfortunately, IntelliJ cannot recognize these additional methods. Auto-completion etc. is therefore not available inside the SpEL string (see the bug report in IntelliJ).

  3. Or, when the check cannot be expressed declaratively — for example because the data has to be loaded first — check roles programmatically through the ServletSemanticAuthorization Spring bean:

    import ch.admin.bit.jeap.security.resource.semanticAuthentication.ServletSemanticAuthorization;
    ...

    private ServletSemanticAuthorization jeapAuthorization;
    ...

    public Partner findPartner(...) {
    Partner partner = ...
    //Throw an exception if the user is not allowed to access the partner
    if (!jeapAuthorization.hasRoleForPartner("resource", "operation", partner.getPartnerId())) {
    throw new AccessDeniedException("Missing role for partner with id '" + partner.getPartnerId());
    }
    return partner;
    }

For the full set of available query methods, including how to query all roles or all business partners a user has access for, see Authorization with semantic roles.