Make the distinction between justice and science
You are talking about two very different things here...
Around 500 years ago it was common to think in Europe that people should only have sex within marriage
This is a matter of law, i.e. rules that society sets up to govern the behaviour of its citizens, its officials, its watchers, its protectors and itself.
5000 years ago people commonly thought the Sun is not a star. Now people commonly think the Sun is star.
This is a matter of science and knowledge, which is a completely different matter. Sol and the other stars are what they are, no matter what we humans believe about it.
The difference here is that laws are entirely artificial. Sure, we may share some fundamental ethics with fellow animals, some of those ethics being quite advanced, such as equal pay for equal work, but in the end, ethics, morals and laws are man-made. You cannot derive any law that we have without having some kind of man-made first principle up the chain of reasoning.
Nature and the universe as a whole is different. Nature made us, we did not make it. Nature exists as it does, and when we want to find out how that works, we can always go to nature to look at it, poke at it and see how it reacts. We have an external reference that is not ourself.
So the latter example, we can just throw out. Becuase people were quite simply wrong in their mistaken belief 5 000 years ago that the Sun was special from the stars. It has nothing to do with your question.
So it boils down to this...
Is consensus enough of a justification for justice?
This is a huge subject. On what basis do we make laws? By what justification do we lay down the framework for society and the rules that govern it? To give you the complete rundown on how we have created the rules for our society; how we review, update and delete rules; how we change the process by which we creare, review, update and delete rules... and — to the core of your question — how we justify that this is a valid thing to do, is the subject of years of study at the university, and simply will not lend itself to the limited space we have here on Stack Exchange.
So to summarize the summary of the summary: people are a problem. This problem needs to be dealt with, because experience shows that when we have no rules at all and everyone just runs about doing whatever to whoever whenever, things do not work very well. Sooner or later there will be conflict and strife. So we need rules.
We have also found that we cannot just let everyone make rules willy-nilly, because that is nearly as bad as having no rules at all. We have also found that we cannot let an elite make rules willy-nilly, because that allows the elite to do things that furthers their interrests to the detriment of everyone else.
So we have come up with the notion of constitution. That is to say: the rules for how to make rules.
Why do we have this? What is the justifiction for having this? And most important: why do you have to abide by any of this?
Well, you really do not. You can just ignore all of that if you like. You can boycott society if you want and just do your own thing. But the rest of us will not like it, and we will forcibly stop you from doing some things we really do not want you to do.
You might object to this, and say that you never agreed to letting us do that. But then again: you elected to boycott society, and go your own way. What makes you think you then have a say in anything? If you have said that you will not partake in a discourse with society, then society cannot hear your voice in the matter.
In the end it comes down to a choice on your part: either you choose to be a part of society and reap the benefits and obligations of that — such as being assigned rights and duties — or you can choose to not be, and stand entitrely on your own with no duties, but also no rights. The latter is admittedely hard to achieve though because as a matter of your rights, no society is allowed to say you have no rights any more. So once you are born into a society: sorry, but you are a citizen now, with inalianable rights.
As I said above, this is just the extremely abbreviated and ultra-condensed version of how things work. To give the full explanation I would have to keep writing for days on end. But in the end: yes, justice is based on consensus. And you are born into it, without your consent. Isn't that ironic...