teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

Gradleのソースを編集しました

2017/05/28 04:25

投稿

heavyuseman
heavyuseman

スコア42

title CHANGED
File without changes
body CHANGED
@@ -192,6 +192,8 @@
192
192
  testCompile('org.springframework.boot:spring-boot-starter-test')
193
193
  // MySQL
194
194
  //compile ("mysql:mysql-connector-java:$mySQLVersion")
195
+ //追記したMysqlのデータソース用
196
+ compile ('mysql:mysql-connector-java')
195
197
  compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8')
196
198
  compile("org.springframework.boot:spring-boot-starter-thymeleaf")
197
199
  compile 'commons-dbcp:commons-dbcp:1.4'

1

データソースのjavaconfigについてのソースを追記しました

2017/05/28 04:25

投稿

heavyuseman
heavyuseman

スコア42

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,8 @@
2
2
  springsecurityでのDBの認証情報で質問があります。
3
3
  springsecurityでのDBの認証情報を管理する機能を実装したのですが、DataSource
4
4
  の箇所で下記のエラーが出ます。
5
- 017-05-26 23:38:04.745 WARN 1197 --- [ 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)
6
- 2017-05-26 23:38:04.921 ERROR 1197 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
5
+ 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)
6
+ 2017-05-28 10:11:42.921 ERROR 796 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
7
7
 
8
8
  ***************************
9
9
  APPLICATION FAILED TO START
@@ -21,15 +21,73 @@
21
21
 
22
22
  Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
23
23
 
24
+ DBはMysqlを使用し、データソースがはApache Commons DBCPを使用しています。
25
+ JavaConfigでデータソースを定義しています。
24
- DataSourceaplication.propertiesで設定しているのですがJavaconfigファイルなど他設定ファイルで設定しないといけないでしょうか?
26
+ 上記のエラーの原因DBをMysqlに設定しているためデータソースをMysql用
27
+ 設定していないことが原因なのでしょうか?
28
+ ※ググったのですが、Mysql用のデータソースについての情報がなかったため
29
+ 困惑しております。
30
+ それとも他の原因がありますでしょうか?
25
- 下にソースを記載しました。ご回答よろしくお願いいたします。
31
+ 上です。ご回答しくお願いします。
26
32
  ```java
33
+ //Apache Commons DBCPのjavaconfig
27
34
  package com.tuyano.springboot.springsecurity;
28
35
 
36
+ import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
37
+
38
+
39
+ import javax.sql.DataSource;
40
+
41
+ import org.apache.commons.dbcp.BasicDataSource;
42
+ import org.springframework.beans.factory.annotation.Value;
43
+ import org.springframework.context.annotation.Bean;
44
+ import org.springframework.context.annotation.Configuration;
45
+ import org.springframework.context.annotation.PropertySource;
46
+ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
47
+ import org.springframework.core.io.ClassPathResource;
48
+ import org.springframework.core.io.FileSystemResource;
49
+ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
50
+
51
+ @Configuration
52
+ @PropertySource("application.properties")
53
+ public class DataSourceConfig {
54
+
55
+ @Value("${spring.datasource.driverClassName}")
56
+ private String driverName;
57
+ @Value("${spring.datasource.url}")
58
+ private String url;
59
+ @Value("${spring.datasource.username}")
60
+ private String userName;
61
+ @Value("${spring.datasource.password}")
62
+ private String password;
63
+
64
+ @Bean
65
+ public static PropertySourcesPlaceholderConfigurer propertyConfig() {
66
+ return new PropertySourcesPlaceholderConfigurer();
67
+ }
68
+
69
+ @Bean
70
+ public DataSource dataSource() {
71
+ BasicDataSource ds = new BasicDataSource();
72
+ ds.setDriverClassName(driverName);
73
+ ds.setUrl(url);
74
+ ds.setUsername(userName);
75
+ ds.setPassword(password);
76
+ return ds;
77
+ }
78
+
79
+ }
80
+
81
+ ```
82
+ ```java
83
+ package com.tuyano.springboot.springsecurity;
84
+
85
+ import org.springframework.context.annotation.Bean;
29
86
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
30
87
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
31
88
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
32
89
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
90
+ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
33
91
 
34
92
  import javax.sql.DataSource;
35
93
 
@@ -40,8 +98,8 @@
40
98
 
41
99
  @EnableWebSecurity
42
100
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
101
+ @Qualifier("authDataSource")
43
102
  @Autowired
44
- @Qualifier("authDataSource")
45
103
  private DataSource dataSource;
46
104
  @Override
47
105
  protected void configure(HttpSecurity http) throws Exception{
@@ -66,7 +124,8 @@
66
124
  auth.jdbcAuthentication()
67
125
  .dataSource(dataSource)
68
126
  .usersByUsernameQuery(
69
- "select name, password from Newaccount where name = ?");
127
+ "select name, password from Newaccount where name = ?")
128
+ .passwordEncoder(new BCryptPasswordEncoder());
70
129
  //.authoritiesByUsernameQuery(
71
130
  // "select mail_address, role from accounts where mail_address = ?");
72
131
  }
@@ -74,28 +133,69 @@
74
133
 
75
134
  }
76
135
  ```
