質問編集履歴

2

SecurityConfig\.javaを修正、gradleのソース追加、認証・認可用のDB追加

2017/06/04 06:10

投稿

heavyuseman
heavyuseman

スコア42

test CHANGED
File without changes
test CHANGED
@@ -20,9 +20,7 @@
20
20
 
21
21
  原因が不明ですのでご回答宜しくお願い致します
22
22
 
23
-
24
-
25
- -20170602追記
23
+ -20170605追記
26
24
 
27
25
  SecurityConfig.javaの箇所でブレークポイントし、デバックしたところ
28
26
 
@@ -44,18 +42,236 @@
44
42
 
45
43
  原因が不明ですのでご回答宜しくお願い致します
46
44
 
45
+
46
+
47
47
  ```java
48
48
 
49
- //SecurityConfig.java
49
+ //Datasource.java
50
-
51
-
52
50
 
53
51
  package com.tuyano.springboot.springsecurity;
54
52
 
55
53
 
56
54
 
55
+ import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
56
+
57
+ import javax.sql.DataSource;
58
+
59
+ import org.apache.commons.dbcp.BasicDataSource;
60
+
61
+ import org.springframework.beans.factory.annotation.Value;
62
+
57
63
  import org.springframework.context.annotation.Bean;
58
64
 
65
+ import org.springframework.context.annotation.Configuration;
66
+
67
+ import org.springframework.context.annotation.PropertySource;
68
+
69
+ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
70
+
71
+ import org.springframework.core.io.ClassPathResource;
72
+
73
+ import org.springframework.core.io.FileSystemResource;
74
+
75
+ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
76
+
77
+ @Configuration
78
+
79
+ @PropertySource("application.properties")
80
+
81
+ public class DataSourceConfig {
82
+
83
+
84
+
85
+ @Value("${spring.datasource.driverClassName}")
86
+
87
+ private String driverClassName;
88
+
89
+ @Value("${spring.datasource.url}")
90
+
91
+ private String url;
92
+
93
+ @Value("${spring.datasource.username}")
94
+
95
+ private String userName;
96
+
97
+ @Value("${spring.datasource.password}")
98
+
99
+ private String password;
100
+
101
+
102
+
103
+ @Bean
104
+
105
+ public static PropertySourcesPlaceholderConfigurer propertyConfig() {
106
+
107
+ return new PropertySourcesPlaceholderConfigurer();
108
+
109
+ }
110
+
111
+ @Bean(destroyMethod="close")
112
+
113
+ public DataSource dataSource() {
114
+
115
+
116
+
117
+ BasicDataSource ds = new BasicDataSource() {
118
+
119
+ @Override
120
+
121
+ public String toString() {
122
+
123
+ return "[driverClassName=" + driverClassName + ",username=" + userName + ", password=" + password+ "]";
124
+
125
+ }
126
+
127
+ };
128
+
129
+ ds.setDriverClassName(driverClassName);
130
+
131
+ ds.setUrl(url);
132
+
133
+ ds.setUsername(userName);
134
+
135
+ ds.setPassword(password);
136
+
137
+ System.out.println("dsの値"+ds);
138
+
139
+ return ds;
140
+
141
+
142
+
143
+ }
144
+
145
+ ```
146
+
147
+
148
+
149
+ ```HTML
150
+
151
+ //First.html
152
+
153
+ <!DOCTYPE html>
154
+
155
+ <html xmlns:th="http://www.thymeleaf.org">
156
+
157
+ <head>
158
+
159
+ <title>top page</title>
160
+
161
+ <meta http-equiv="Content-Type"
162
+
163
+ content="text/html" charset="UTF-8"/>
164
+
165
+ </head>
166
+
167
+ <body>
168
+
169
+ <form action="/processLogin" >
170
+
171
+ <dl>
172
+
173
+ <dt>
174
+
175
+ ログイン名前
176
+
177
+ </dt>
178
+
179
+ <dd>
180
+
181
+ <input type="text" name="name"></input>
182
+
183
+ </dd>
184
+
185
+ <dt>
186
+
187
+ ログインパスワード
188
+
189
+ </dt>
190
+
191
+ <dd>
192
+
193
+ <input type="password" name="password"></input>
194
+
195
+ </dd>
196
+
197
+ </dl>
198
+
199
+ <button>ログイン</button>
200
+
201
+ </form>
202
+
203
+ <hr/>
204
+
205
+ <form action="urlForUpload" enctype="multipart/form-data" method="post">
206
+
207
+ <div class="form-group">
208
+
209
+ <label>■ファイル種類:</label>
210
+
211
+ <select id="select_file_type" name="select_file_type" required="">
212
+
213
+ <option value="login-user">ログインユーザー</option>
214
+
215
+ <!-- アップロードするファイルを定義していく -->
216
+
217
+ </select>
218
+
219
+ </div>
220
+
221
+ <div class="form-group">
222
+
223
+ <label>■ファイルパス:</label>
224
+
225
+ <input type="file" id="upload_file" name="upload_file" required="" />
226
+
227
+ </div>
228
+
229
+ <div class="form-group">
230
+
231
+ <input id="data_upload_button" type="submit" value="アップロード" />
232
+
233
+ </div>
234
+
235
+ </form>
236
+
237
+ <table>
238
+
239
+ <tr><th>名前</th><th>住所</th></tr>
240
+
241
+ <tr th:each="obj :${datalist}">
242
+
243
+ <td th:text="${obj.name}"></td>
244
+
245
+
246
+
247
+ </tr>
248
+
249
+ </table>
250
+
251
+ <div id ="map"></div>
252
+
253
+ <script type="text/javascript" src="js/main.js"
254
+
255
+
256
+
257
+ </body>
258
+
259
+
260
+
261
+ </html>
262
+
263
+ ```
264
+
265
+ ```java
266
+
267
+ //Security.config
268
+
269
+ package com.tuyano.springboot.springsecurity;
270
+
271
+
272
+
273
+ import org.springframework.context.annotation.Bean;
274
+
59
275
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
60
276
 
61
277
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -78,6 +294,10 @@
78
294
 
79
295
 
80
296
 
297
+
298
+
299
+
300
+
81
301
  @EnableWebSecurity
82
302
 
83
303
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
@@ -88,13 +308,11 @@
88
308
 
89
309
 
90
310
 
91
- private static final String USER_QUERY
311
+ private static final String USER_QUERY="select name, password, 1 from Newaccount where name = ?";
92
-
312
+
93
- ="select name, password, 1"
313
+ private static final String ROLES_QUERY="select username, authority from AUTHORITIES where username = ?";
94
-
95
- +"from Newaccount"
314
+
96
-
97
- + "where name = ?";
315
+
98
316
 
99
317
  @Override
100
318
 
@@ -106,17 +324,21 @@
106
324
 
107
325
  .antMatchers("/First.html").permitAll()
108
326
 
327
+ .antMatchers("/templates/**").hasAnyAuthority("ROLE_ADMIN")
328
+
329
+ .anyRequest().authenticated()
330
+
109
331
  .and()
110
332
 
111
333
  .formLogin()
112
334
 
113
- .loginPage("/First.html")
335
+ .loginPage("/First.html")
114
336
 
115
337
  .loginProcessingUrl("/processLogin")
116
338
 
117
- .defaultSuccessUrl("/First.html")
339
+ .defaultSuccessUrl("/sucess.html")
118
-
340
+
119
- .failureUrl("/user.html")
341
+ .failureUrl("/faliure.html")
120
342
 
121
343
  .usernameParameter("name")
122
344
 
@@ -142,7 +364,9 @@
142
364
 
143
365
  .usersByUsernameQuery(USER_QUERY)
144
366
 
367
+ .authoritiesByUsernameQuery(ROLES_QUERY);
368
+
145
- .passwordEncoder(new BCryptPasswordEncoder());
369
+ //passwordEncoder(new BCryptPasswordEncoder());
146
370
 
147
371
  //.authoritiesByUsernameQuery(
148
372
 
@@ -150,348 +374,130 @@
150
374
 
151
375
  }
152
376
 
377
+
378
+
379
+
380
+
153
381
  }
154
382
 
155
383
  ```
