目前我的 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/