A good time for this to occur would be in the late 1850s. Spanish-American tension was high due the Black Warrior affair, disputes over Spanish intentions in Mexico and South America and threat of a major filibuster attempt on Cuba. Quitman's Death in 1858 prevented further filibuster attempts on Cuba and the confrontation between the American and Spanish squadrons off the coast of Veracruz came to nothing. If Quitman lives (he died of a weird illness (referred to as Hotel's Disease) acquired during Buchanan's inauguration; possibly some type of food poisoning) he may be able to organize a filibuster attempt in 1858 or 1859. There is a very good chance that this may lead to a Spanish-American War. Furthermore this conflict could easily coincide with the Spanish-Moroccan War (1859-60). It is conceivable for an official alliance to be formed between the US and Morocco if perhaps a bit unlikely. Of course a Republican victory in 1860 would almost certainly still bring about secession with the South promising the world to Spain, while acting all innocent about those pesky filibusters, to gain Spanish recognition. I doubt Spain would be powerful enough to tip the balance against the Union, though they could make the blockade far more difficult.
Benjamin