How do I map OAuth 2 token to UserDetails object in a resource server? : <p>I have 2 separate Spring Boot applications, one serving as an an OAuth 2 authorization server, and the other as resource server. I'm using Spring's <code>RemoteTokenServices</code> in my resource server to check tokens from the authorization server. Now, I'm trying to define protected controller code in my resource server application, but I'm not sure how to map the <code>UserDetails</code> class to the authentication principal provided through the OAuth 2 mechanism. </p>

<p>I have set up my authorization server with a custom <code>TokenEnhancer</code> that adds more details to the token such that <code>/oauth/check_token?token=&lt;token&gt;</code> returns with custom fields, which I want to map to my resource server controllers.</p>

<p>In a more monolithic setup where the authorization server is also the resource server, I can define controller methods that make use of the authenticated principal this way:</p>

<pre><code>//User implements UserDetails
public Map&lt;String, Object&gt; getResource(@AuthenticationPrincipal User user) {
    //code that uses the user object
}
</code></pre>

<p>However, this doesn't seem to work as straight forward in a more distributed approach. The mapping fails, and the <code>user</code> parameter ends up being a null object. I tried using the following approach:</p>

<pre><code>public Map&lt;String, Object&gt; getResource(Authentication authentication) {
    //code that uses the authentication object
}
</code></pre>

<p>While the code above successfully maps the authentication details, it doesn't provide a way for me to directly access the custom fields I've set through the <code>TokenEnhancer</code> I mentioned earlier. I can't seem to find anything from the Spring docs regarding this.</p>
