Bonsoir,
Spring Boot
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 @PostMapping("/signin") public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) throws ResourceNotFoundException { logger.info("Verify if username [ " + loginRequest.getUsername() + " ] exist in database"); User user = repository.findByUsername(loginRequest.getUsername()); if (user == null ) { logger.error("User with username: [ " + loginRequest.getUsername() + " ] hasn't been found in db."); //return ResponseEntity.notFound().build(); //return new ResponseEntity<>("username is incorrect", HttpStatus.NOT_FOUND); throw new ResourceNotFoundException("user " + loginRequest.getUsername() + " not found" ); } else { logger.info("User found : user [ " + user.getUsername() + " ] exist in database"); if (user.getPassword().equals(loginRequest.getPassword()) ) { logger.info("User found with password : user [ " + user.getUsername() + " ] exist in database"); return new ResponseEntity<>(user, HttpStatus.OK); } else { logger.info("User found but password no correct: user [ " + user.getUsername() + " ] exist in database"); //return new ResponseEntity<>("Password is incorrect", HttpStatus.NOT_FOUND); //return ResponseEntity.badRequest().body("Password is incorrect"); throw new ResourceNotFoundException("password is incorrect " ); //return ResponseEntity.notFound(). } } }Postman021-01-22 14:33:37.551 INFO 8500 --- [nio-9000-exec-4] c.m.l.c.AuthenticationController : Verify if username [ 323465X ] exist in database
2021-01-22 14:33:37.651 INFO 8500 --- [nio-9000-exec-4] c.m.l.c.AuthenticationController : User found : user [ 323465X ] exist in database
2021-01-22 14:33:37.651 INFO 8500 --- [nio-9000-exec-4] c.m.l.c.AuthenticationController : User found but password no correct: user [ 323465X ] exist in database
2021-01-22 14:33:37.672 WARN 8500 --- [nio-9000-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [com.mis.laplace.exception.ResourceNotFoundException: password is incorrect ]
Angular
auth service
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 addHero(context: LoginContext): Observable<any> { return this.http.post<User>(`${environment.serverUrl}/auth/signin`, {username: context.username, password: context.password}) .pipe( catchError((error) =>this.handleError(error)) ); } private handleError(error: HttpErrorResponse) { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong. console.error( `Backend returned code ${error.status}, ` + `body was: ${error.message}`); } // Return an observable with a user-facing error message. return throwError( 'Something bad happened; please try again later.'); }
Je m'attendais a avoir password is incorrect dans le message de l'erreur mais j'ai plutot Http failure response for http://localhost:9000/api/auth/signin: 404 OK.
Merci pour votre aide
Partager