0
我想用Spring配置服务器。我想同时使用Spring安全和JDBI。 所以我配置了我的服务器(?)的数据源并将其链接到JDBI。但是我无法在WebSecurityConfig中使用这个数据源。如何使用SpringSecurity和JDBI
这是我的主要配置的Java文件:
@SpringBootApplication
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {
private static DBI dbi = null;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
static DBI getDbi() {
if(dbi == null) {
DataSource ds = JdbcConnectionPool.create("jdbc:h2:mem:test", "ndl", "ndl");
dbi = new DBI(ds);
}
return dbi;
}
}
这是出于安全春
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login.html")
.permitAll()
.and()
.logout()
.permitAll();
http.csrf().disable();
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username,password from users where username=?")
.authoritiesByUsernameQuery(
"select username, role from users where username=?");
}
}
我得到这个错误的文件。
Field dataSource in rest.WebSecurityConfig required a bean of type 'javax.sql.DataSource' that could not be found.
我试图在类(而不是方法)中写入DataSource ds。并添加注释@Bean。但我得到了一个其他错误
public static DataSource ds = JdbcConnectionPool.create("jdbc:h2:mem:test", "ndl", "ndl");
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public static DataSource getDataSource(){
return ds;
}
和错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/core/support/JdbcDaoSupport
我希望你有什么想法? 感谢;)
什么是JDBI .. –
它的存在http://jdbi.org/getting_jdbi/? 这是一个工具,可以帮助你与数据库 –
我不需要这种工具。 但是我用Spring在其他项目中使用它,它很酷,所以我已经有很多可以轻松重用的代码。 但我有春季安全问题。 –