The Messenger of Allah ﷺ said, "Whoever obeys me has obeyed Allah, and whoever disobeys me has disobeyed Allah. Whoever obeys my appointed leader has obeyed me, and whoever disobeys my appointed leader has disobeyed me."
أخبرنا يوسف بن سعيد قال حدثنا حجاج عن ابن جريج أن زياد بن سعد أخبره أن ابن شهاب أخبره أن أبا سلمة أخبره أنه سمع
قال رسول الله ﷺ من أطاعني فقد أطاع الله ومن عصاني فقد عصى الله ومن أطاع أميري فقد أطاعني ومن عصى أميري فقد عصاني
Muslim narrated it in the Book of Leadership, Chapter on the obligation of obeying rulers in matters that do not involve disobedience [to Allah] and the prohibition of obeying them in matters of disobedience, hadith no. 1835, from the hadith of Ibn Jurayj. Bukhari narrated it in the Book of Judgments, Chapter on the saying of Allah Almighty, "Obey Allah and obey the Messenger and those in authority among you," hadith no. 7137, from the hadith of Ibn Shihab Zuhri, with the same wording. It is also found in Al-Kubra, hadith no. 7816.