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

标签 java hibernate spring-mvc spring-security

目前我的 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 资源管理器插件连接到我的数据库。

感谢您的帮助。

最佳答案

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

关于java - org.springframework.security.authentication.InternalAuthenticationServiceException : Could not get JDBC Connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033711/

相关文章:

java - 如何使用属性连接到多实例 MQSeries 队列管理器?

java - 在 Java 中避免 RTTI

java - PSQL异常 : ERROR: operator does not exist: character varying = uuid

java - 如何将多部分文件转换为文件?

java - Spring 3.1 Controller 返回 HTML 页面的名称 - 它可以工作吗?

java - 如何使用 Java 拆分字符串?

java - 如何将选定行的值从表查看器传递到 SWT 中的文本框

java - 合并和延迟加载

java - 如何处理: SQL Error: 0, SQLState : 08006 and 08003?

Java Spring View 格式化货币