いつもお世話になっております。
springsecurityでのDBの認証情報で質問があります。
springsecurityでのDBの認証情報を管理する機能を実装したのですが、DataSource
の箇所で下記のエラーが出ます。
2017-05-28 10:11:42.697 WARN 796 --- [ main] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-05-28 10:11:42.921 ERROR 796 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
Field dataSource in com.tuyano.springboot.springsecurity.SecurityConfig required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
DBはMysqlを使用し、データソースがはApache Commons DBCPを使用しています。
JavaConfigでデータソースを定義しています。
上記のエラーの原因はDBをMysqlに設定しているため、データソースをMysql用のものを
設定していないことが原因なのでしょうか?
※ググったのですが、Mysql用のデータソースについての情報がなかったため
困惑しております。
それとも他の原因がありますでしょうか?
以上です。ご回答宜しくお願い致します。
java
1//Apache Commons DBCPのjavaconfig 2package com.tuyano.springboot.springsecurity; 3 4import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 5 6 7import javax.sql.DataSource; 8 9import org.apache.commons.dbcp.BasicDataSource; 10import org.springframework.beans.factory.annotation.Value; 11import org.springframework.context.annotation.Bean; 12import org.springframework.context.annotation.Configuration; 13import org.springframework.context.annotation.PropertySource; 14import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 15import org.springframework.core.io.ClassPathResource; 16import org.springframework.core.io.FileSystemResource; 17import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 18 19@Configuration 20@PropertySource("application.properties") 21public class DataSourceConfig { 22 23 @Value("${spring.datasource.driverClassName}") 24 private String driverName; 25 @Value("${spring.datasource.url}") 26 private String url; 27 @Value("${spring.datasource.username}") 28 private String userName; 29 @Value("${spring.datasource.password}") 30 private String password; 31 32 @Bean 33 public static PropertySourcesPlaceholderConfigurer propertyConfig() { 34 return new PropertySourcesPlaceholderConfigurer(); 35 } 36 37 @Bean 38 public DataSource dataSource() { 39 BasicDataSource ds = new BasicDataSource(); 40 ds.setDriverClassName(driverName); 41 ds.setUrl(url); 42 ds.setUsername(userName); 43 ds.setPassword(password); 44 return ds; 45 } 46 47} 48
java
1package com.tuyano.springboot.springsecurity; 2 3import org.springframework.context.annotation.Bean; 4import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 9 10import javax.sql.DataSource; 11 12import org.springframework.beans.factory.annotation.Autowired; 13import org.springframework.beans.factory.annotation.Qualifier; 14 15 16 17@EnableWebSecurity 18public class SecurityConfig extends WebSecurityConfigurerAdapter{ 19 @Qualifier("authDataSource") 20 @Autowired 21 private DataSource dataSource; 22 @Override 23 protected void configure(HttpSecurity http) throws Exception{ 24 http 25 .authorizeRequests() 26 .antMatchers("/First.html").permitAll() 27 .and() 28 .formLogin() 29 .loginPage("/First.html") 30 .loginProcessingUrl("/pricessLogin") 31 .defaultSuccessUrl("/Member.html") 32 .failureUrl("/user.html") 33 .usernameParameter("name") 34 .passwordParameter("password") 35 .and() 36 .csrf() 37 .disable(); 38 39 } 40 @Override 41 public void configure(AuthenticationManagerBuilder auth) throws Exception { 42 auth.jdbcAuthentication() 43 .dataSource(dataSource) 44 .usersByUsernameQuery( 45 "select name, password from Newaccount where name = ?") 46 .passwordEncoder(new BCryptPasswordEncoder()); 47 //.authoritiesByUsernameQuery( 48 // "select mail_address, role from accounts where mail_address = ?"); 49 } 50 51 52}
xml
1//Mysql用の設定ファイル 2spring.jpa.database=MYSQL 3spring.datasource.url=jdbc:mysql://localhost/mysql?useSSL=false 4spring.datasource.name=authDataSource 5spring.datasource.username=root 6spring.datasource.password=root 7spring.datasource.sqlScriptEncoding=UTF-8 8spring.datasource.driverClassName=com.mysql.jdbc.Driver 9security.user.role=USER,ADMIN 10security.basic.enabled=false
Gradle
1buildscript { 2 ext { 3 springBootVersion = '1.5.1.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11} 12 13apply plugin: 'java' 14apply plugin: 'eclipse-wtp' 15apply plugin: 'org.springframework.boot' 16apply plugin: 'war' 17 18war { 19 baseName = 'MybootApp' 20 version = '0.0.1-SNAPSHOT' 21} 22 23sourceCompatibility = 1.8 24 25repositories { 26 mavenCentral() 27} 28 29configurations { 30 providedRuntime 31} 32 33dependencies { 34 //SpringSecurityを使うため、追加 35 compile('org.springframework.boot:spring-boot-starter-security') 36 compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4') 37 compile('org.springframework.boot:spring-boot-starter-data-jpa') 38 compile('org.springframework.boot:spring-boot-starter-thymeleaf') 39 compile('org.springframework.boot:spring-boot-starter-web') 40 runtime('mysql:mysql-connector-java') 41 providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 42 testCompile('org.springframework.boot:spring-boot-starter-test') 43 // MySQL 44 //compile ("mysql:mysql-connector-java:$mySQLVersion") 45 //追記したMysqlのデータソース用 46 compile ('mysql:mysql-connector-java') 47 compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8') 48 compile("org.springframework.boot:spring-boot-starter-thymeleaf") 49 compile 'commons-dbcp:commons-dbcp:1.4' 50 51} 52
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/28 01:50 編集
2017/05/28 02:21
2017/05/28 04:25 編集