No, making the law does not make you legitimate, having authority accepted by your military and tolerated by your people is enough.
Look at Taiwan for example? Almost every country does not acknowledge them as a country separate from China yet they are because their people and military accept their authority over their island and they have control over the island.
Look at north korea, the Kim family's rule is very much legitimate even though we can't say most of the people enjoy their rule, however they tolerate it and the military accepts them as rightful rulers. You can't justifiably prosecute them under NK law for stealing from their starving people for example because everything they do is by their legitimate and basically absolute authority.
If a ruler is not legitimate it is either the military or the people that have to engage in active revolt at a meaningful scale to bring about doubt regarding the ruler's legitimacy.
In the US civil war for example, it is hard to say Abraham Lincoln was the legitimate ruler of the south. At least not until he won the war.
Rules that can't be enforced have no meaning. Rules are enforced by force which ultimately means military but at the same time rules also have no meaning if a very largr portion if the people being ruled reject the rules. Therefore under the rule of law, legitimacy of a ruler is established simply by their ability to make and enforce rules. Not their popularity or niceness.
Anyone who followed the law cannot be punished and anyone who broke the law is subject to punishment. You can change laws and punish people who break them but the rule of law loses all meaning if anyone can be punished for an action that was lawful at the time.