# "On-Behalf-Of" (OBO) implementation in Java okta-spring-boot-starter

**URL:** <https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [January 19, 2026, 11:16am UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380 "2026-01-19T11:16:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmiller](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dmiller/32/70911_2.png) [@dmiller](https://community.auth0.com/u/dmiller)\
**Post date:** [January 19, 2026, 11:16am UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/1 "2026-01-19T11:16:15Z")

</div>

My understanding is that for a machine to machine application, an OBO ‘on-behalf-of’ flow is good practice.

Thus what is the correct way to implement the OBO, ‘on-behalf-of’ flow for a Java implementation using the okta-spring-boot-starter library?  
Whereupon, a JWT will already be available in the security context, then that JWT can be used to call the Auth0 token endpoint to get another JWT to then call a downstream service, with all claims intact.

Is there an example that I have missed?

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [January 19, 2026, 1:50pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/3 "2026-01-19T13:50:08Z")

</div>

Hi @dmiller,

Welcome to the Auth0 Community!

The “On-Behalf-Of” (OBO) pattern is indeed a best practice for preserving user identity across service boundaries. While `okta-spring-boot-starter` simplifies resource server configuration, it doesn’t provide a “one-click” OBO implementation because Auth0 handles this via the **OAuth 2.0 Token Exchange** (RFC 8693) or **Client Credentials** flow, depending on whether you need to strictly maintain the original user’s identity or just the service’s authority.

Since you are using Auth0, the modern way to achieve this is through **Token Exchange**.

### 1. Enable Token Exchange in Auth0

You must first ensure your “API A” (the Service) is allowed to perform the exchange.

- Go to **Auth0 Dashboard \> Applications**.
- Select your **Machine-to-Machine** application representing “API A”.
- In **Advanced Settings \> Grant Types** , ensure **Token Exchange** is enabled.

### 2. Implementation in Java (Spring Boot)

You can use the `WebClient` or `RestTemplate` to perform the exchange. The `okta-spring-boot-starter` manages the _incoming_ token, but you’ll need a small helper to get the _outgoing_ token.

```Java
@Service
public class DownstreamService {

    @Value("${okta.oauth2.issuer}")
    private String issuer;

    @Value("${okta.oauth2.client-id}")
    private String clientId;

    @Value("${okta.oauth2.client-secret}")
    private String clientSecret;

    public String getOnBehalfOfToken() {
        // 1. Get the current user's JWT from the Security Context
        JwtAuthenticationToken authentication = (JwtAuthenticationToken) 
            SecurityContextHolder.getContext().getAuthentication();
        String subjectToken = authentication.getToken().getTokenValue();

        // 2. Exchange it for a token scoped for the downstream API
        WebClient webClient = WebClient.builder().baseUrl(issuer).build();

        return webClient.post()
            .uri("/oauth/token")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .body(BodyInserters.fromFormData("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
                .with("subject_token", subjectToken)
                .with("subject_token_type", "urn:ietf:params:oauth:token-type:access_token")
                .with("client_id", clientId)
                .with("client_secret", clientSecret)
                .with("audience", "https://your-downstream-api-identifier")
                .with("scope", "read:data")) // Requested scopes for API B
            .retrieve()
            .bodyToMono(TokenResponse.class)
            .block()
            .getAccessToken();
    }
}

```

If you have any further questions, please don’t hesitate to reach out.

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![dmiller](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dmiller/32/70911_2.png) [@dmiller](https://community.auth0.com/u/dmiller)\
**Post date:** [January 19, 2026, 2:12pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/4 "2026-01-19T14:12:48Z")

</div>

Thanks @vlad.murarasu ,

Much appreciated.

One thing though, I don’t have and option for the grant type ‘Token Exchange’

 ![image](https://global.discourse-cdn.com/auth0/original/3X/9/8/98ff3bf8e972728908a353bbc7a1414ccc9ca554.png)

Is there another setting I need to use to enable it?

Thanks

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [January 19, 2026, 2:44pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/5 "2026-01-19T14:44:55Z")

</div>

Hi @dmiller,

It should be Client Credentials, but I see it’s greyed out. See the documentation provided in that warning on how to enable it.

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![dmiller](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dmiller/32/70911_2.png) [@dmiller](https://community.auth0.com/u/dmiller)\
**Post date:** [January 19, 2026, 3:47pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/6 "2026-01-19T15:47:23Z")

</div>

Thanks @vlad.murarasu ,

So using an application with client credentials,

 ![image](https://global.discourse-cdn.com/auth0/original/3X/d/b/dbd5584392fbe5f0361aa6f2240389788f62533c.png)

I get the following:

`{￼“error”: “invalid_request”,￼“error_description”: “Invalid subject_token_type.”￼}`

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [January 19, 2026, 6:39pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/7 "2026-01-19T18:39:06Z")

</div>

Hi again @dmiller,

I think I made an error in the code snippet. Instead of

> [@vlad.murarasu](#):
>
> ```Java
> ("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
> 
> ```

it should be

```Java
("grant_type", "urn:ietf:params:oauth:grant-type:client-credentials")

```

I hope this fixes the issue

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![dmiller](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dmiller/32/70911_2.png) [@dmiller](https://community.auth0.com/u/dmiller)\
**Post date:** [January 20, 2026, 9:30am UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/8 "2026-01-20T09:30:09Z")

</div>

> [@vlad.murarasu](#):
>
> `token-exchange`

Hi @vlad.murarasu ,

I have it working, but only with `grant_type: "client_credentials"`  
And it does not return the sub and claims that were specified in the `“subject-token"` for the token exchange.  
Is there another setting I’m missing?

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [January 28, 2026, 7:22pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/9 "2026-01-28T19:22:42Z")

</div>

Hi @dmiller,

Can you also try

```java
.with("subject_token_type", "access_token")

```

instead of

```java
.with("subject_token_type", "urn:ietf:params:oauth:token-type:access_token")

```

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![dmiller](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dmiller/32/70911_2.png) [@dmiller](https://community.auth0.com/u/dmiller)\
**Post date:** [February 23, 2026, 10:07am UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/10 "2026-02-23T10:07:09Z")

</div>

Thanks @vlad.murarasu ,

So realised I really needed to start from scratch and implement it properly. Key thing I was missing was adding the action and linking that to the user defined ‘subject-token-type’.  
Found it all here:  
[Custom Token Exchange - Auth0 Docs](https://auth0.com/docs/authenticate/custom-token-exchange)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 9, 2026, 10:07am UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/11 "2026-03-09T10:07:10Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [September 1, 2026, 11:12pm UTC](https://community.auth0.com/t/on-behalf-of-obo-implementation-in-java-okta-spring-boot-starter/196380/12 "2026-09-01T23:12:55Z")

</div>

Hi everyone!

I will be optimizing the solution provided for future reference on this topic in case anybody else runs into the same issue.

I can see that you are having issues in implementingthe **On-Behalf-Of (OBO) flow** for a machine-to-machine application using Java and the `okta-spring-boot-starter` library.

**[ROOT CAUSE]**

The `okta-spring-boot-starter` library simplifies **incoming** token validation (resource server configuration) but does **not** provide built-in support for **outgoing** token exchange. This is because:

1. **Token Exchange is not automatic** — You must explicitly call Auth0’s token endpoint
2. **Custom subject token type required** — Auth0 needs to know what type of token you’re exchanging (access token, ID token, etc.)
3. **Action configuration needed** — You must create an Auth0 Action to define how the subject token should be processed

**SOLUTION**

#### **Step 1: Enable Token Exchange in Auth0**

**Navigate to** Auth0 Dashboard → Applications → Your Machine-to-Machine App → Advanced Settings → Grant Types.

**Verify** that **Token Exchange** is enabled. If not, enable it.

#### **Step 2: Create a custom subject token type in Auth0 (via Action)**

Auth0 requires you to define a custom subject token type so it knows how to process your incoming token.

**Create an Auth0 Action:**

1. **Go to** Dashboard → Actions → Library → Create Action
2. **Name it** something like `handle-token-exchange`
3. **Add this code:**

```auto
exports.onExecuteTokenExchange = async (event, api) => {
  // Define your custom subject token type
  if (event.request.body.subject_token_type === 'urn:custom:subject-token-type') {
    // Validate and process the incoming token
    api.access.addClaimToToken('custom_claim', 'value');
  }
};

```

1. **Deploy** the Action
2. **Link it** to your API in Dashboard → APIs → Your API → Actions → Token Exchange

#### **Step 3: Implement Token Exchange in Java (Spring Boot)**

Create a service that extracts the current JWT and exchanges it for a downstream API token:

```auto
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

@Service
public class TokenExchangeService {

    @Value("${auth0.domain}")
    private String auth0Domain;

    @Value("${auth0.client-id}")
    private String clientId;

    @Value("${auth0.client-secret}")
    private String clientSecret;

    /**
     * Exchange the current user's JWT for a token scoped to a downstream API
     */
    public String getOnBehalfOfToken(String downstreamApiAudience) {
        // Step 1: Extract the current user's JWT from the security context
        JwtAuthenticationToken authentication = 
            (JwtAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        
        if (authentication == null || authentication.getToken() == null) {
            throw new IllegalStateException("No JWT found in security context");
        }

        String subjectToken = authentication.getToken().getTokenValue();

        // Step 2: Exchange the subject token for a downstream API token
        WebClient webClient = WebClient.builder()
            .baseUrl("https://" + auth0Domain)
            .build();

        TokenExchangeResponse response = webClient.post()
            .uri("/oauth/token")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .body(BodyInserters.fromFormData("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
                .with("subject_token", subjectToken)
                .with("subject_token_type", "urn:custom:subject-token-type") // Custom type defined in Action
                .with("client_id", clientId)
                .with("client_secret", clientSecret)
                .with("audience", downstreamApiAudience)
                .with("scope", "read:data write:data")) // Scopes for downstream API
            .retrieve()
            .bodyToMono(TokenExchangeResponse.class)
            .block();

        if (response == null || response.getAccessToken() == null) {
            throw new RuntimeException("Failed to exchange token");
        }

        return response.getAccessToken();
    }
}

```

**Create a response DTO:**

```auto
import com.fasterxml.jackson.annotation.JsonProperty;

public class TokenExchangeResponse {
    
    @JsonProperty("access_token")
    private String accessToken;
    
    @JsonProperty("token_type")
    private String tokenType;
    
    @JsonProperty("expires_in")
    private Long expiresIn;

    // Getters and setters
    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public Long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }
}

```

#### **Step 4: Use the exchanged token to call the downstream API**

```auto
@Service
public class DownstreamApiClient {

    private final TokenExchangeService tokenExchangeService;
    private final WebClient webClient;

    public DownstreamApiClient(TokenExchangeService tokenExchangeService, WebClient.Builder webClientBuilder) {
        this.tokenExchangeService = tokenExchangeService;
        this.webClient = webClientBuilder.build();
    }

    public String callDownstreamApi(String downstreamApiUrl, String downstreamApiAudience) {
        // Step 1: Get the OBO token
        String oboToken = tokenExchangeService.getOnBehalfOfToken(downstreamApiAudience);

        // Step 2: Call the downstream API with the OBO token
        return webClient.get()
            .uri(downstreamApiUrl)
            .header("Authorization", "Bearer " + oboToken)
            .retrieve()
            .bodyToMono(String.class)
            .block();
    }
}

```

#### **Step 5: Configure application properties**

Add to `application.yml` or `application.properties`:

```auto
auth0:
  domain: your-auth0-domain.auth0.com
  client-id: your-machine-to-machine-client-id
  client-secret: your-machine-to-machine-client-secret

okta:
  oauth2:
    issuer: https://your-auth0-domain.auth0.com/
    client-id: your-resource-server-client-id

```

Kind Regards,  
Nik
