title: How to add Spring WebSecurityConfig to an existing project
body: <p>I want to add to a an existing spring REST api project a simply configuration for WebSecurityConfigurerAdapter to test it. But when spring starts it doesn't load the configuration. Maybe I need to add it to the application context but I don't know how to do it.</p>
  
  <p>If I make a <code>curl localhost:8080/</code> always get an unauthorized response, so I think that is not loading the configuration, why it is? Or, how I should load it? 
  In all the diversed projects that I saw on github, they never do special things to load it! Its maybe because its loading an embbeded servlet first?</p>
  
  <p>This is the simple web security configuration:</p>
  
  <pre><code>@SuppressWarnings("SpringJavaAutowiringInspection")
  @Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private UserDetailsService userDetailsService;
  
  @Autowired
  public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
      authenticationManagerBuilder
              .userDetailsService(this.userDetailsService)
              .passwordEncoder(passwordEncoder());
  }
  
  @Bean
  public PasswordEncoder passwordEncoder() {
      return new BCryptPasswordEncoder();
  }
  
  @Override
  protected void configure(HttpSecurity httpSecurity) throws Exception {
      httpSecurity
              // we don't need CSRF because our token is invulnerable
              .csrf().disable()
  
              // don't create session
              .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  
              .authorizeRequests()
              //.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
  
              // allow anonymous resource requests
              .antMatchers(
                      HttpMethod.GET,
                      "/",
                      "/*.html",
                      "/favicon.ico",
                      "/**/*.html",
                      "/**/*.css",
                      "/**/*.js",
                      "/**"
              ).permitAll()
              .antMatchers("/auth/**").permitAll()
              .anyRequest().authenticated();
  
      // disable page caching
      httpSecurity.headers().cacheControl();
  }
  }
  </code></pre>
  
  <p>And this is my Application</p>
  
  <pre><code>@Configuration
  @EnableConfigurationProperties
  @ComponentScan(basePackageClasses = SimpleCORSFilter.class)
  @EnableAutoConfiguration    org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
  @EntityScan(basePackages = "com.thing.model")
  @RestController
  public class Application {
  
  
      @Bean
      public FilterRegistrationBean filterRegistrationBean() {
          FilterRegistrationBean registrationBean = new FilterRegistrationBean();
          CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
          registrationBean.setFilter(characterEncodingFilter);
          characterEncodingFilter.setEncoding("UTF-8");
          characterEncodingFilter.setForceEncoding(true);
          registrationBean.setOrder(Integer.MIN_VALUE);
          registrationBean.addUrlPatterns("/*");
          return registrationBean;
      }    
  
  
  
  
      public static void main(String[] args) {
                  SpringApplication application = new SpringApplication(Application.class);
          SpringApplication.run(Application.class, args);
      }
  
      @RequestMapping("/")
      public String home() {
              return "Hello World";
      }
  </code></pre>
  
  <p><strong>pom.xml dependencies</strong></p>
  
  <pre><code>       &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;
      &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-tomcat&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-actuator&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.geotools&lt;/groupId&gt;
          &lt;artifactId&gt;gt-referencing&lt;/artifactId&gt;
          &lt;version&gt;8.2&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
          &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
          &lt;version&gt;1.7.2&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;version&gt;9.3-1102-jdbc41&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.yaml&lt;/groupId&gt;
          &lt;artifactId&gt;snakeyaml&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;
  </code></pre>