Mexican–American War from the war of independence (1810–1821) as a weak and divided force. Before the war with the United States, the military faced both internal and foreign challenges. The Spanish still occupied the coastal fortress of San Juan de Ulúa, and Spain did not recognize Mexico's independence, so that the new nation was at risk for invasion. In 1829, the Spanish attempted to reconquer their former colony and Antonio López de Santa Anna became a national hero defending the homeland. The army had a set of privileges ("fueros"), established in the colonial era, that gave it jurisdiction over many aspects of
Mexican War of Independence rule until the Hidalgo revolt of 1810. Elites in Mexico City in the seventeenth century did force the removal of a reformist viceroy, the Marqués de Gelves, following an urban riot in 1624 fomented by those elites. He attempted to eliminate corrupt practices by creole elites as well as rein in the opulent displays of the clergy's power, but ecclesiastical authorities in conjunction with creole elites mobilized urban plebeians to oust the viceroy. The crowd was reported to shout, "Long live the King! Love live Christ! Death to bad government! Death to the heretic Lutheran [Viceroy Gelves]! Arrest the viceroy!"