The Prophet ﷺ said, "The leader is but a shield, behind whom people fight and by whom they are protected. If he commands the fear of Allah and rules with justice, he will be rewarded for that. But if he commands otherwise, he will bear the consequences of it."
عن النبي ﷺ قال: إنما الإمام جنة يقاتل من ورائه ويتقى به، فإن أمر بتقوى الله عز وجل وعدل، كان له بذلك أجر، وإن يأمر بغيره كان عليه منه.
Bukhari related it in the book of jihad and expeditions (2957), and Muslim in the book of leadership (1841), both from the hadith of Abu Zinad, from Araj, from Abu Hurairah, who mentioned it. The wording given here is Muslim's.