Authentic Supplication Number 35.

Abu Salamata bin Abdurrahman said: I heard Qatada son of Rib`iy say: I heard the Messenger of Allah  say: “Ru’ya (57) is from Allah, and hulmu is from the shaytan, so if one of you sees something he hates, let him spittle (58) to his left three times when he awakens, and let him seek protection in Allah from its evil, for, Allah willing, it will not harm him.”

Abu Salamata said: I used to consider a Ru’ya heavier upon me than a mountain, then when I heard this hadeeth, I became disconcerned, and in one narration “He said: I used to see Ru’yas as cause for concern, until I heard Aba Qatada say: I used to see Ru’yas and they would make me sick, until I heard the Messenger of Allah  say: “The good Ru’ya is from Allah, so if one of you sees what he likes, then he should not tell anyone about it except those whom he loves, and if he sees something he hates, then he should tell no one of it, and he should spit to his left three times, and solicit Allah’s protection from the despicable Shaytan from the evil of what he saw, for it will not harm him.

Footnotes.
(57) This hadeeth seems to be describing a Ru’ya as one might describe the English word “Dream”, and a Hulum as the English word “Nightmare”. Later in this hadeeth we see that the word Ru’ya can also be used loosely to describe any vision (good or bad) experienced during sleep.

(58) See footnote #45.

Reporters.
Agreed upon by Bukhari and Muslim, and Al-Albani included it in The Authentic of Good Sayings as #39.