Guide · Spring Boot

JWT and RBAC in Spring Boot, in a way you can maintain

Validating the token is the easy part. The hard part is deciding who may call each route and changing that rule without a deploy. This guide shows two paths: plain Spring Security and GatekeeperID IAM.

Updated

The concepts, one line each

  • JWT: a signed token that says who the user is (claims) and when it expires.
  • RBAC: users get roles or groups, and each role carries permissions such as orders:create.
  • Authentication answers "who are you?". Authorization answers "may you do this?".

Path 1: Spring Security as a resource server

With spring-boot-starter-oauth2-resource-server, Spring validates the JWT signature against the issuer and you declare the permission on the method.

application.yml
spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://your-issuer/realms/your-company
SecurityConfig.java
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
  @Bean
  SecurityFilterChain api(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(a -> a.anyRequest().authenticated())
        .oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()));
    return http.build();
  }
}

@PreAuthorize("hasAuthority('SCOPE_orders:create')")
@PostMapping("/orders")
public Order create(@RequestBody NewOrder body) { ... }

By default Spring maps the scope claim to SCOPE_* authorities. To use Keycloak roles you need your own converter that reads realm_access.roles.

It works. The cost shows up later: one claims converter per issuer, permissions locked in annotations that only change with a deploy, and no screen for an administrator to see who can do what.

Path 2: GatekeeperID IAM

No issuer to run and no converter to write. One dependency, the Console credentials, and the permission on the route.

pom.xml
<dependency>
  <groupId>cloud.gatekeeperid</groupId>
  <artifactId>gatekeeper-spring-boot-starter</artifactId>
  <version>0.3.5</version>
</dependency>
application.yml
gatekeeper:
  application-name: orders-api
  tenant-id: ${GATEKEEPER_TENANT_ID}
  client-id: ${GATEKEEPER_CLIENT_ID}
  hmac:
    secret: ${GATEKEEPER_HMAC_SECRET}
OrdersController.java
@GatekeeperProtected(permission = "orders:create")
@PostMapping("/orders")
public Order create(@RequestBody NewOrder body) {
  return orders.create(body);
}

On startup the SDK sends the protected routes to the Console. There you create the orders:create permission, bind it to a group, and add users to the group.

On every request the SDK validates the token and asks GatekeeperID whether that user may call that method and path. The answer is 200 or 403, and changing who can do what needs no deploy.

Which path to pick

SituationBest path
You already have a token issuer and rules rarely changePlain Spring Security
You also need login, sign-up, and password resetGatekeeperID IAM
Your customer’s admin must manage access on their ownGatekeeperID IAM
SaaS with many isolated customersGatekeeperID IAM

RBAC in Spring Boot without writing the filter

Request early access and protect your first route today.

Frequently asked questions

Which versions does the SDK support?
Java 17 or newer and Spring Boot 3.x. The starter is on Maven Central as cloud.gatekeeperid:gatekeeper-spring-boot-starter.
Where does user login live?
In a controller of yours that delegates to GatekeeperAuthService: login, sign-up, refresh, logout, and profile. The quickstart walks through it.
What if my backend is Node.js?
The @gatekeeperid/sdk package has the same API. You call authorize in the route middleware instead of using the annotation.