Com o usuario cadastrado no banco de dados, o insominia apresenta o seguinte erro ao tentar autenticar:
{
"timestamp": "2023-04-21T01:40:32.735+00:00",
"status": 403,
"error": "Forbidden",
"trace": "org.springframework.security.authentication.BadCredentialsException: Usuário inexistente ou senha inválida\r\n\tat org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:80)\r\n\tat org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:147)\r\n\tat org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182)\r\n\tat
...
"message": "Access Denied",
"path": "/login"
}
Segue a minha classe Controller
@RestController
@RequestMapping("/login")
public class AutenticacaoController {
@Autowired
private AuthenticationManager manager;
@PostMapping
public ResponseEntity efetuarLogin(@RequestBody @Valid DadosAutenticacaoUsuario dados) {
var token = new UsernamePasswordAuthenticationToken(dados.login(), dados.senha());
var authentication = manager.authenticate(token);
return ResponseEntity.ok().build();
}
}
Classe Service:
@Service
public class AutenticaoUsuarioService implements UserDetailsService {
@Autowired
private UsuarioRepository usuarioRepository;
//método que o spring busca toda vez que o usuário fizer login
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return usuarioRepository.findByLogin(username);
}
}
Classe repository:
public interface UsuarioRepository extends JpaRepository<Usuario, Long> {
UserDetails findByLogin(String login);
}
Classe DTO:
public record DadosAutenticacaoUsuario(
String login,
String senha
) {
}
Classe SecurityConfigurations:
@Configuration
@EnableWebSecurity
public class SecurityConfigurations {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Alguém poderia me ajudar a descobrir o problema?