156
384
 
157
- ```java
158
-
159
- //Datasource.java
160
-
161
- package com.tuyano.springboot.springsecurity;
162
-
163
-
164
-
165
- import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
166
-
167
- import javax.sql.DataSource;
168
-
169
- import org.apache.commons.dbcp.BasicDataSource;
170
-
171
- import org.springframework.beans.factory.annotation.Value;
172
-
173
- import org.springframework.context.annotation.Bean;
174
-
175
- import org.springframework.context.annotation.Configuration;
176
-
177
- import org.springframework.context.annotation.PropertySource;
178
-
179
- import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
180
-
181
- import org.springframework.core.io.ClassPathResource;
182
-
183
- import org.springframework.core.io.FileSystemResource;
184
-
185
- import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
186
-
187
- @Configuration
188
-
189
- @PropertySource("application.properties")
190
-
191
- public class DataSourceConfig {
192
-
193
-
194
-
195
- @Value("${spring.datasource.driverClassName}")
196
-
197
- private String driverClassName;
198
-
199
- @Value("${spring.datasource.url}")
200
-
201
- private String url;
202
-
203
- @Value("${spring.datasource.username}")
204
-
205
- private String userName;
206
-
207
- @Value("${spring.datasource.password}")
208
-
209
- private String password;
210
-
211
-
212
-
213
- @Bean
214
-
215
- public static PropertySourcesPlaceholderConfigurer propertyConfig() {
216
-
217
- return new PropertySourcesPlaceholderConfigurer();
218
-
219
- }
220
-
221
- @Bean(destroyMethod="close")
222
-
223
- public DataSource dataSource() {
224
-
225
-
226
-
227
- BasicDataSource ds = new BasicDataSource() {
228
-
229
- @Override
230
-
231
- public String toString() {
232
-
233
- return "[driverClassName=" + driverClassName + ",username=" + userName + ", password=" + password+ "]";
234
-
235
- }
236
-
237
- };
238
-
239
- ds.setDriverClassName(driverClassName);
240
-
241
- ds.setUrl(url);
242
-
243
- ds.setUsername(userName);
244
-
245
- ds.setPassword(password);
246
-
247
- System.out.println("ds値"+ds);
248
-
249
- return ds;
250
-
251
-
252
-
253
- }
385
+ ```gradle
386
+
387
+ buildscript {
388
+
389
+ ext {
390
+
391
+ springBootVersion = '1.5.1.RELEASE'
392
+
393
+ }
394
+
395
+ repositories {
396
+
397
+ mavenCentral()
398
+
399
+ }
400
+
401
+ dependencies {
402
+
403
+ classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
404
+
405
+ }
406
+
407
+ }
408
+
409
+
410
+
411
+ apply plugin: 'java'
412
+
413
+ apply plugin: 'eclipse-wtp'
414
+
415
+ apply plugin: 'org.springframework.boot'
416
+
417
+ apply plugin: 'war'
418
+
419
+
420
+
421
+ war {
422
+
423
+ baseName = 'MybootApp'
424
+
425
+ version = '0.0.1-SNAPSHOT'
426
+
427
+ }
428
+
429
+
430
+
431
+ sourceCompatibility = 1.8
432
+
433
+
434
+
435
+ repositories {
436
+
437
+ mavenCentral()
438
+
439
+ }
440
+
441
+
442
+
443
+ configurations {
444
+
445
+ providedRuntime
446
+
447
+ }
448
+
449
+
450
+
451
+ dependencies {
452
+
453
+ //SpringSecurityを使うため、追加
454
+
455
+ compile('org.springframework.boot:spring-boot-starter-security')
456
+
457
+ compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
458
+
459
+ compile('org.springframework.boot:spring-boot-starter-data-jpa')
460
+
461
+ compile('org.springframework.boot:spring-boot-starter-thymeleaf')
462
+
463
+ compile('org.springframework.boot:spring-boot-starter-web')
464
+
465
+ runtime('mysql:mysql-connector-java')
466
+
467
+ providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
468
+
469
+ testCompile('org.springframework.boot:spring-boot-starter-test')
470
+
471
+ // MySQL
472
+
473
+ //compile ("mysql:mysql-connector-java:$mySQLVersion")
474
+
475
+ //追記したMysqlのデータソース用
476
+
477
+ compile ('mysql:mysql-connector-java')
478
+
479
+ compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8')
480
+
481
+ compile("org.springframework.boot:spring-boot-starter-thymeleaf")
482
+
483
+ compile 'commons-dbcp:commons-dbcp:1.4'
484
+
485
+
486
+
487
+ }
488
+
489
+
254
490
 
