Dies ist die Logikimplementierung, die ich in meinem Controller für meine Login-REST-API verwendet habe:
Code: Select all
@PostMapping("/login")
public ResponseEntity login(@RequestBody LoginDto loginDto){
String response=authService.login(loginDto);
return ResponseEntity.ok(response);
}
Code: Select all
@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService{
private final AuthenticationManager authenticationManager;
@Override
public String login(LoginDto loginDto) {
Authentication authentication=authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
loginDto.getUsernameOrEmail(), loginDto.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
return "User Logged-in Successfully";
}
}
Die erwartete Ausgabe ist, dass sie den Statuscode 200 anzeigen und „Benutzer erfolgreich angemeldet“ zurückgeben sollte.
aber wenn ich versuche, diese API zu testen, erhalte ich diese Art von Fehler Meldung in meiner Konsole:
org.springframework.security.authentication.BadCredentialsException: Bad credentials
Wie kann ich dieses Problem beheben?
Mobile version