public record DatosAutenticacionUsuario(
String login,
String clave
) {
}
public record DatosJWTToken(String token) {
}
@RestController
@RequestMapping("/login")
public class AuthenticationController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private TokenService tokenService;
@PostMapping
public ResponseEntity autenticarUsuario(@RequestBody @Valid DatosAutenticacionUsuario datos) {
var authenticationToken = new UsernamePasswordAuthenticationToken(datos.login(), datos.clave());
var autenticacion = authenticationManager.authenticate(authenticationToken);
var jwtToken = tokenService.generarToken((Usuario) autenticacion.getPrincipal());
return ResponseEntity.ok(new DatosJWTToken(jwtToken));
}
}