How to use Spring Boot/Spring Security to wrap a call to an OAuth2 bearer token request?
<p>I'm stuck with the following problem: in order to access an online API, I need to get authenticated. Right now, I do everything in my own code:</p>

<ol>
<li>Call the token URL for a bearer token</li>
<li>Get the bearer token</li>
<li>Call the real service with the bearer token</li>
<li>Get the result</li>
</ol>

<p>Here's the code:</p>

<pre><code>@RestController
public class RandomController {

    private final Random random;

    public RandomController(Random random) {
        this.random = random;
    }

    @RequestMapping(value = "/get", method = GET)
    public int random(@RequestParam(value = "limit", defaultValue = "100") int limit) {
        String bearerToken = getBearerToken();
        int[] bounds = getBounds(bearerToken);
        return computeRandom(bounds[0], bounds[1]);
    }

    private String getBearerToken() {
        RestTemplate tokenTemplate = new RestTemplate();
        MultiValueMap&lt;String, String&gt; body = new LinkedMultiValueMap&lt;&gt;();
        body.add("client_id", "my id");
        body.add("client_secret", "my secret");
        body.add("grant_type", "client_credentials");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        HttpEntity&lt;?&gt; entity = new HttpEntity&lt;&gt;(body, headers);
        ResponseEntity&lt;String&gt; res = tokenTemplate.exchange(
                "https://bearer.token/get", POST, entity, String.class);
        Map&lt;String, Object&gt; map = new BasicJsonParser().parseMap(res.getBody());
        return (String) map.get("access_token");
    }

    private int[] getBounds(String bearerToken) {
        RestTemplate configurationTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + bearerToken);
        HttpEntity&lt;?&gt; entity = new HttpEntity&lt;&gt;(headers);
        ResponseEntity&lt;String&gt; res = configurationTemplate.exchange(
                "https://configurations.com/bounds", HttpMethod.GET, entity, String.class);
        Map&lt;String, Object&gt; map = new BasicJsonParser().parseMap(res.getBody());
        Map&lt;String, Long&gt; value = (Map&lt;String, Long&gt;) map.get("value");
        int lowerBound = value.get("lower").intValue();
        int upperBound = value.get("upper").intValue();
        return new int[]{lowerBound, upperBound};
    }

    private int computeRandom(int lowerBound, int upperBound) {
        int difference = upperBound - lowerBound;
        int raw = random.nextInt(difference);
        return raw + lowerBound;
    }
}
</code></pre>

<p>It works, but I'm wasting a call to the token URL at every call. This is how I'd like it to work:</p>

<ol>
<li>Call the real service</li>
<li>If getting a 401

<ol>
<li>Call the token URL for a bearer token</li>
<li>Get the bearer token</li>
<li>Recall the service with the bearer token</li>
</ol></li>
<li>Get the result</li>
</ol>

<p>I could do that in my code, but I'm already using Spring Boot. I'm wondering how to achieve that. Is there an existing filter, interceptor, whatever?</p>

<p>Thanks for your insights.</p>