The case cuts to the heart of the feud over migration that has bitterly divided the EU since a spike in Mediterranean arrivals caught it unprepared in 2015.
The three ex-communist countries on the EU's eastern flank refused to take in any of those refugees and migrants, citing security concerns, and questioned the legal grounds for the EU assigning each country a fixed quota of people to accept.
The matter eventually ended up in court and the Advocate General, which advises the European Court of Justice (ECJ), said on Thursday that EU law must be followed and the bloc's principle of solidarity "necessarily sometimes implies accepting burden-sharing."
The ECJ is not obliged to, but usually does follow the Advocate General's opinions. A ruling in this case is expected early next year. The ECJ can fine member countries that violate EU law.
Polish government spokesman Piotr Muller, responding to the Advocate General's opinion, said that "ensuring security for our citizens is the most important goal of the government's policies. Out actions were dictated by the interests of Polish citizens and the need for protection against uncontrolled migration."
Beyond migration, the nationalist governments in Poland and Hungary have also locked horns with the EU over the rule of law and climate policies.
The opinion comes as Germany has warned of a repeat of the 2015 EU migration crisis, expressing concern over a resurgence of arrivals from Turkey.
In 2015, more than a million refugees and migrants from the Middle East reached the EU, an influx that left the bloc scrambling to provide shelter and security.
The EU tried to introduce quotas to make sharing out asylum-seekers automatic among member states to help southern countries on the Mediterranean where most people arrived.But vehement opposition by Warsaw, Budapest and others has largely defeated the attempt and, four years on, the bloc is still quarreling over how to manage those seeking shelter from the wars in the Middle East or poverty and conflicts in Africa.