title: Spring Security not mapping requests to controllers - Java Config
body: <p>I am creating a spring security application using Spring 4.0.2.RELEASE and Spring Security 3.2.3.RELEASE using entirely java configuration, no xml. The configuration for security seems to be working correctly and is generating the login page correctly and authenticating. However I get 404 errors for all of my pages.</p>
  
  <p>I have controllers and jsp pages set up for each page. When I run the application, I see log messages showing that the controllers were mapped </p>
  
  <p><code>Mapped "{[/ || /welcome] ... onto ... WelcomeController.welcome()</code></p>
  
  <p>However, when I try to hit one of those URLs, I get the login page, then on sucessfull login get a 404 and I see nothing in the log.</p>
  
  <p>Below you will find my controller, my 2 configuration classes, and my 2 initializes.</p>
  
  <p>WelcomeController.java</p>
  
  <pre><code>@Controller
  public class WelcomeController {
      @RequestMapping(value = {"/", "/welcome"})
      public ModelAndView welcome() {
          System.out.println("welcome invoked");
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.setViewName("welcome");
          return modelAndView;
      }
  }
  </code></pre>
  
  <p>Below You will find my configuration files</p>
  
  <p>WebConfig.java</p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  @ComponentScan({ "com.myproject.pagegen.controller" })
  public class WebConfig extends WebMvcConfigurerAdapter {
  
      @Bean
      public ViewResolver viewResolver() {
          InternalResourceViewResolver resolver 
                  = new InternalResourceViewResolver();
          resolver .setViewClass(JstlView.class);
          resolver.setPrefix("/WEB-INF/jsp/");
          resolver.setSuffix(".jsp");
          return resolver;
      }
  </code></pre>
  
  <p>SecurityConfig.java</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth)
          throws Exception {
              auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests()
              .antMatchers("/").hasRole("USER")
              .antMatchers("/welcome").hasRole("USER")
              .anyRequest().anonymous()
              .and().formLogin();
      }
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  </code></pre>
  
  <p>WebAppInitializer.java</p>
  
  <pre><code>public class WebAppInitializer 
      extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[] { WebConfig.class, SecurityConfig.class };
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return null;
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  </code></pre>
  
  <p>SecurityWebAppInitializer.java</p>
  
  <pre><code>public class SecurityWebAppInitializer 
      extends AbstractSecurityWebApplicationInitializer { }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I did find something interesting. If I changed my servletMapping to <code>/*</code> instead of <code>/</code>, I would then get a log message showing that the controller was invoked, but it would have no mapping for the jsp. It seems like it is trying to map the jsp url to the controllers.</p>
  
  <pre><code>welcome invoked
  org.springframework.web.servlet.PageNotFound noHandlerFound
  WARNING: No mapping found for HTTP request with URI [/ROOT/WEB-INF/jsp/welcome.jsp] in DispatcherServlet with name 'dispatcher'
  </code></pre>