提问者:小点点

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


我现在正面临着我的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个答案

匿名用户

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

匿名用户

请检查您正在使用的Spring和Spring Security性的库版本。 java.lang.NoClassDefFoundError: org/springFramework/Security/身份验证/AuthenticationManager