77
- ```java
78
- package com.tuyano.springboot.springsecurity;
79
-
80
- import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
81
-
82
-
83
- public class SecurityWebAppIicationInitializer extends AbstractSecurityWebApplicationInitializer {
84
- public SecurityWebAppIicationInitializer(){
85
- super(SecurityConfig.class);
86
-
87
- }
88
- }
89
-
90
- ```
91
136
  ```xml
137
+ //Mysql用の設定ファイル
92
138
  spring.jpa.database=MYSQL
93
139
  spring.datasource.url=jdbc:mysql://localhost/mysql?useSSL=false
140
+ spring.datasource.name=authDataSource
94
141
  spring.datasource.username=root
95
142
  spring.datasource.password=root
96
143
  spring.datasource.sqlScriptEncoding=UTF-8
97
144
  spring.datasource.driverClassName=com.mysql.jdbc.Driver
98
145
  security.user.role=USER,ADMIN
99
146
  security.basic.enabled=false
147
+ ```
100
148
 
149
+
150
+ ```Gradle
151
+ buildscript {
152
+ ext {
153
+ springBootVersion = '1.5.1.RELEASE'
154
+ }
155
+ repositories {
156
+ mavenCentral()
157
+ }
158
+ dependencies {
159
+ classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
160
+ }
161
+ }
162
+
163
+ apply plugin: 'java'
164
+ apply plugin: 'eclipse-wtp'
165
+ apply plugin: 'org.springframework.boot'
166
+ apply plugin: 'war'
167
+
168
+ war {
169
+ baseName = 'MybootApp'
170
+ version = '0.0.1-SNAPSHOT'
171
+ }
172
+
173
+ sourceCompatibility = 1.8
174
+
175
+ repositories {
176
+ mavenCentral()
177
+ }
178
+
179
+ configurations {
180
+ providedRuntime
181
+ }
182
+
183
+ dependencies {
184
+ //SpringSecurityを使うため、追加
185
+ compile('org.springframework.boot:spring-boot-starter-security')
186
+ compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
187
+ compile('org.springframework.boot:spring-boot-starter-data-jpa')
188
+ compile('org.springframework.boot:spring-boot-starter-thymeleaf')
189
+ compile('org.springframework.boot:spring-boot-starter-web')
190
+ runtime('mysql:mysql-connector-java')
191
+ providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
192
+ testCompile('org.springframework.boot:spring-boot-starter-test')
193
+ // MySQL
194
+ //compile ("mysql:mysql-connector-java:$mySQLVersion")
195
+ compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8')
196
+ compile("org.springframework.boot:spring-boot-starter-thymeleaf")
197
+ compile 'commons-dbcp:commons-dbcp:1.4'
198
+
199
+ }
200
+
101
201
  ```