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

質問編集履歴

4

エラー文追記

2021/12/24 10:56

投稿

nuIIpo
nuIIpo

スコア20

title CHANGED
File without changes
body CHANGED
@@ -22,18 +22,22 @@
22
22
  java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "javax.servlet.http.HttpServletRequest.getHeader(String)" is null
23
23
 
24
24
  ```
25
+ フロントエンド側のエラー文
26
+ ```
27
+ 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.
25
28
 
29
+ POST http://localhost:8080/api/auth/login net::ERR_FAILED 401
30
+ ```
31
+
26
32
  ```authRequest
27
- {userName: 'user', password: 'pass'}
33
+ {"userName": 'user', "password": 'pass'}
28
- password: "pass"
29
- userName: "user"
30
34
  ```
31
35
 
32
36
  ### 該当のソースコード
33
37
 
34
38
  ```java
35
39
  //Controller
36
- @PostMapping("api/auth/login")
40
+ @PostMapping("/api/auth/login")
37
41
  public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
38
42
  final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
39
43
  authenticationRequest.getUsename(), authenticationRequest.getPassword()));

3

Controllerのアノテーション修正

2021/12/24 10:56

投稿

nuIIpo
nuIIpo

スコア20

title CHANGED
File without changes
body CHANGED
@@ -33,7 +33,7 @@
33
33
 
34
34
  ```java
35
35
  //Controller
36
- @PostMapping("/auth/login")
36
+ @PostMapping("api/auth/login")
37
37
  public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
38
38
  final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
39
39
  authenticationRequest.getUsename(), authenticationRequest.getPassword()));

2

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

2021/12/24 10:36

投稿

nuIIpo
nuIIpo

スコア20

title CHANGED
File without changes
body CHANGED
@@ -23,8 +23,36 @@
23
23
 
24
24
  ```
25
25
 
26
+ ```authRequest
27
+ ▼{userName: 'user', password: 'pass'}
28
+ password: "pass"
29
+ userName: "user"
30
+ ```
31
+
26
32
  ### 該当のソースコード
27
33
 
34
+ ```java
35
+ //Controller
36
+ @PostMapping("/auth/login")
37
+ public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest)throws InvalidKeySpecException, NoSuchAlgorithmException {
38
+ final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
39
+ authenticationRequest.getUsename(), authenticationRequest.getPassword()));
40
+
41
+ SecurityContextHolder.getContext().setAuthentication(authentication);
42
+
43
+ //トークン生成
44
+ User user=(User)authentication.getPrincipal();
45
+ String jwtToken=jwtprovider.createToken(user);
46
+
47
+          //レスポンス生成
48
+ LoginResponse response=new LoginResponse();
49
+ response.setToken(jwtToken);
50
+
51
+ return ResponseEntity.ok(response);
52
+ }
53
+ }
54
+ ```
55
+
28
56
  ```javascript
29
57
  export const userLogin=(authRequest)=>{
30
58
  return axios({

1

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

2021/12/24 10:33

投稿

nuIIpo
nuIIpo

スコア20

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  postmanでは、
6
6
  method: POST
7
- URL: http:locaclhost:8080/auth/login
7
+ URL: http:locaclhost:8080/api/auth/login
8
8
  body:{
9
9
  "userName":"user",
10
10
  "password":"pass",