The Messenger of Allah ﷺ said, "Whoever obeys me has obeyed Allah, and whoever disobeys me has disobeyed Allah. Whoever obeys the leader has obeyed me, and whoever disobeys the leader has disobeyed me. The leader is a shield. When he says the takbir, say the takbir. When he bows, bow. When he says, 'Allah hears whoever praises Him,' say, 'O Allah, our Lord, to You belongs all praise,' for if your words coincide with the words of the angels, you will be forgiven. And if he prays sitting, then pray sitting."
حدثنا بهز حدثنا حماد بن سلمة قال أنبأنا يعلى بن عطاء عن أبي علقمة وقال أبو عوانة الأنصاري
أن رسول الله ﷺ قال من أطاعني فقد أطاع الله ومن عصاني فقد عصى الله ومن أطاع الأمير فقد أطاعني ومن عصى الأمير فقد عصاني والأمير مجن فإذا كبر فكبروا وإذا ركع فاركعوا وإذا قال سمع الله لمن حمده فقولوا اللهم ربنا لك الحمد فإنه إذا وافق ذلك قول الملائكة غفر لكم وإن صلى قاعدا فصلوا قعودا
Its chain is sahih. It occurred previously at 7428 with chains other than this one, and whoever examines this chain may not find the correct explanation clear, because there is no Abu Awana who narrates from Abu Hurairah. Rather, it is Abu Alqama who narrates it from Abu Hurairah, and Imam Ahmad inserted a parenthetical clause stating that this Abu Alqama is the Ansari, as we have clarified by our placement of the parenthetical brackets and two dots before "Ansari."Bukhari narrated it (13/111, no. 7137 in Fath al-Bari) in the Book of Rulings, on Allah's statement, "Obey Allah and obey the Messenger" [Nisa 4:59], and Muslim (3/1466, no. 1835) in the Book of Leadership, on the obligation of obeying rulers, and Nasai (7/154, no. 4193) in the Book of Allegiance, on the encouragement to obey the leader, and Ibn Majah (2/954, no. 2859), and Ibn Khuzayma (3/46, no. 1597).