title: Get principal in Spring Boot resource server from JWT token
body: <p>I have a separate auth server and a resource server - both Spring Boot applications. I am using OAuth2 with JWT tokens for authentication and authorisation. </p>
  
  <p>I can get a token by accessing the auth server:</p>
  
  <pre><code>curl -X POST --user 'client:secret' -d 'grant_type=password&amp;username=john&amp;password=123' http://localhost:9000/auth-server/oauth/token
  </code></pre>
  
  <p>and use it when getting a resource from the resource server (running on different server) by attaching the token to the request header.</p>
  
  <p>What is unclear to me though, is to determine which user is logged in in the resource server.</p>
  
  <p>In the auth server, I can do something like this:</p>
  
  <pre><code>@RequestMapping("/user")
  public Principal user(Principal user) {
      return user;
  }
  </code></pre>
  
  <p>and this endpoint will get me the current user based on the token used. If I try to do the same thing in the resource server though, I only get empty response.</p>
  
  <p>I need to get the id of the authenticated user somehow in the resource server to decide whether I should return some data to him (I have some resources which can be accessed only by their owner). </p>
  
  <p>How could I do that? I thought that by using JWT, I don't need to share the same token store.</p>
  
  <p>I can provide code samples if my question is not clear.. I am just looking for a confirmation that my assumption is correct and this behaviour can be achieved - this is my first time experimenting with spring security.</p>
  
  <p>The resource server is linked to the auth server in the application.properties:</p>
  
  <pre><code>security.oauth2.resource.userInfoUri: http://localhost:9000/auth-server/user
  </code></pre>