title: RestController not working in oauth2 Spring Boot
body: <p>I have setup a auth server and resource server as mentioned in the below article
  <a href="http://www.hascode.com/2016/03/setting-up-an-oauth2-authorization-server-and-resource-provider-with-spring-boot/" rel="nofollow noreferrer">http://www.hascode.com/2016/03/setting-up-an-oauth2-authorization-server-and-resource-provider-with-spring-boot/</a></p>
  
  <p>I downloaded the code and it is working fine. Now the issue is that in the resource provider project there is only one RestController annotated class as shown below</p>
  
  <pre><code>package com.hascode.tutorial;
  
  import java.util.UUID;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Scope;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @SpringBootApplication
  @RestController
  @EnableResourceServer
  public class SampleResourceApplication {
      public static void main(String[] args) {
          SpringApplication.run(SampleResourceApplication.class, args);
      }
  
      @RequestMapping("/")
      public String securedCall() {
          return "success (id: " + UUID.randomUUID().toString().toUpperCase() + ")";
      }
  }
  </code></pre>
  
  <p>Now when I create a different class annotated with <strong>@RestController</strong> as shown below</p>
  
  <pre><code>@RestController
  @RequestMapping("/public")
  public class PersonController {
  
      @Autowired
      private PersonRepository personRepo;
  
      @RequestMapping(value = "/person", method = RequestMethod.GET)
      public ResponseEntity&lt;Collection&lt;Person&gt;&gt; getPeople() {
          return new ResponseEntity&lt;&gt;(personRepo.findAll(), HttpStatus.OK);
      }
  
      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public ResponseEntity&lt;Person&gt; getPerson(@PathVariable long id) {
          Person person = personRepo.findOne(id);
  
          if (person != null) {
              return new ResponseEntity&lt;&gt;(personRepo.findOne(id), HttpStatus.OK);
          } else {
              return new ResponseEntity&lt;&gt;(null, HttpStatus.NOT_FOUND);
          }
      }
  
      @RequestMapping(method = RequestMethod.POST)
      public ResponseEntity&lt;?&gt; addPerson(@RequestBody Person person) {
          return new ResponseEntity&lt;&gt;(personRepo.save(person), HttpStatus.CREATED);
      }
  
      @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
      public ResponseEntity&lt;Void&gt; deletePerson(@PathVariable long id, Principal principal) {
          Person currentPerson = personRepo.findByUsername(principal.getName());
  
          if (currentPerson.getId() == id) {
              personRepo.delete(id);
              return new ResponseEntity&lt;Void&gt;(HttpStatus.OK);
          } else {
              return new ResponseEntity&lt;Void&gt;(HttpStatus.UNAUTHORIZED);
          }
      }
  
      @RequestMapping(value = "/{id}/parties", method = RequestMethod.GET)
      public ResponseEntity&lt;Collection&lt;Party&gt;&gt; getPersonParties(@PathVariable long id) {
          Person person = personRepo.findOne(id);
  
          if (person != null) {
              return new ResponseEntity&lt;&gt;(person.getParties(), HttpStatus.OK);
          } else {
              return new ResponseEntity&lt;&gt;(null, HttpStatus.NOT_FOUND);
          }
      }
  
  }
  </code></pre>
  
  <p>but when I tried to access the service (<a href="http://localhost:9001/resources/public/person" rel="nofollow noreferrer">http://localhost:9001/resources/public/person</a>) I am getting 404</p>
  
  <pre><code>{
      "timestamp": 1508752923085,
      "status": 404,
      "error": "Not Found",
      "message": "No message available",
      "path": "/resources/public/person"
  }
  </code></pre>
  
  <p>when I access <a href="http://localhost:9001/resources/" rel="nofollow noreferrer">http://localhost:9001/resources/</a> I am getting the correct result like</p>
  
  <p><strong>success (id: 27DCEF5E-AF11-4355-88C5-150F804563D0)</strong></p>
  
  <p>Should I register the Contoller anywherer or am I missing any configuration</p>
  
  <p><a href="https://bitbucket.org/hascode/spring-oauth2-example" rel="nofollow noreferrer">https://bitbucket.org/hascode/spring-oauth2-example</a></p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p><strong>ResourceServerConfiguration.java</strong></p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  
  @Configuration
  @EnableResourceServer
  public class ResourceServerConfiguration  extends ResourceServerConfigurerAdapter {
  
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http
          .anonymous().and()
          .authorizeRequests()
          .antMatchers("/resources/public/**").permitAll()
          .antMatchers("/resources/private/**").authenticated();
      }
  }
  </code></pre>
  
  <p><strong>OAuth2SecurityConfiguration.java</strong></p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  
  @Configuration
  @EnableWebSecurity
  public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http
          .anonymous().and()
          .authorizeRequests()
          .antMatchers("/resources/public/**").permitAll()
          .antMatchers("/resources/private/**").authenticated();
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <pre><code>@Override
      public void configure(HttpSecurity http) throws Exception {
           http.authorizeRequests()
           .antMatchers("/resources/public/**").permitAll() //Allow register url
           .anyRequest().authenticated().and()
           .antMatcher("/resources/**").authorizeRequests() //Authenticate all urls with this body /api/home, /api/gallery
           .antMatchers("/resources/**").hasRole("ADMIN")
           .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler()); //This is optional if you want to handle exception
      }
  </code></pre>