A knife-wielding man shouting "Allahu Akbar" charged across the border between Morocco and the Spanish territory of Melilla on Tuesday, attacking and injuring a police officer, authorities said.
The man was subsequently detained, Spanish Interior Minister Juan Ignacio Zoido said.
"A man entered the border post and once inside, pulled out a large knife and confronted (police) shouting 'Allahu Akbar', slightly injuring a policeman," Irene Flores, spokeswoman for the central government's representative office in Melilla, told AFP.
A spokesman for Spanish police added he ran into the border post.
Melilla and its sister city Ceuta are two Spanish territories located on Morocco's northern coast, and as such represent the only two land borders between Africa and the European Union.
They have been hit by unrest before as migrants desperate to reach Europe regularly storm the border between both territories or try and smuggle themselves in, but this is the first attack of this type.
Spain has been spared the type of jihadist violence that has occurred in nearby France, Belgium and Germany.
But it was hit by what is still Europe's deadliest jihadist attack in March 2004, when bombs exploded on commuter trains in Madrid, killing 191 people in an attack claimed by Al Qaeda-inspired militants.