#Having trouble connecting my Swagger to the H2.
19 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @void lotus! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 720 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
the error log includes:
com.erp.controller.AuthController.login(AuthController.java:32)
try debugging the AuthController#login method
and when I say debugging, I mean stepping through with a debugger
It looks like you are having issues with debugging or issues that can be solved using a debugger.
Check out this article on dev.java to see how debugging works and how to use a debugger.
This Stack Overflow question and its answers also explain debugging in general.
These links describe how to use the debugger in some IDEs:
• Debugging in IntelliJ
• Debugging in Eclipse
basically it seems like that method considers the credentials to be invalid
Can you show that method?
package com.erp.controller;
import com.erp.config.JwtService;
import com.erp.dto.LoginRequestDTO;
import com.erp.dto.LoginResponseDTO;
import com.erp.entity.Usuario;
import com.erp.repository.UsuarioRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
private final AuthenticationManager authenticationManager;
private final JwtService jwtService;
private final UsuarioRepository usuarioRepository;
@PostMapping("/login")
public ResponseEntity<LoginResponseDTO> login(@RequestBody LoginRequestDTO request) {
System.out.println("=== TENTATIVA DE LOGIN ===");
System.out.println("Login: " + request.getLogin());
System.out.println("Senha: " + request.getSenha());
try {
Authentication auth = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(request.getLogin(), request.getSenha())
);
System.out.println("Autenticação bem-sucedida: " + auth.isAuthenticated());
Usuario usuario = usuarioRepository.findByLogin(request.getLogin())
.orElseThrow(() -> new UsernameNotFoundException("Usuário não encontrado"));
String token = jwtService.generateToken(usuario);
System.out.println("Token gerado: " + token.substring(0, 20) + "...");
return ResponseEntity.ok(new LoginResponseDTO(token, "Bearer", usuario.getId(), usuario.getNome(), usuario.getLogin()));
} catch (Exception e) {
System.out.println("ERRO NO LOGIN: " + e.getClass().getSimpleName());
System.out.println("Mensagem: " + e.getMessage());
e.printStackTrace();
throw e;
}
}
}
All the code
What exactly handles the actual authentication? Getting the user, checking the password, etc
The authentication flow is managed by Spring Security through the DaoAuthenticationProvider, which uses my UserDetailsServiceImpl to load the user from the database and the BCryptPasswordEncoder to validate the password.
Could this error be happening because I'm typing "password: admin" in Swagger, and the code is already encrypted, causing the encrypted password to be encrypted again?
idk, I didn't see that part of your code
I'm not opening a rar file on my phone to navigate through code on it lol
Passwords shouldn't be encrypted (except during transmission which should be handled by TLS), passwords should be hashed
and then you can use the corresponding APIs to check the password against the hash
With Spring, that's passwordEncoder.matches(passwordFromUser, hashedPassword)
The stack trace mentions
org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:141)
Do you have aUsserDetailService?