Spring Security java configuration and login form
<p>I've read about spring security also seen some examples but I can't make it work... I'm not sure if I'm missing something. Please I'll appreciate some explication because I'm getting hard time to understand this. </p>

<p>Using Spring mvc 4.3.3, Spring Security 4.2.0, Tiles 3, CSS, Java 1.7, Eclipse neon.</p>

<p>1.- My first page is login page (I don't use a home page or index).</p>

<p>2.- I want Spring Security take user and pass from my login (first page shown in browser), also I'm using <code>&lt;form action="&lt;c:url value='j_spring_security_check' /&gt;" method="post"&gt;</code> in my login but something is wrong.</p>

<p><a href="https://i.stack.imgur.com/Rcc0g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rcc0g.png" alt="enter image description here"></a></p>

<p>3.- I want it to redirect to the same view to all users /myPanel (I'll change menus acording to user role)</p>

<p>Structure;</p>

<p><a href="https://i.stack.imgur.com/aZ2IV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aZ2IV.png" alt="enter image description here"></a></p>

<p>Classes (removed imports and packages); UPDATE:</p>

<p>ApplicationContextConfig.java</p>

<pre><code>@Configuration
@ComponentScan("mx.com.myapp.*")
@Import({ SecurityConfig.class })
public class ApplicationContextConfig {

    @Bean(name = "viewResolver")
    public ViewResolver getViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();

        // TilesView 3
        viewResolver.setViewClass(TilesView.class);

        return viewResolver;
    }

    @Bean(name = "tilesConfigurer")
    public TilesConfigurer getTilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();

        // TilesView 3
        tilesConfigurer.setDefinitions("/WEB-INF/tiles.xml");

        return tilesConfigurer;
    }
</code></pre>

<p>WebMvcConfig.java:   </p>

<pre><code>    @Configuration
//@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

//  @Override
//  public void addResourceHandlers(ResourceHandlerRegistry registry) {
//
//      // Default..
//  }
//
//  @Override
//  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//      configurer.enable();
//  }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}
</code></pre>

<p>SpringWebAppInitializer.java</p>

<pre><code>public class SpringWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
                new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        // UtF8 Charactor Filter.
        FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);

        fr.setInitParameter("encoding", "UTF-8");
        fr.setInitParameter("forceEncoding", "true");
        fr.addMappingForUrlPatterns(null, true, "/*");
    }

}
</code></pre>

<p>SpringSecurityInitializer.java</p>

<pre><code>public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {

}
</code></pre>

<p>SecurityConfig.java</p>

<pre><code>@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                   .withUser("mkyong").password("123456").roles("ADMIN");
        System.out.println("SpringSecurity ConfigureGlobal");
    }

    //  .csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
//  @Override
//  protected void configure(HttpSecurity http) throws Exception {
//
//      System.out.println("SpringSecurity configure");
//        http.authorizeRequests()
//        .antMatchers("/").permitAll()
//        .antMatchers("/myPanel**").access("hasRole('ADMIN')")
//        .and().formLogin()
//              .usernameParameter("username").passwordParameter("password")
//              .permitAll()
//        .and()
//            .csrf();
//  }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
                .loginPage("/login").failureUrl("/login?error").permitAll().and()
                .logout().permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/path/**");
    }
}
</code></pre>

<p>MyController.java</p>

<pre><code>@Controller
public class MyController {

    @RequestMapping(value = { "/" })
    public String loginPage(Model model) {
        return "loginPage";
    }

    @RequestMapping(value = { "/myPanel" }, method = RequestMethod.POST)
    public ModelAndView myPanel(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("INICIA REQUEST");

        System.out.println("-------- " + request.getParameter("user"));


        String message = "&lt;br&gt;&lt;div style='text-align:center;'&gt;"
                + "&lt;h3&gt;********** This is protected page!&lt;/h3&gt; **********&lt;/div&gt;&lt;br&gt;&lt;br&gt;";

        System.out.println("TERMINA REQUEST");
        return new ModelAndView("homePage", "message", message);
    }

  //Spring Security see this :
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(
        @RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) {

        System.out.println("/login SpringSecurity");

        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid username and password!");
        }

        if (logout != null) {
            model.addObject("msg", "You've been logged out successfully.");
        }
        model.setViewName("homePage");

        return model;
    }
}
</code></pre>

<p>login.jsp</p>

<pre><code>&lt;form action="&lt;c:url value='/login' /&gt;" method="post"&gt;

                &lt;c:if test="${not empty error}"&gt;
                    &lt;div class="error"&gt;${error}&lt;/div&gt;
                &lt;/c:if&gt;
                &lt;c:if test="${not empty msg}"&gt;
                    &lt;div class="msg"&gt;${msg}&lt;/div&gt;
                &lt;/c:if&gt;

                &lt;input type="text" name="username" placeholder="Username" required="required" class="input-txt" /&gt;
                &lt;input type="password" name="password" placeholder="Password" required="required" class="input-txt" /&gt; 
                &lt;div class="login-footer"&gt;
                    &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /&gt;
                    &lt;strong&gt;&lt;a href="#" class="lnk"&gt;I've forgotten something&lt;/a&gt;  | 
                    &lt;a href="#" class="lnk"&gt;Register&lt;/a&gt;&lt;/strong&gt;
                    &lt;button type="submit" class="btn btn--right"&gt;Sign in&lt;/button&gt;
                &lt;/div&gt;
              &lt;/form&gt;
</code></pre>

<p>Many thanks in advance.</p>