Sub topic 2
Bearer tokens on the wire
This chapter assumes you know basic HTTP (headers, status codes) and the OAuth2 roles from the previous chapter. It covers how a client sends an access token to a resource server and how the server finds it in the request.
What "bearer" means
A bearer token is a credential that works for whoever holds it. The resource server does not ask who is sending the token. It only checks whether the token is valid. If someone copies your token, they can call the API as you until it expires.
This is why the transport matters. Bearer tokens must only travel over HTTPS.
The Authorization header
RFC 6750 defines how to send a bearer token. The client puts it in the Authorization request header, using the Bearer scheme followed by one space and the token:
GET /orders HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhbGljZSJ9.c2ln
Accept: application/jsonThe scheme name is case-insensitive in the RFC. The token itself is opaque to HTTP. It may be a JWT or a random string, and the header looks the same either way.
The same call from Java, with no libraries beyond the JDK:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class CallOrders {
public static void main(String[] args) throws Exception {
String token = System.getenv("ACCESS_TOKEN");
HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.example.com/orders"))
.header("Authorization", "Bearer " + token)
.GET()
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}The request flow
The client gets the token from the authorization server, then reuses it for each API call until it expires.
How Spring Security reads the header
When you enable oauth2ResourceServer, Spring Security adds a BearerTokenAuthenticationFilter. It uses a BearerTokenResolver, by default DefaultBearerTokenResolver, to pull the token string from the request. The filter then hands the string to an authentication provider, which validates it. That step is covered in later chapters.
The default resolver behaves as follows:
- Header present and well-formed: it returns the token and authentication continues.
- No header: it returns nothing. The request stays unauthenticated, and a protected endpoint answers 401 with a
WWW-Authenticate: Bearerheader. - Token rejected later: the response is 401 and
WWW-Authenticateincludeserror="invalid_token".
RFC 6750 also allows a token in a form body or a query parameter. Spring Security disables both by default. Query strings end up in server logs, browser history, and proxy logs, so a token there leaks easily.
Common errors
- Wrong scheme:
Authorization: Basic ...orAuthorization: eyJ...(noBearer). The resolver ignores it and you get 401. - Two spaces or a missing space:
Bearer tokenorBearertokenis malformed. - Plain HTTP: the call may work in development, but the token is readable on the network.
- Browsers and cookies: a browser sends cookies automatically, but it never adds a bearer header by itself. Your front-end code must set the header on each request.
Exercise
A client calls GET /orders without any Authorization header, and the endpoint requires authentication. Which status code and header does the resource server return, and why is the token never validated?
Show answer
It returns 401 Unauthorized with WWW-Authenticate: Bearer. The BearerTokenResolver finds no token in the request, so there is nothing to validate. The request stays unauthenticated and the authorization rules reject it.