質問編集履歴
1
import文の追加と文字数制限に伴うTop.controller,html文の移動
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Spring Security】emailとpasswordでログイン認証を行う
|
1
|
+
【Spring Security】emailとpasswordでログイン認証を行うpart1
|
test
CHANGED
@@ -16,6 +16,24 @@
|
|
16
16
|
|
17
17
|
```Account
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
package com.example.demo.entity;
|
22
|
+
|
23
|
+
import org.springframework.security.core.GrantedAuthority;
|
24
|
+
|
25
|
+
import org.springframework.security.core.userdetails.UserDetails;
|
26
|
+
|
27
|
+
import javax.persistence.*;
|
28
|
+
|
29
|
+
import java.sql.Timestamp;
|
30
|
+
|
31
|
+
import java.util.Collection;
|
32
|
+
|
33
|
+
import java.util.Date;
|
34
|
+
|
35
|
+
|
36
|
+
|
19
37
|
@Entity
|
20
38
|
|
21
39
|
@Table(name = "accounts")
|
@@ -208,6 +226,22 @@
|
|
208
226
|
|
209
227
|
```AccountRepository
|
210
228
|
|
229
|
+
package com.example.demo.entity;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
import org.springframework.data.jpa.repository.JpaRepository;
|
234
|
+
|
235
|
+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
236
|
+
|
237
|
+
import org.springframework.data.jpa.repository.Query;
|
238
|
+
|
239
|
+
import org.springframework.data.repository.query.Param;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
import java.util.List;
|
244
|
+
|
211
245
|
public interface AccountRepository extends JpaRepository<Account, Integer>, JpaSpecificationExecutor<Account> {
|
212
246
|
|
213
247
|
|
@@ -262,6 +296,36 @@
|
|
262
296
|
|
263
297
|
```WebSecurityConfig
|
264
298
|
|
299
|
+
package com.example.demo.config;
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
import com.example.demo.service.JpaUserDetailsServiceImpl;
|
304
|
+
|
305
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
306
|
+
|
307
|
+
import org.springframework.context.annotation.Bean;
|
308
|
+
|
309
|
+
import org.springframework.context.annotation.Configuration;
|
310
|
+
|
311
|
+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
312
|
+
|
313
|
+
import org.springframework.security.config.annotation.authentication.configuration.GlobalAuthenticationConfigurerAdapter;
|
314
|
+
|
315
|
+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
316
|
+
|
317
|
+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
318
|
+
|
319
|
+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
320
|
+
|
321
|
+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
322
|
+
|
323
|
+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
324
|
+
|
325
|
+
import org.springframework.security.crypto.password.PasswordEncoder;
|
326
|
+
|
327
|
+
|
328
|
+
|
265
329
|
@Configuration
|
266
330
|
|
267
331
|
@EnableWebSecurity
|
@@ -398,6 +462,26 @@
|
|
398
462
|
|
399
463
|
```JpaUserDetailsServiceImpl
|
400
464
|
|
465
|
+
package com.example.demo.service;
|
466
|
+
|
467
|
+
import com.example.demo.entity.Account;
|
468
|
+
|
469
|
+
import com.example.demo.entity.AccountRepository;
|
470
|
+
|
471
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
472
|
+
|
473
|
+
import org.springframework.security.core.userdetails.UserDetails;
|
474
|
+
|
475
|
+
import org.springframework.security.core.userdetails.UserDetailsService;
|
476
|
+
|
477
|
+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
478
|
+
|
479
|
+
import org.springframework.stereotype.Component;
|
480
|
+
|
481
|
+
import org.springframework.transaction.annotation.Transactional;
|
482
|
+
|
483
|
+
import java.util.Objects;
|
484
|
+
|
401
485
|
@Component
|
402
486
|
|
403
487
|
@Transactional
|
@@ -446,194 +530,6 @@
|
|
446
530
|
|
447
531
|
|
448
532
|
|
449
|
-
```TopController
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
@Controller
|
454
|
-
|
455
|
-
@RequestMapping("/top")
|
456
|
-
|
457
|
-
public class TopController {
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
/**トップサービス*/
|
462
|
-
|
463
|
-
private final TopService service;
|
464
|
-
|
465
|
-
/** HTTPセッション */
|
466
|
-
|
467
|
-
private final HttpSession session;
|
468
|
-
|
469
|
-
/** セッションキー(ログインユーザのアカウント) */
|
470
|
-
|
471
|
-
private static final String SESSION_FORM_ID = "account";
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
/** コード値 */
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
@Autowired
|
480
|
-
|
481
|
-
public TopController(TopService topService, HttpSession session) {
|
482
|
-
|
483
|
-
this.service = topService;
|
484
|
-
|
485
|
-
this.session = session;
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
}
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
/**
|
494
|
-
|
495
|
-
* ログイン成功時処理。
|
496
|
-
|
497
|
-
*
|
498
|
-
|
499
|
-
* @return Path
|
500
|
-
|
501
|
-
*/
|
502
|
-
|
503
|
-
@RequestMapping(value = "loginSuccess")
|
504
|
-
|
505
|
-
public String loginSuccess() {
|
506
|
-
|
507
|
-
return "redirect:/top";
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
/**
|
514
|
-
|
515
|
-
* トップ画面表示。
|
516
|
-
|
517
|
-
*
|
518
|
-
|
519
|
-
* @param account 認証されたアカウント
|
520
|
-
|
521
|
-
* @param model モデル
|
522
|
-
|
523
|
-
* @return Path
|
524
|
-
|
525
|
-
*/
|
526
|
-
|
527
|
-
@RequestMapping(value = "")
|
528
|
-
|
529
|
-
public String init(@AuthenticationPrincipal Account account, Model model) {
|
530
|
-
|
531
|
-
// 初回のアクセスなら、アカウントを検索してセッションに格納する
|
532
|
-
|
533
|
-
if (Objects.isNull(session.getAttribute(SESSION_FORM_ID))) {
|
534
|
-
|
535
|
-
Account sessionAccount = service.getAccountById(account.getAccountId());
|
536
|
-
|
537
|
-
session.setAttribute(SESSION_FORM_ID, sessionAccount);
|
538
|
-
|
539
|
-
}
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
return "top";
|
544
|
-
|
545
|
-
}
|
546
|
-
|
547
|
-
}
|
548
|
-
|
549
|
-
```
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
```login
|
554
|
-
|
555
|
-
<!DOCTYPE html>
|
556
|
-
|
557
|
-
<html xmlns:th="http://www.thymeleaf.org">
|
558
|
-
|
559
|
-
<head xmlns:th="http://www.w3.org/1999/xhtml">
|
560
|
-
|
561
|
-
<meta charset="UTF-8">
|
562
|
-
|
563
|
-
<title>Login</title>
|
564
|
-
|
565
|
-
</head>
|
566
|
-
|
567
|
-
<body class="body-background">
|
568
|
-
|
569
|
-
<div>
|
570
|
-
|
571
|
-
<nav th:replace="nav"></nav>
|
572
|
-
|
573
|
-
<div class="container">
|
574
|
-
|
575
|
-
<div class="row justify-content-center">
|
576
|
-
|
577
|
-
<div class="col-6">
|
578
|
-
|
579
|
-
<div class="card bg-transparent border-secondary">
|
580
|
-
|
581
|
-
<div class="card-body">
|
582
|
-
|
583
|
-
<form class="col-8 offset-2" id="login_form" method="post" th:action="@{'/login'}">
|
584
|
-
|
585
|
-
<fieldset>
|
586
|
-
|
587
|
-
<div class="form-group required">
|
588
|
-
|
589
|
-
<input type="text" name="login_id" id="login_id" class="form-control" placeholder="Email" required/>
|
590
|
-
|
591
|
-
</div>
|
592
|
-
|
593
|
-
<div class="form-group required">
|
594
|
-
|
595
|
-
<input type="password" name="login_password" id="login_password" class="form-control" placeholder="Password" required/>
|
596
|
-
|
597
|
-
</div>
|
598
|
-
|
599
|
-
<div class="form-group col-6 offset-3 margin-bottom">
|
600
|
-
|
601
|
-
<input type="submit" value="Login"
|
602
|
-
|
603
|
-
class="submit-button btn btn-block center-block bg-custom3"/>
|
604
|
-
|
605
|
-
</div>
|
606
|
-
|
607
|
-
</fieldset>
|
608
|
-
|
609
|
-
</form>
|
610
|
-
|
611
|
-
<p id="signup">or
|
612
|
-
|
613
|
-
<a th:href="@{'/account/register/init'}">Sign up</a>
|
614
|
-
|
615
|
-
</p>
|
616
|
-
|
617
|
-
</div>
|
618
|
-
|
619
|
-
</div>
|
620
|
-
|
621
|
-
</div>
|
622
|
-
|
623
|
-
</div>
|
624
|
-
|
625
|
-
</div>
|
626
|
-
|
627
|
-
</div>
|
628
|
-
|
629
|
-
</div>
|
630
|
-
|
631
|
-
</body>
|
632
|
-
|
633
|
-
</html>
|
634
|
-
|
635
|
-
```
|
636
|
-
|
637
533
|
|
638
534
|
|
639
535
|
### 試したこと
|