質問編集履歴

4

エラー文追記

2021/12/24 10:56

投稿

nuIIpo
nuIIpo

スコア20

test CHANGED
File without changes
test CHANGED
@@ -46,15 +46,23 @@
46
46
 
47
47
  ```
48
48
 
49
+ フロントエンド側のエラー文
50
+
51
+ ```
52
+
53
+ Access to XMLHttpRequest at 'http://localhost:8080/api/auth/login' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
54
+
55
+
56
+
57
+ POST http://localhost:8080/api/auth/login net::ERR_FAILED 401
58
+
59
+ ```
60
+
49
61
 
50
62
 
51
63
  ```authRequest
52
64
 
53
- {userName: 'user', password: 'pass'}
65
+ {"userName": 'user', "password": 'pass'}
54
-
55
- password: "pass"
56
-
57
- userName: "user"
58
66
 
59
67
  ```
60
68
 
@@ -68,7 +76,7 @@
68
76
 
69
77
  //Controller
70
78
 
71
- @PostMapping("api/auth/login")
79
+ @PostMapping("/api/auth/login")
72
80
 
73
81
  public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
74
82
 

3

Controllerのアノテーション修正

2021/12/24 10:56

投稿

nuIIpo
nuIIpo

スコア20

test CHANGED
File without changes
test CHANGED
@@ -68,7 +68,7 @@
68
68
 
69
69
  //Controller
70
70
 
71
- @PostMapping("/auth/login")
71
+ @PostMapping("api/auth/login")
72
72
 
73
73
  public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
74
74
 

2

タグ追加とバックエンドのController.javaを追記しました。また、handleSubmit内にconsole.log(values);を挿入して得たauthRequestを追記しました

2021/12/24 10:36

投稿

nuIIpo
nuIIpo

スコア20

test CHANGED
File without changes
test CHANGED
@@ -48,10 +48,66 @@
48
48
 
49
49
 
50
50
 
51
+ ```authRequest
52
+
53
+ ▼{userName: 'user', password: 'pass'}
54
+
55
+ password: "pass"
56
+
57
+ userName: "user"
58
+
59
+ ```
60
+
61
+
62
+
51
63
  ### 該当のソースコード
52
64
 
53
65
 
54
66
 
67
+ ```java
68
+
69
+ //Controller
70
+
71
+ @PostMapping("/auth/login")
72
+
73
+ public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
74
+
75
+ final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
76
+
77
+ authenticationRequest.getUsename(), authenticationRequest.getPassword()));
78
+
79
+
80
+
81
+ SecurityContextHolder.getContext().setAuthentication(authentication);
82
+
83
+
84
+
85
+ //トークン生成
86
+
87
+ User user=(User)authentication.getPrincipal();
88
+
89
+ String jwtToken=jwtprovider.createToken(user);
90
+
91
+
92
+
93
+          //レスポンス生成
94
+
95
+ LoginResponse response=new LoginResponse();
96
+
97
+ response.setToken(jwtToken);
98
+
99
+
100
+
101
+ return ResponseEntity.ok(response);
102
+
103
+ }
104
+
105
+ }
106
+
107
+ ```
108
+
109
+
110
+
55
111
  ```javascript
56
112
 
57
113
  export const userLogin=(authRequest)=>{

1

postmanの入力値の変更http:locaclhost:8080/auth/login => http:locaclhost:8080/api/auth/login

2021/12/24 10:33

投稿

nuIIpo
nuIIpo

スコア20

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  method: POST
12
12
 
13
- URL: http:locaclhost:8080/auth/login
13
+ URL: http:locaclhost:8080/api/auth/login
14
14
 
15
15
  body:{
16
16