title: Spring Cloud OAuth2Authentication returns NullPointerException
body: <p>I'm slowly moving into understanding Spring Cloud Security. I've created an authorization service and it works when authorizing and returning a token, but doesn't return any current user details when using that token, when getting these from <code>OAuth2Authentication</code>. This two lines return an NPE:</p>
  
  <pre><code>userInfo.put("user", user.getUserAuthentication().getPrincipal());
              userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
  </code></pre>
  
  <p><code>OAuth2Authentication user</code> isn't instantiated and is null, while I understood that it should be by default instantiated by Spring Security. Maybe I am missing some configuration beans? Thanks in advance!</p>
  
  <p><strong>Application.class</strong></p>
  
  <pre><code>@SpringBootApplication
  @RestController
  @EnableResourceServer
  @EnableAuthorizationServer
  public class AuthorizationServiceApplication {
  
      @RequestMapping(value = {"/user"}, produces = "application/json")
      public Map &lt;String, Object&gt; user (OAuth2Authentication user) {
          Map &lt;String, Object&gt; userInfo = new HashMap &lt;&gt;();
          userInfo.put("user", user.getUserAuthentication().getPrincipal());
          userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
          return userInfo;
      }
  
      public static void main (String[] args) {
          SpringApplication.run(AuthorizationServiceApplication.class, args);
      }
  }
  </code></pre>
  
  <p><strong>OAuth2Config.class</strong></p>
  
  <pre><code>@Configuration
  public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
  
      @Value("${token.secret}")
      private String secret;
      private AuthenticationManager authenticationManager;
      private UserDetailsService userDetailsService;
  
      public OAuth2Config (AuthenticationManager authenticationManager, UserDetailsService userDetailsService) {
          this.authenticationManager = authenticationManager;
          this.userDetailsService = userDetailsService;
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients.inMemory()
                  .withClient("eagleeye")
                  .secret(secret)
                  .authorizedGrantTypes("refresh_token", "password", "client_credentials")
                  .scopes("webclient", "mobileclient");
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
          endpoints
                  .authenticationManager(authenticationManager)
                  .userDetailsService(userDetailsService);
      }
  }
  </code></pre>
  
  <p><strong>WebSecurityConfigurer.class</strong></p>
  
  <pre><code>@Configuration
  public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean () throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Override
      @Bean
      public UserDetailsService userDetailsServiceBean() throws Exception {
          return super.userDetailsServiceBean();
      }
  
      // TODO: implemented DB stuff
      @Override
      protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
          authenticationManagerBuilder
                  .inMemoryAuthentication()
                  .withUser("deniss").password("deniss1").roles("USER")
                  .and()
                  .withUser("oksana").password("oksana").roles("USER, ADMIN");
      }
  
      private CsrfTokenRepository csrfTokenRepository() {
          HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
          repository.setSessionAttributeName("_csrf");
          return repository;
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().csrfTokenRepository(csrfTokenRepository());
      }
  }
  </code></pre>