質問編集履歴

2

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

2017/05/28 04:25

投稿

heavyuseman
heavyuseman

スコア42

test CHANGED
File without changes
test CHANGED
@@ -386,6 +386,10 @@
386
386
 
387
387
  //compile ("mysql:mysql-connector-java:$mySQLVersion")
388
388
 
389
+ //追記したMysqlのデータソース用
390
+
391
+ compile ('mysql:mysql-connector-java')
392
+
389
393
  compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8')
390
394
 
391
395
  compile("org.springframework.boot:spring-boot-starter-thymeleaf")

1

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

2017/05/28 04:25

投稿

heavyuseman
heavyuseman

スコア42

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,9 @@
6
6
 
7
7
  の箇所で下記のエラーが出ます。
8
8
 
9
- 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)
10
-
11
- 2017-05-26 23:38:04.921 ERROR 1197 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
9
+ 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)
10
+
11
+ 2017-05-28 10:11:42.921 ERROR 796 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
12
12
 
13
13
 
14
14
 
@@ -44,16 +44,130 @@
44
44
 
45
45
 
46
46
 
47
+ DBはMysqlを使用し、データソースがはApache Commons DBCPを使用しています。
48
+
49
+ JavaConfigでデータソースを定義しています。
50
+
47
- DataSourceはaplication.propertiesで設定しているのですがJavaconfigファイルなど他設定ファイルで設定しないといけないでしょうか?
51
+ 上記のエラーの原因はDBをMysql設定しているためデータソースをMysql用
52
+
48
-
53
+ 設定していないことが原因なのでしょうか?
54
+
55
+ ※ググったのですが、Mysql用のデータソースについての情報がなかったため
56
+
57
+ 困惑しております。
58
+
59
+ それとも他の原因がありますでしょうか?
60
+
49
- 下にソースを記載しました。ご回答よろしくお願いいたします。
61
+ 上です。ご回答しくお願いします。
50
62
 
51
63
  ```java
52
64
 
65
+ //Apache Commons DBCPのjavaconfig
66
+
53
67
  package com.tuyano.springboot.springsecurity;
54
68
 
55
69
 
56
70
 
71
+ import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
72
+
73
+
74
+
75
+
76
+
77
+ import javax.sql.DataSource;
78
+
79
+
80
+
81
+ import org.apache.commons.dbcp.BasicDataSource;
82
+
83
+ import org.springframework.beans.factory.annotation.Value;
84
+
85
+ import org.springframework.context.annotation.Bean;
86
+
87
+ import org.springframework.context.annotation.Configuration;
88
+
89
+ import org.springframework.context.annotation.PropertySource;
90
+
91
+ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
92
+
93
+ import org.springframework.core.io.ClassPathResource;
94
+
95
+ import org.springframework.core.io.FileSystemResource;
96
+
97
+ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
98
+
99
+
100
+
101
+ @Configuration
102
+
103
+ @PropertySource("application.properties")
104
+
105
+ public class DataSourceConfig {
106
+
107
+
108
+
109
+ @Value("${spring.datasource.driverClassName}")
110
+
111
+ private String driverName;
112
+
113
+ @Value("${spring.datasource.url}")
114
+
115
+ private String url;
116
+
117
+ @Value("${spring.datasource.username}")
118
+
119
+ private String userName;
120
+
121
+ @Value("${spring.datasource.password}")
122
+
123
+ private String password;
124
+
125
+
126
+
127
+ @Bean
128
+
129
+ public static PropertySourcesPlaceholderConfigurer propertyConfig() {
130
+
131
+ return new PropertySourcesPlaceholderConfigurer();
132
+
133
+ }
134
+
135
+
136
+
137
+ @Bean
138
+
139
+ public DataSource dataSource() {
140
+
141
+ BasicDataSource ds = new BasicDataSource();
142
+
143
+ ds.setDriverClassName(driverName);
144
+
145
+ ds.setUrl(url);
146
+
147
+ ds.setUsername(userName);
148
+
149
+ ds.setPassword(password);
150
+
151
+ return ds;
152
+
153
+ }
154
+
155
+
156
+
157
+ }
158
+
159
+
160
+
161
+ ```
162
+
163
+ ```java
164
+
165
+ package com.tuyano.springboot.springsecurity;
166
+
167
+
168
+
169
+ import org.springframework.context.annotation.Bean;
170
+
57
171
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
58
172
 
59
173
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -62,6 +176,8 @@
62
176
 
63
177
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
64
178
 
179
+ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
180
+
65
181
 
66
182
 
67
183
  import javax.sql.DataSource;
@@ -82,10 +198,10 @@
82
198
 
83
199
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
84
200
 
201
+ @Qualifier("authDataSource")
202
+
85
203
  @Autowired
86
204
 
87
- @Qualifier("authDataSource")
88
-
89
205
  private DataSource dataSource;
90
206
 
91
207
  @Override
@@ -134,7 +250,9 @@
134
250
 
135
251
  .usersByUsernameQuery(
136
252
 
137
- "select name, password from Newaccount where name = ?");
253
+ "select name, password from Newaccount where name = ?")
254
+
255
+ .passwordEncoder(new BCryptPasswordEncoder());
138
256
 
139
257
  //.authoritiesByUsernameQuery(
140
258
 
@@ -150,51 +268,133 @@
150
268
 
151
269
  ```