255
491
  ```
256
492
 
257
- ```java
258
-
259
- //Test.java
260
-
261
- package com.tuyano.springboot.app;
262
-
263
-
264
-
265
- import java.util.List;
266
-
267
-
268
-
269
- import org.springframework.beans.factory.annotation.Autowired;
270
-
271
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
272
-
273
- import org.springframework.http.MediaType;
274
-
275
- import org.springframework.stereotype.Controller;
276
-
277
- import org.springframework.transaction.annotation.Transactional;
278
-
279
- import org.springframework.ui.Model;
280
-
281
- import org.springframework.web.bind.annotation.ModelAttribute;
282
-
283
- import org.springframework.web.bind.annotation.RequestBody;
284
-
285
- import org.springframework.web.bind.annotation.RequestMapping;
286
-
287
- import org.springframework.web.bind.annotation.RequestMethod;
288
-
289
- import org.springframework.web.bind.annotation.RequestParam;
290
-
291
- import org.springframework.web.bind.annotation.ResponseBody;
292
-
293
- import org.springframework.web.servlet.ModelAndView;
294
-
295
- import com.fasterxml.jackson.databind.SerializationFeature;
296
-
297
- import com.fasterxml.jackson.core.JsonProcessingException;
298
-
299
- import com.fasterxml.jackson.databind.ObjectMapper;
300
-
301
- import com.tuyano.springboot.app.Emp;
302
-
303
- import com.tuyano.springboot.app.EmpRepository;
304
-
305
- import com.tuyano.springboot.dateaccess.DataAccessRepository;
306
-
307
- import com.tuyano.springboot.dateaccess.Newaccount;
308
-
309
-
310
-
311
- @Controller
312
-
313
- @EnableAutoConfiguration
314
-
315
- public class Test {
316
-
317
- @Autowired //リポジトリを紐づけます
318
-
319
- EmpRepository repository;
320
-
321
- @Autowired //リポジトリを紐づけます
322
-
323
- DataAccessRepository dataAccessRepository;
324
-
325
-
326
-
327
- //firstGet画面呼び出し
328
-
329
- @RequestMapping(value="/First",method=RequestMethod.GET)
330
-
331
- public String FirstGet(){
332
-
333
-
334
-
335
- return null;
336
-
337
- }
338
-
339
- //firstPost画面呼び出し
340
-
341
- @RequestMapping(value="/First",method=RequestMethod.POST)
342
-
343
- public String FirstPost(){
344
-
345
-
346
-
347
- return null;
348
-
349
- }
350
-
351
- //NewAccountGet画面呼び出し
352
-
353
- @RequestMapping(value="/NewAccount",method=RequestMethod.GET)
354
-
355
- public String NewAccountGet(){
356
-
357
-
358
-
359
- return null;
360
-
361
- }
362
-
363
- //NewAccountPost画面呼び出し
364
-
365
- @RequestMapping(value="/NewAccount",method=RequestMethod.POST)
366
-
367
- @Transactional(readOnly=false)
368
-
369
- public ModelAndView NewAccountPost(@ModelAttribute("formModel") Newaccount newaccount,ModelAndView mav){
370
-
371
- System.out.println("newaccountは"+newaccount);
372
-
373
- dataAccessRepository.saveAndFlush(newaccount);
374
-
375
- return new ModelAndView("redirect:/NewAccount");
376
-
377
- }
378
-
379
- }
380
-
381
- ```
382
-
383
- ```HTML
384
-
385
- //First.html
386
-
387
- <!DOCTYPE html>
388
-
389
- <html xmlns:th="http://www.thymeleaf.org">
390
-
391
- <head>
392
-
393
- <title>top page</title>
394
-
395
- <meta http-equiv="Content-Type"
396
-
397
- content="text/html" charset="UTF-8"/>
398
-
399
- </head>
400
-
401
- <body>
402
-
403
- <form action="/processLogin" >
404
-
405
- <dl>
406
-
407
- <dt>
408
-
409
- ログイン名前
410
-
411
- </dt>
412
-
413
- <dd>
414
-
415
- <input type="text" name="name"></input>
416
-
417
- </dd>
418
-
419
- <dt>
420
-
421
- ログインパスワード
422
-
423
- </dt>
424
-
425
- <dd>
426
-
427
- <input type="password" name="password"></input>
428
-
429
- </dd>
430
-
431
- </dl>
432
-
433
- <button>ログイン</button>
434
-
435
- </form>
436
-
437
- <hr/>
438
-
439
- <form action="urlForUpload" enctype="multipart/form-data" method="post">
440
-
441
- <div class="form-group">
442
-
443
- <label>■ファイル種類:</label>
444
-
445
- <select id="select_file_type" name="select_file_type" required="">
446
-
447
- <option value="login-user">ログインユーザー</option>
448
-
449
- <!-- アップロードするファイルを定義していく -->
450
-
451
- </select>
452
-
453
- </div>
454
-
455
- <div class="form-group">
456
-
457
- <label>■ファイルパス:</label>
458
-
459
- <input type="file" id="upload_file" name="upload_file" required="" />
460
-
461
- </div>
462
-
463
- <div class="form-group">
464
-
465
- <input id="data_upload_button" type="submit" value="アップロード" />
466
-
467
- </div>
468
-
469
- </form>
470
-
471
- <table>
472
-
473
- <tr><th>名前</th><th>住所</th></tr>
474
-
475
- <tr th:each="obj :${datalist}">
476
-
477
- <td th:text="${obj.name}"></td>
478
-
479
-
480
-
481
- </tr>
482
-
483
- </table>
484
-
485
- <div id ="map"></div>
486
-
487
- <script type="text/javascript" src="js/main.js"
488
-
489
-
490
-
491
- </body>
492
-
493
-
494
-
495
- </html>
496
-
497
- ```
493
+
494
+
495
+ NewaccountのDB(認証用DB)
496
+
497
+ ![イメージ説明](40ca56ecaf968a7b567f93e3b265ba80.png)
498
+
499
+
500
+
501
+ AUTHORITIESのDB(認可用DB)
502
+
503
+ ![![イメージ説明](cd05ba45ef1a3589a489d8b3c88b2096.png)

1

Datasourceのデバック結果を追記しました

2017/06/04 06:10

投稿

heavyuseman
heavyuseman

スコア42

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,30 @@
20
20
 
21
21
  原因が不明ですのでご回答宜しくお願い致します
22
22
 
23
+
24
+
25
+ -20170602追記
26
+
27
+ SecurityConfig.javaの箇所でブレークポイントし、デバックしたところ
28
+
29
+ Source not foundeエラーが出ました。
30
+
31
+ 内容としましては
32
+
33
+ Spring-security-config-4-2-1RELEASE.jarファイルが
34
+
35
+ no source attachment
36
+
37
+ と出てきました。
38
+
39
+ gradleでSpring-security-config-4-2-1RELEASE.jarファイルを
40
+
41
+ 入れるように設定しているため、設定しているはずなのですが
42
+
43
+ エラーが出てくるのが何故なのか不明です。
44
+
45
+ 原因が不明ですのでご回答宜しくお願い致します
46
+
23
47
  ```java
