2017-11-17 117 views
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 

我希望你有什麼想法? 感謝;)

+0

什麼是JDBI .. –

+1

它的存在http://jdbi.org/getting_jdbi/? 這是一個工具,可以幫助你與數據庫 –

+1

我不需要這種工具。 但是我用Spring在其他項目中使用它,它很酷,所以我已經有很多可以輕鬆重用的代碼。 但我有春季安全問題。 –

回答

0

發現問題。

我錯過以下依賴性:彈簧JDBC

所以我最後的build.gradle是

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'org.springframework.boot' 

jar { 
    baseName = 'gs-rest-service' 
    version = '0.1.0' 
} 

repositories { 
    mavenCentral() 
    maven { 
     url 'https://repo.spring.io/libs-milestone' 
    } 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    compile("org.springframework.boot:spring-boot-starter-security") 
    testCompile("org.springframework.security:spring-security-test") 
    compile group: 'org.jdbi', name: 'jdbi', version: '2.4.2' 
    compile group: 'com.h2database', name: 'h2', version: '1.3.148' 
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.8.RELEASE' 
}