r/SpringBoot • u/hakdogan75 • 5h ago
Discussion Client-side DPoP with Spring Security, adding the proofs yourself and working around a WebFlux token type bug
I maintain authzen-enforcer, an open source Java SDK that enforces authorization decisions of a Policy Decision Point (PDP) through the OpenID AuthZEN API, with a Spring Boot starter. The application calls the PDP with its own token, obtained through the client credentials grant of Spring Security's OAuth 2.0 client. For the latest release I wanted that token to be DPoP-bound (RFC 9449), so that a stolen token alone cannot ask the PDP anything. Spring Security covers DPoP on the resource server side; on the client side the application does part of the work, so here is what I added.
What Spring Security offers (checked with 7.2.0, the version in Spring Boot 4.2.0)
- DPoP on the resource server side:
DPoPAuthenticationProvider, proof decoding and validation, and a replay validator. - The servlet token response converter reads
token_type: DPoP. - Creating proofs is not part of
spring-security-oauth2-client(there is no DPoP class in it as of 7.2.0), so the application adds them to its token requests itself. Without a proof, the authorization server issues a plain bearer token.
What I had to add
- A proof on every token request. On the servlet side, this is a
ClientHttpRequestInterceptoron theRestClientof the token response client. The proof takeshtmandhtufrom the outgoing request, has noathbecause there is no token yet, and is new for every request: - If the authorization server answers
400witherror="use_dpop_nonce", the request is repeated once with the nonce of itsDPoP-Nonceheader. On WebFlux, the same thing is anExchangeFilterFunctionon theWebClient. - Reading the token type on WebFlux. The reactive
OAuth2AccessTokenResponseBodyExtractormaps onlyBearer, so atoken_type: DPoPresponse fails with "tokenType cannot be null". The starter reads the token response itself for now. I reported this as spring-security#20189. - No silent downgrade. If the request carried a proof but the response says
Bearer, the starter fails the request instead of sending an unbound token in place of a bound one. - Requests to the PDP. Each one carries
Authorization: DPoP <token>and a new proof withath, and retries get a new proof too, since a server is expected to refuse a proof it has already seen. - An application's own
OAuth2AccessTokenResponseClientbean would bypass all of this, so with DPoP on the starter refuses to start next to one instead of quietly sending token requests without proofs.
Checking it against a real server
The examples repository runs the Spring MVC and WebFlux applications with DPOP=1 against Keycloak 26.8.2, which is both the authorization server and the PDP there. Keycloak binds the token when the token request carries a proof, with no client setting. Besides the application's own requests, the decision check asks the PDP directly with a token bound to the same key:
DPoP the type of the PEP's token, requested with a proof
200 the PDP answers the token with a proof of its key
401 the PDP refuses the same proof again (a replay)
401 the PDP refuses a proof of another key (a stolen token)
401 the PDP refuses a proof for another endpoint
401 the PDP refuses the token without a proof
401 the PDP refuses the token as a bearer tokenThe replay refusal comes from Keycloak itself, because its AuthZEN endpoints run inside Keycloak. A PDP or resource server elsewhere has to remember the jti values of the proofs it accepts on its own.
Questions
- Is anyone doing client-side DPoP with Spring Security in another way, for example with the authorization code flow, where the same idea applies to more requests?
- Would first-class proof creation in
spring-security-oauth2-clientbe useful to you? The closest open request I found is #18228, which asks for a way to mark a client registration as requiring DPoP.