As a recent example, the territory of Nagorno-Karabakh was recognized to be a part of Azerbaijan by the UN and most countries but de-facto controlled by Armenia. Does this mean that Azerbaijan's war to reclaim said territory was fully legal according to international law? We have a precedent where the UN authorized military action to resolve a territorial dispute, but I'm not sure if such a resolution was ever required in the case of Azerbaijan.
Article 51 allows for countries to exercise the right to self defense when they come under attack:
Nothing in the present Charter shall impair the inherent right of individual or collective self-defence if an armed attack occurs against a Member of the United Nations, [...]
After a successful attack, the proper, legal process is for the occupied country to petition the security council, which may then authorise the use of force.
In practice, there are 1000 variations on this and 1000 individual situations.