liveBook · Manning

Resource server

As explained in Spring in Action, Sixth Edition

A resource server is a server that protects resources by validating access tokens and enforcing their scopes before serving requests.

A resource server protects an API. It checks that a request carries a valid, nonexpired access token with permission for the requested operation before the API serves a protected resource. Think of the token as a valet key: it grants access, but only within defined limits. section 8.5 section 8.0

In an OAuth 2 authorization-code flow, a user grants a client application permission, and an authorization server issues the client an access token. The client sends that token with its API request. The resource server belongs to the API; it does not issue the token. section 8.1

To turn an existing Spring Boot API into a JWT-based resource server:

  • Add the spring-boot-starter-oauth2-resource-server dependency. section 8.3
  • Require SCOPE_writeIngredients for POST requests that add ingredients and SCOPE_deleteIngredients for DELETE requests that remove them. The SCOPE_ prefix tells Spring Security to match authorities against token scopes. section 8.3
  • Enable JWT handling with oauth2ResourceServer(oauth2 -> oauth2.jwt()), then configure the authorization server’s JWK set URL. The resource server uses its public key to verify the token signature rather than trusting the token at face value. section 8.3 section 8.2
@Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
       ...
        .antMatchers(HttpMethod.POST, "/api/ingredients")
            .hasAuthority("SCOPE_writeIngredients")
        .antMatchers(HttpMethod.DELETE, "/api/ /ingredients")
            .hasAuthority("SCOPE_deleteIngredients")
     ...
  }

The security configuration shows how POST and DELETE operations receive different scope requirements. section 8.3

Adding an ingredient

An administrative client obtains a token with writeIngredients scope, then sends an ingredient POST with an Authorization: Bearer header. A POST without a token fails; with the appropriate token, the new ingredient can be added. A separate deleteIngredients scope limits permission to remove ingredients. section 8.2 section 8.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through dependency, endpoint rules, JWT enablement, JWK configuration, and request tests; stops short of implementing token validation internals.
Examples
Uses ingredient POST and DELETE endpoints, Spring Security configuration, and curl requests with and without Bearer tokens.
What it assumes
Builds on an existing REST API while introducing the roles of the authorization server, client, user, and resource server.
What you can do after
Operate/configure it with Spring Security to require distinct scopes for ingredient creation and deletion.
How it connects
Connects OAuth 2 authorization-code flow, authorization servers, JWT access tokens, scopes, and public-key signature verification.

Also explained in

Compare how 4 books explain resource server →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.