title: Spring boot + oauth2 : Full authentication is required to access this resource
body: <p>I am trying to implement a authentication-server using spring boot, spring cloud security and spring cloud oauth2.</p>
  
  <p>I am getting below error when I try to hit <a href="http://localhost:8080/auth/oauth/token" rel="noreferrer">http://localhost:8080/auth/oauth/token</a> from postman</p>
  
  <blockquote>
    <p>{
        "error": "unauthorized",
        "error_description": "Full authentication is required to access this resource"
    }</p>
  </blockquote>
  
  <p>Below is my pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; . 
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.teckink.tp&lt;/groupId&gt;
  &lt;artifactId&gt;tp-auth-server&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;name&gt;tp-auth-server&lt;/name&gt;
  &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;spring-cloud.version&gt;Finchley.M9&lt;/spring-cloud.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-oauth2&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
              &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  
  &lt;repositories&gt;
      &lt;repository&gt;
          &lt;id&gt;spring-milestones&lt;/id&gt;
          &lt;name&gt;Spring Milestones&lt;/name&gt;
          &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
          &lt;snapshots&gt;
              &lt;enabled&gt;false&lt;/enabled&gt;
          &lt;/snapshots&gt;
      &lt;/repository&gt;
  &lt;/repositories&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Starter(Main) class:</p>
  
  <pre><code>package com.teckink.tp.authserver;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.security.core.authority.AuthorityUtils;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.provider.OAuth2Authentication;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @SpringBootApplication
  @RestController
  @EnableResourceServer
  @EnableAuthorizationServer
  public class App {
      @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(App.class, args);
      }
  
  }
  </code></pre>
  
  <p>OAuth2Config class which defines client and its secret:</p>
  
  <pre><code>package com.teckink.tp.authserver.security;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  
  @Configuration
  public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
  
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients.inMemory()
                  .withClient("eagleeye")
                  .secret("thisissecret")
                  .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>WebSecurityConfigurer class which defines in-memory user, password and roles: </p>
  
  <pre><code>package com.teckink.tp.authserver.security;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.core.userdetails.UserDetailsService;
  
  @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();
      }
  
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .inMemoryAuthentication()
                  .withUser("john.carnell").password("password1").roles("USER")
                  .and()
                  .withUser("william.woodward").password("password2").roles("USER", "ADMIN");
      }
  }
  </code></pre>
  
  <p>I am calling Rest API from POSTMAN as below-
  Request with authentication screen show as below :
  <a href="https://i.stack.imgur.com/flv7V.png" rel="noreferrer"><img src="https://i.stack.imgur.com/flv7V.png" alt="enter image description here"></a></p>
  
  <p>Request with form-data as below 
  <a href="https://i.stack.imgur.com/1m6qw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1m6qw.png" alt="enter image description here"></a></p>