title: Using jdbcAuthentication in Spring Security with Hibernate
body: <p>I just finished an Spring application which is based on the <code>inMemoryAuthentication()</code>, and now, after verified all are working perfectly, I want use the JDBC Authentication.</p>
  
  <p>I have three kinds of classes to handle the connection to the database:</p>
  
  <ol>
  <li><p><code>HibernateConfig</code>, based on the code presented in this blog post.</p></li>
  <li><p>DAO and Entity class, one for each table from my database (in this current state, I have three: User, Role and User_Role)</p></li>
  <li><p>And a UserService class, which implement <code>UserDetailsService</code>.</p></li>
  </ol>
  
  <p>I read some articles on the Internet, and pratically all of them uses the following configuration for the class SecurityConfig:</p>
  
  <pre><code>public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private DataSource dataSource;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth
            .jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery(getUserQuery())
                .authoritiesByUsernameQuery(getAuthoritiesQuery());
      }
  </code></pre>
  
  <p>What is an issue for me, since I don't have a Datasource class in my project. My current SecurityConfig is this:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
              .jdbcAuthentication()
              .usersByUsernameQuery(getUserQuery())
              .authoritiesByUsernameQuery(getAuthoritiesQuery());
      }
  
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf()
                  .disable()
              .authorizeRequests()
                  .antMatchers("/css/**", "/fonts/**", "/image/**", "/js/**").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .formLogin()
                  .loginPage("/spring/index").permitAll()
                  .loginProcessingUrl("/spring/login").permitAll()
                  .usernameParameter("login")
                  .passwordParameter("senha")
                  .successHandler(new CustomAuthenticationSuccessHandler())
                  .failureHandler(new CustomAuthenticationFailureHandler())
                  .and()
              .logout()
                  .logoutUrl("/spring/logout")
                  .logoutSuccessUrl("/spring/index").permitAll();
      }
  
      private String getUserQuery() {
          return "SELECT login as username, senha as password "
                  + "FROM usuario "
                  + "WHERE login = ?";
      }
  
      private String getAuthoritiesQuery() {
          return "SELECT DISTINCT usuario.login as username, autorizacao.descricao as authority "
                  + "FROM usuario, autorizacao_usuario, autorizacao "
                  + "WHERE usuario.id = autorizacao_usuario.fk_usuario "
                  + "AND autorizacao.id = autorizacao_usuario.fk_autorizacao "
                  + "AND usuario.login = ? ";
      }
  
  }
  </code></pre>
  
  <p>As you can see, I already made some adaptations for use JDBC Authentication, but I am still missing the part where I link this class to my Hibernate class.</p>
  
  <p>Someone have any suggestions in how to make this?</p>
  
  <p><strong>my HibernateConfig is this</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource({ "classpath:persistence.properties" })
  @ComponentScan({ "org.webapp.persistence" })
  public class HibernateConfig {
  
     @Autowired
     private Environment env;
  
     @Bean
     public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(restDataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.webapp.persistence.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
  
        return sessionFactory;
     }
  
     @Bean
     public DataSource restDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassname"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.pass"));
  
        return dataSource;
     }
  
     @Bean
     @Autowired
     public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);
  
        return txManager;
     }
  
     @Bean
     public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
     }
  
     Properties hibernateProperties() {
        return new Properties() {
           /**
           * 
           */
          private static final long serialVersionUID = 1L;
  
          {
              setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
              setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
              setProperty("hibernate.globally_quoted_identifiers", "true");
           }
        };
     }
  }
  </code></pre>