Sayyiduna al-Miqdam (may Allah be pleased with him) says that the Prophet (peace and blessings be upon him) said to them: "Obey your rulers. If they command you with what I have told you, they will be rewarded for it and you will be rewarded for your obedience; and if they command you with something other than what I have told you, its burden is upon them and you are free of it. That is because when you meet Allah, you will say: 'Our Lord, there is no injustice,' and Allah will say: 'There is no injustice.' Then you will say: 'Our Lord! You sent Messengers to us and we obeyed them; then (You appointed) caliphs and by Your command we obeyed them too; then (You appointed) rulers and by Your command alone we obeyed them too.' Allah will say: 'You have spoken the truth; its burden is upon them, and you are free of it.'"
Hadith Referenceالسنن الكبرى للبيهقي / كتاب قتال أهل البغي / 16626