The knife-wielding man was subsequently detained, Spanish Interior Minister Juan Ignacio Zoido said.
Madrid: 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' (God is Greatest), slightly injuring a policeman," Irene Flores, spokeswoman for the central government's representative office in Melilla said.
Read: UK armed police arrested a man 'with knife' outside parliament
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.
Read: US cops shoot man threatening kids with knife
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.