Judges at the Luxembourg court were asked to decide whether Germany could refuse 'Hartz IV' unemployment benefits to so-called 'poverty migrants' under certain conditions.
The case was prompted after a Romanian woman living in Leipzig was denied benefits by the local Jobcenter. Officials said she refused to take any of the jobs that were suggested to her.
Both the Jobcenter and a court in Leipzig agreed that 25-year-old Elisabeta Dano, who moved to Germany in 2010 to live with her sister, had never really been looking for work.
But she appealed her case all the way to the European Court.
Now the EU court has said that states must have the right to refuse social benefits to unemployed migrants.
The judges wrote that Dano did not have “sufficient means of support" for herself and her 10-year-old son when she moved to Germany, and therefore couldn't justify her right to remain.
That also meant that she couldn't bring a case against the authorities based on EU anti-discrimination rules.
"This judgement will hinder the misuse of social benefits and shuts the door to those who only come to Germany to get money from our benefits system," Christian Democratic Union General Secretary Peter Tauber told The Local.
"The CDU is for free movement of people within the EU, and the judgement doesn't limit that.
"And it's still not at issue that any European citizen who lives in Germany, has worked here and lost their job has a claim to social benefits."
As in other European countries, some Germans have warned of the risks of 'benefit tourism' from Bulgaria and Romania since their citizens gained freedom of movement at the beginning of 2014.
By September, there were 32,000 more Bulgarians in Germany than the previous year at almost 175,000, while the number of Romanians increased by almost 82,000 to 338,000.
But they were a small fraction of the total foreign-born population of almost eight million people.
The rules for EU migrants
EU citizens who have lived and worked in Germany for a long time have similar rights to unemployment benefits as ordinary Germans, 4.3 million of whom receive the benefits.
They can be granted for unemployment, to top up low wages, or to support workers with children under three years old or who are caring for other dependents.
But EU immigrants can't claim Hartz IV for their first three months in the country – or until they've held at least one job.
By agreeing that countries should be allowed to deny immigrants benefits under certain circumstances, the court followed the advice of experts whose testimony it had heard during the case.
They said that it should be legal to deny the applications of people who had travelled to a country with the sole purpose of claiming benefits.
Other European Union members have been eagerly awaiting the result, as it will have significant consequences for the current debate on freedom of movement.
British Prime Minister David Cameron hopes to limit the ability of migrants from other EU countries to claim benefits in the UK, as he is faced with a Eurosceptic, anti-immigration challenge from the right by the UK Independence Party.
The court received submissions from Dano's lawyer as well as the German, Danish, Irish, French, Austrian and UK governments and the European Commission, the judges reported in their ruling.