152
270
 
153
- ```java
271
+ ```xml
272
+
154
-
273
+ //Mysql用の設定ファイル
274
+
275
+ spring.jpa.database=MYSQL
276
+
277
+ spring.datasource.url=jdbc:mysql://localhost/mysql?useSSL=false
278
+
279
+ spring.datasource.name=authDataSource
280
+
281
+ spring.datasource.username=root
282
+
283
+ spring.datasource.password=root
284
+
155
- package com.tuyano.springboot.springsecurity;
285
+ spring.datasource.sqlScriptEncoding=UTF-8
156
-
157
-
158
-
286
+
159
- import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
287
+ spring.datasource.driverClassName=com.mysql.jdbc.Driver
160
-
161
-
162
-
163
-
164
-
165
- public class SecurityWebAppIicationInitializer extends AbstractSecurityWebApplicationInitializer {
288
+
166
-
167
- public SecurityWebAppIicationInitializer(){
289
+ security.user.role=USER,ADMIN
168
-
290
+
169
- super(SecurityConfig.class);
291
+ security.basic.enabled=false
170
-
171
-
172
-
173
- }
174
-
175
- }
176
-
177
-
178
292
 
179
293
  ```
180
294
 
295
+
296
+
297
+
298
+
181
- ```xml
299
+ ```Gradle
182
-
300
+
183
- spring.jpa.database=MYSQL
301
+ buildscript {
184
-
185
- spring.datasource.url=jdbc:mysql://localhost/mysql?useSSL=false
302
+
186
-
187
- spring.datasource.username=root
303
+ ext {
188
-
189
- spring.datasource.password=root
304
+
190
-
191
- spring.datasource.sqlScriptEncoding=UTF-8
305
+ springBootVersion = '1.5.1.RELEASE'
306
+
192
-
307
+ }
308
+
309
+ repositories {
310
+
311
+ mavenCentral()
312
+
313
+ }
314
+
315
+ dependencies {
316
+
317
+ classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
318
+
319
+ }
320
+
321
+ }
322
+
323
+
324
+
325
+ apply plugin: 'java'
326
+
327
+ apply plugin: 'eclipse-wtp'
328
+
193
- spring.datasource.driverClassName=com.mysql.jdbc.Driver
329
+ apply plugin: 'org.springframework.boot'
330
+
194
-
331
+ apply plugin: 'war'
332
+
333
+
334
+
335
+ war {
336
+
195
- security.user.role=USER,ADMIN
337
+ baseName = 'MybootApp'
338
+
196
-
339
+ version = '0.0.1-SNAPSHOT'
340
+
341
+ }
342
+
343
+
344
+
197
- security.basic.enabled=false
345
+ sourceCompatibility = 1.8
346
+
347
+
348
+
349
+ repositories {
350
+
351
+ mavenCentral()
352
+
353
+ }
354
+
355
+
356
+
357
+ configurations {
358
+
359
+ providedRuntime
360
+
361
+ }
362
+
363
+
364
+
365
+ dependencies {
366
+
367
+ //SpringSecurityを使うため、追加
368
+
369
+ compile('org.springframework.boot:spring-boot-starter-security')
370
+
371
+ compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
372
+
373
+ compile('org.springframework.boot:spring-boot-starter-data-jpa')
374
+
375
+ compile('org.springframework.boot:spring-boot-starter-thymeleaf')
376
+
377
+ compile('org.springframework.boot:spring-boot-starter-web')
378
+
379
+ runtime('mysql:mysql-connector-java')
380
+
381
+ providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
382
+
383
+ testCompile('org.springframework.boot:spring-boot-starter-test')
384
+
385
+ // MySQL
386
+
387
+ //compile ("mysql:mysql-connector-java:$mySQLVersion")
388
+
389
+ compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8')
390
+
391
+ compile("org.springframework.boot:spring-boot-starter-thymeleaf")
392
+
393
+ compile 'commons-dbcp:commons-dbcp:1.4'
394
+
395
+
396
+
397
+ }
198
398
 
199
399
 
200
400