title: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [EnableWebMvcConfiguration.class]
body: <p>I am trying to create a rest oauth secured service using spring boot 2.0.0.M4. I am keep getting below exception. I read couple of posts related to error in creating bean with name [...] but nothing worked out.I am posting here my pom.xml along with code. Please guide me if anything clicks to anybody.</p>
  
  <hr>
  
  <pre><code>POM.xml
  
  &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;ctl.spring&lt;/groupId&gt;
      &lt;artifactId&gt;OauthSpringBoot&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;OauthSpringBoot&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.M4&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;aspectj.version&gt;1.8.10&lt;/aspectj.version&gt;
          &lt;maven-jar-plugin.version&gt;2.6&lt;/maven-jar-plugin.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.noraui&lt;/groupId&gt;
              &lt;artifactId&gt;ojdbc7&lt;/artifactId&gt;
              &lt;version&gt;12.1.0.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;    
              &lt;groupId&gt;javax.validation&lt;/groupId&gt;
              &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
              &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;/dependency&gt;
  
              &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
             &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
          &lt;/dependency&gt;
      &lt;!--    &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt; --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security.oauth&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-oauth2&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-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-web&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;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&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-snapshots&lt;/id&gt;
              &lt;name&gt;Spring Snapshots&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&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;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-snapshots&lt;/id&gt;
              &lt;name&gt;Spring Snapshots&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/pluginRepository&gt;
          &lt;pluginRepository&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;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  &lt;/project&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>OauthSpringBootApplication.java
  
  package ctl.spring.OauthSpringBoot;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
  import org.springframework.context.annotation.ComponentScan;
  
  @SpringBootApplication
  //@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
  //@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class })
  @ComponentScan("ctl.spring")
  public class OauthSpringBootApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(OauthSpringBootApplication.class, args);
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>EaseRestController .java
  
  package ctl.spring.controllers;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.MediaType;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import ctl.spring.model.User;
  import ctl.spring.services.UserService;
  
  
  @RestController
  public class EaseRestController {
  
      @Autowired
      UserService userService;
  
      @RequestMapping(value="/hello", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
      public String getHello()
      {
          return "Hello World !!";
      }
  
      @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
       public ResponseEntity&lt;List&lt;User&gt;&gt;  getAllUsers()
      {
          List&lt;User&gt; users =userService.findAllUsers();
  
          if(users ==null || users.size()&lt;0)
              return new ResponseEntity&lt;List&lt;User&gt;&gt;(HttpStatus.NO_CONTENT);
  
          return new ResponseEntity&lt;List&lt;User&gt;&gt;(users,HttpStatus.OK); 
      }
  
      @RequestMapping(value="/user/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity&lt;User&gt; findUser(@PathVariable String id)
         {
             User data=userService.findUserById(Integer.parseInt(id));
  
             if(data== null)
                 return new ResponseEntity&lt;User&gt;(HttpStatus.NOT_FOUND);
             else
                 return new ResponseEntity&lt;User&gt;(data,HttpStatus.OK);
         }
  
        @RequestMapping(value="/createUser/", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
          public ResponseEntity&lt;Void&gt; createUser(@RequestBody User user)
          {
              User dUser= userService.findUserById(Integer.parseInt(user.getId()));
  
              if(dUser== null) {
              userService.saveUser(user);
              return new ResponseEntity&lt;Void&gt;(HttpStatus.CREATED);
              }
              else
              {
                  return new ResponseEntity&lt;Void&gt;(HttpStatus.CONFLICT);
              }
          }
  
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>AuthorizationServer
  
  package ctl.spring.security;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  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.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
  import org.springframework.security.oauth2.provider.token.TokenStore;
  
  @Configuration
  @EnableAuthorizationServer
  public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
      private static String REALM="MY_OAUTH_REALM";
  
      @Autowired
      private TokenStore tokenStore;
  
      @Autowired
      private UserApprovalHandler userApprovalHandler;
  
      @Autowired
      @Qualifier("authenticationManagerBean")
      private AuthenticationManager authenticationManager;
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  
          clients.inMemory()
              .withClient("my-trusted-client")
              .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
              .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
              .scopes("read", "write", "trust")
              .secret("secret")
              .accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
              refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
          endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
                  .authenticationManager(authenticationManager);
      }
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
          oauthServer.realm(REALM+"/client");
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>ResourceServer
  
  package ctl.spring.security;
  
  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;
  import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
  
  @Configuration
  @EnableResourceServer
  public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  
      private static final String RESOURCE_ID = "my_rest_api";
  
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) {
          resources.resourceId(RESOURCE_ID).stateless(false);
      }
  
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http        
          .anonymous()
          .disable()
          .requestMatchers()
          .antMatchers("/users/**")
          .and()
          .authorizeRequests()
          .antMatchers("/user/**").access("hasRole('ADMIN')")
          .and()
          .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>WebSecurityConfigurerAdapter
  
  package ctl.spring.security;
  
  import org.springframework.beans.factory.annotation.Autowired;
  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.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.oauth2.provider.ClientDetailsService;
  import org.springframework.security.oauth2.provider.approval.ApprovalStore;
  import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
  import org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler;
  import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
  import org.springframework.security.oauth2.provider.token.TokenStore;
  import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  
  @Configuration
  @EnableWebSecurity
  public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private ClientDetailsService clientDetailsService;
  
      @Autowired
      public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
          auth.inMemoryAuthentication()
          .withUser("bill").password("abc123").roles("ADMIN").and()
          .withUser("bob").password("abc123").roles("USER");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
          .csrf().disable()
          .anonymous().disable()
          .authorizeRequests()
          .antMatchers("/oauth/token").permitAll();
      }
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
  
      @Bean
      public TokenStore tokenStore() {
          return new InMemoryTokenStore();
      }
  
      @Bean
      @Autowired
      public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
          TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
          handler.setTokenStore(tokenStore);
          handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
          handler.setClientDetailsService(clientDetailsService);
          return handler;
      }
  
      @Bean
      @Autowired
      public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
          TokenApprovalStore store = new TokenApprovalStore();
          store.setTokenStore(tokenStore);
          return store;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>SecurityInitializer
  
  package ctl.spring.security;
  
  import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
  
  public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>MethodSecurityConfig
  
  package ctl.spring.security;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
  import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;
  
  @Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
  public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
      @SuppressWarnings("unused")
      @Autowired
      private OAuth2SecurityConfiguration securityConfig;
  
      @Override
      protected MethodSecurityExpressionHandler createExpressionHandler() {
          return new OAuth2MethodSecurityExpressionHandler();
      }
  }
  </code></pre>
  
  <p>Along with these I have setup <code>AbstractAnnotationConfigDispatcherServletInitializer</code> to add <code>CORSFilter</code>.</p>