24
48
 
25
49
  //SecurityConfig.java
@@ -466,148 +490,8 @@
466
490
 
467
491
  </body>
468
492
 
493
+
494
+
469
495
  </html>
470
496
 
471
497
  ```
472
-
473
- ```JAVA
474
-
475
- //Newaccount.java
476
-
477
- package com.tuyano.springboot.dateaccess;
478
-
479
-
480
-
481
- import javax.persistence.Column;
482
-
483
- import javax.persistence.Entity;
484
-
485
- import javax.persistence.GeneratedValue;
486
-
487
- import javax.persistence.Id;
488
-
489
-
490
-
491
-
492
-
493
- @Entity// このクラスはEntityとして登録しますよ、とspringに教えてます
494
-
495
- public class Newaccount {
496
-
497
-
498
-
499
- @Id// プライマリーキーのものに設定してください
500
-
501
- @GeneratedValue// 主に数字に対して、順番に一意に設定しますよ、の意味
502
-
503
- private Integer ID;
504
-
505
-
506
-
507
- @Column// ただの変数じゃなくて、DBのカラムだよ、の意味
508
-
509
- private String name;
510
-
511
- @Column//
512
-
513
- private int password;
514
-
515
- @Column//
516
-
517
- private String address;
518
-
519
- // setter & getter ---------------------
520
-
521
- public Integer getID() {
522
-
523
- return ID;
524
-
525
- }
526
-
527
- public String getName(){
528
-
529
- return name;
530
-
531
- }
532
-
533
- public int getPassword(){
534
-
535
- return password;
536
-
537
- }
538
-
539
- public String getAddress(){
540
-
541
- return address;
542
-
543
- }
544
-
545
-
546
-
547
- public void setEmpID(Integer ID) {
548
-
549
- this.ID = ID;
550
-
551
- }
552
-
553
-
554
-
555
- public void setName(String name) {
556
-
557
- this.name = name;
558
-
559
- }
560
-
561
-
562
-
563
- public void setPassword(int password) {
564
-
565
- this.password = password;
566
-
567
- }
568
-
569
-
570
-
571
- public void setAddress(String address) {
572
-
573
- this.address = address;
574
-
575
- }
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
- // constructor --------------------------
584
-
585
- public Newaccount(){
586
-
587
- super();
588
-
589
- }
590
-
591
-
592
-
593
- public Newaccount(Integer ID, String name,int password,String address){
594
-
595
- super();
596
-
597
- this.ID =ID;
598
-
599
- this.name = name;
600
-
601
- this.password = password;
602
-
603
- this.address = address;
604
-
605
- }
606
-
607
-
608
-
609
- }
610
-
611
-
612
-
613
- ```