Pope St. Leo did have visions, which did inspire this prayer. It’s no urban legend.
"An article in the Roman journal Ephemerides Liturgicae (V. LXIX, pages 54-60) in 1955 gave an account in Latin and Italian of how the St. Michael prayer developed. Footnote nine of this account quotes an article in another Italian journal called La Settimana del Clero in 1947 by Fr. Domenico Pechenino who worked at the Vatican during the time of Leo XIII, in which he stated:
"I do not remember the exact year. One morning the great Pope Leo XIII had celebrated a Mass and, as usual, was attending a Mass of thanksgiving. Suddenly, we saw him raise his head and stare at something above the celebrant's head. He was staring motionlessly, without batting an eye. His expression was one of horror and awe; the colour and look on his face changing rapidly. Something unusual and grave was happening in him.
“Finally, as though coming to his senses, he lightly but firmly tapped his hand and rose to his feet. He headed for his private office. His retinue followed anxiously and solicitously, whispering: ‘Holy Father, are you not feeling well? Do you need anything?’ He answered: ‘Nothing, nothing.’ About half an hour later, he called for the Secretary of the Congregation of Rites and, handing him a sheet of paper, requested that it be printed and sent to all the ordinaries around the world. What was that paper? It was the prayer that we recite with the people at the end of every Mass. It is the plea to Mary and the passionate request to the Prince of the heavenly host, [St. Michael: Saint Michael the Archangel, defend us in battle] beseeching God to send Satan back to hell.”
According to the same article in Ephemerides Liturgicae, Cardinal Giovanni Nasalli Rocca di Corneliano wrote in his Litteris Pastoralibus pro Quadragesima (Pastoral Letters for Lent) that “the sentence ‘The evil spirits who wander through the world for the ruin of souls’ has a historical explanation that was many times repeated by his private secretary, Monsignor Rinaldo Angeli. Leo XIII truly saw, in a vision, demonic spirits who were congregating on the Eternal City (Rome). The prayer that he asked all the Church to recite was the fruit of that experience. He would recite that prayer with strong, powerful voice: we heard it many a time in the Vatican Basilica. Leo XIII also personally wrote an exorcism that is included in the Roman Ritual. He recommended that bishops and priests read these exorcisms often in their dioceses and parishes. He himself would recite them often throughout the day.”
Remember, visions are not all that unusual of a spiritual experience. In fact, there’s been more saints who weren’t thought of as particularly visionary or miracleworking who turn out to have had remarkable experiences or done wonders, than those who don’t. (Though there’s plenty of saints who don’t, too.)
Catholic urban legends tend, rather, to elaborate on known visions and prophecies, and to insist that they either describe exactly what’s going on now or point directly to a specific sort of horrible doom which is currently most feared. If people are scared of UFOs, then the Fatima Third Secret is really about alien invasion. If people are scared of nukes, it’s about nuclear war. If people are scared of global warming, it’s about the earth shriveling up thanks to excessive release of carbons.
The next favorite is to claim that a known vision or prophecy wasn’t about what it’s generally claimed to have been about. Joan of Arc wasn’t the real Pucelle; the real Pucelle who’ll save France is X. The Third Secret wasn’t really about Pope John Paul II getting shot; it’s really all about flying dragons and the version released was a pathetic coverup.
Only the third favorite is making up prophecies and visions for some famous saint who, sadly, never seemed to have had any apocalyptic, mystical, obscure visions. This usually is done by claiming that of course St Y had visions, but they were covered up by the mean nasty bad people.