#Having trouble connecting my Swagger to the H2.

19 messages · Page 1 of 1 (latest)

runic windBOT
#

This post has been reserved for your question.

Hey @void lotus! Please use /close or the Close Post button 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.

golden aspen
#

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

runic windBOT
golden aspen
#

Can you show that method?

void lotus
#

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;
    }
}

}

golden aspen
#

What exactly handles the actual authentication? Getting the user, checking the password, etc

void lotus
#

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?

golden aspen
#

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

golden aspen
#

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 a UsserDetailService?