nader
nader

Reputation: 141

org.springframework.security.authentication.InternalAuthenticationServiceException: Could not get JDBC Connection

I am facing an issue with my spring security ( version 4) at this moment.

org.springframework.security.authentication.InternalAuthenticationServiceException: Could not get JDBC Connection

Here is the AppConfig class:

 @EnableWebMvc
 @Configuration
  @ComponentScan({ "controller" })
 @Import({ SecurityConfig.class })
  public class AppConfig {

@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");
    System.out.println("JDBC Driver Found");
    DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
       driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/QSQL");
    driverManagerDataSource.setUsername("root");
    driverManagerDataSource.setPassword("password");
    return driverManagerDataSource;
}

 @Bean
  public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/Pages/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }

}

Here is my SecurityConfig class:

@Configuration
 @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 @Autowired
DataSource dataSource;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
 System.out.println("begin Auth Process");
  auth.jdbcAuthentication().dataSource(dataSource)
    .usersByUsernameQuery(
        "select username,password, enabled from QSQL.users where username=?")
    .authoritiesByUsernameQuery(
        "select username, role from QSQL.user_roles where username=?");
}   

  @Override
 protected void configure(HttpSecurity http) throws Exception {

  http.authorizeRequests()
    .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
    .and()
      .formLogin().loginPage("/login").failureUrl("/login?error")
      .usernameParameter("username").passwordParameter("password")
    .and()
      .logout().logoutSuccessUrl("/login?logout")
    .and()
      .exceptionHandling().accessDeniedPage("/403")
    .and()
      .csrf();
 }
}

However I am getting the error mentioned above along with the following:

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.

Although I can connect to my database using the SQL explorer plugin.

Thanks for the help.

Upvotes: 0

Views: 2792

Answers (2)

nader
nader

Reputation: 141

The issue was in the mysql driver (mysql connector version 6.0.2) I was using once I used the latest GA'd version mysql java connector 5.1.39 the issue went away.

Upvotes: 1

Manoj Kumar
Manoj Kumar

Reputation: 380

Please check your library version of spring and Spring security you are using. java.lang.NoClassDefFoundError: org/springframework/security/authentication/AuthenticationManager

Upvotes: 0

Related Questions