org.springframework.security.authentication.InternalAuthenticationServiceException:无法获取JDBC连接

时间:2016-06-25 23:06:27

标签: java hibernate spring-mvc spring-security

此刻我的弹簧安全问题(第4版)正面临问题。

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

这是AppConfig类:

 @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;
  }

}

这是我的SecurityConfig类:

@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();
 }
}

但是我收到上面提到的错误以及以下内容:

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

虽然我可以使用SQL explorer插件连接到我的数据库。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

问题出在mysql驱动程序(mysql连接器版本6.0.2)中,我使用的是最新的GA版本mysql java连接器5.1.39问题消失了。

答案 1 :(得分:0)

请检查您正在使用的弹簧和Spring安全库的库版本。 java.lang.NoClassDefFoundError: org/springframework/security/authentication/AuthenticationManager