title: Spring MVC not authenticating versus database
body: <p>We are working with Spring MVC 4.0, but we are unable to autheticate users against database. We have the following Java security configuration class:</p>
  
  <pre><code>@EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Autowired
  private DataSource dataSource;
  
  /**
   * Configura el acceso a la aplicación para los usuarios.
   */
  @Autowired
  protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      String queryUsers = "SELECT acod_usuario as username, ades_clave as password, true as enabled " +
                          "FROM t_usuarios " +
                          "WHERE acod_usuario = ?";
      String queryAuth = "SELECT acod_usuario as username, 'ROLE_USER' as role" +
                         "FROM t_usuarios " +
                         "WHERE acod_usuario = ?";
      auth.jdbcAuthentication().dataSource(dataSource).
          usersByUsernameQuery(queryUsers).
          authoritiesByUsernameQuery(queryAuth);
  }
  
  /**
   * Configuración de la seguridad HTTP.
   */
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().
              antMatchers("/home","/403","/resources/**").permitAll().
              anyRequest().hasAnyRole("ROLE_USER, ANONYMOUS").
              and().
           formLogin().
              loginPage("/home").usernameParameter("username").passwordParameter("password").
              and().
           logout().
              permitAll().
              and().
           exceptionHandling().accessDeniedPage("/403").and().
              csrf();
  }
  }
  </code></pre>
  
  <p>Pagres authorization is working fine, but we are unable to make login works with a valid user. All users can entry the application, instead they do not exist in database. Data source configuration is working fine too, </p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = "com.justinfact.web.*")
  public class WebConfig extends WebMvcConfigurerAdapter {
  private UsuarioDAO usuarioDAO;
  private CatalogosDAO catalogosDAO;
  private CFEDAO cfeDAO;
  
  /**
   * Registra la base de datos de backend, creando un connection pool.
   * 
   * @return DataSource
   */
  @Bean
  public DataSource dataSource() {
      BasicDataSource basicDataSource = new BasicDataSource();
      basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
      basicDataSource.setUrl("jdbc:mysql://localhost:3306/db");
      basicDataSource.setUsername("xxxxx");
      basicDataSource.setPassword("xxxxx");
      basicDataSource.setInitialSize(5);
      basicDataSource.setMaxActive(10);
      basicDataSource.setAccessToUnderlyingConnectionAllowed(true);
      return basicDataSource;
  }
  
  /**
   * Se registra el template para trabajar con JDBC
   * 
   * @return JdbcTemplate
   */
  @Bean
  public JdbcTemplate jdbcTemplate(DataSource dataSource) {
      return new JdbcTemplate(dataSource);
  }
  </code></pre>
  
  <p>but it seems that no query is executing. We have tested the querys and works fine for us. Login page is an standard one:</p>
  
  <pre><code>&lt;div class="contenedor"&gt;
      &lt;img src="&lt;s:url value='resources' /&gt;/images/logo.jpg"     width="220" height="90" border="0" /&gt;
  &lt;h1&gt;
      Acceso Sistema  
  &lt;/h1&gt;
  
  &lt;br&gt;
  &lt;c:url value="login" var="loginURL"/&gt;
  &lt;sf:form id="homeForm" action="${loginURL}" method="POST" commandName="user"&gt;
      &lt;c:if test="${param.error != null}"&gt;
          &lt;p&gt;Usuario o clave incorrecta&lt;/p&gt;
      &lt;/c:if&gt;
      &lt;c:if test="${param.logout != null}"&gt;
          &lt;p&gt;Ha salido correctamente de la aplicación&lt;/p&gt;
      &lt;/c:if&gt;
      &lt;div class="div_form"&gt;
          &lt;sf:input path="rut" placeholder="RUT empresa"/&gt;
      &lt;/div&gt;
      &lt;div class="div_form"&gt;
          &lt;sf:input path="username" placeholder="nombre de usuario"/&gt;&lt;sf:errors path="username"  /&gt;
      &lt;/div&gt;
      &lt;div class="div_form"&gt;
          &lt;sf:password path="password" placeholder="contraseña"/&gt;
      &lt;/div&gt;
      &lt;div&gt;&lt;a id="ingresar" href="#"&gt;Acceder&lt;/a&gt;&lt;/div&gt;
      &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /&gt;
  &lt;/sf:form&gt;
  </code></pre>
  
  <p></p>
  
  <p>And we have a basic controller file to manage this operations:</p>
  
  <pre><code>@Controller
  public class HomeController {
  
  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  
  
  /**
   * Simply selects the home view to render by returning its name.
   */
  @RequestMapping(value = "home", method = RequestMethod.GET)
  public String home(Locale locale, Model model) {
      logger.info("Welcome home! The client locale is {}.", locale);
  
      /*
      Date date = new Date();
      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
      String formattedDate = dateFormat.format(date);
      model.addAttribute("serverTime", formattedDate );
      */
      model.addAttribute("user",new Login());
      return "home";
  }
  
  /**
   * Realiza la gestión de la operación de login/acceso al sistema con usuario y clave.
   * 
   * @param user
   * @param model
   * @return
   */
  @RequestMapping(value = "login", method = RequestMethod.POST)
  public String login(@Valid Login user, Model model, Errors errors) {
      logger.info("en Login");
      logger.info(errors.toString());
      logger.info(user.toString());
      System.out.println(user);
      model.addAttribute("user", user);
      return "login";
  }
  }
  </code></pre>
  
  <p>What we are doing the wrong way?</p>
  
  <p>Thanks in advance</p>