![](/img/trans.png)
[英]error:org.springframework.security.authentication.InternalAuthenticationServiceException
[英]org.springframework.security.authentication.InternalAuthenticationServiceException: Could not get JDBC Connection
目前,我的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后,问题就消失了。
请检查您的Spring和Spring Security的库版本。 java.lang.NoClassDefFoundError:org / springframework / security / authentication / AuthenticationManager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.