2017-11-17 11 views
0

私はSpringを使ってサーバを設定しようとしています。私は、SpringセキュリティとJDBIを同時に使用したいと思っています。 私は自分のサーバー(?)のデータソースを設定し、それをJDBIにリンクしました。しかし、WebSecurityConfigでこのデータソースを使用することはできません。SpringSecurityとJDBIの使い方

これは私のメインのconfig javaファイルである:これは私はこのエラーを得たセキュリティバネ

@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=?"); 
    } 

} 

ためのファイルである

@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; 
     } 
    } 

Field dataSource in rest.WebSecurityConfig required a bean of type 'javax.sql.DataSource' that could not be found. 

私はDataSource dsをクラスに記述しようとしましたが、メソッドには記述しませんでした。それに注釈@Beanを追加します。見つかっ)

+0

あるJDBIは何..です –

+1

それはそこhttp://jdbi.org/getting_jdbi/ですか?。 データベースを手助けするツールです –

+1

私はこの種のツールは必要ありません。 しかし、私は春に他のプロジェクトで使ったことがあり、それはクールだったので、私は簡単に再利用できるコードをたくさん持っています。 しかし、私は春のセキュリティに問題があります。 –

答えて

0

問題を、しかし、私は おかげで...他のエラー

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 

を得ました。

私は、次の依存関係を逃した:春-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' 
} 
関連する問題