"Economically inactive EU citizens who go to another member state solely in order to obtain social assistance may be excluded from certain social benefits," judges of the Court of Justice of the Europe Union (ECJ) said.
They were ruling on a case involving Romanian Elisabeta Dano and her son who applied for a form of German welfare benefit.
German authorities rejected their request, prompting Dano to appeal to a German court which subsequently sought advice from the Luxembourg-based ECJ.
Read MoreMerkel 'putting her foot down'
Cameron has promised to try to reshape Britain's EU ties before holding a referendum on the country's EU membership by 2017 if he wins next year's election.
"One of the things that(the ruling)...underlines is that the freedom of movement, as the prime minister and others have said, is not a unqualified right," a spokesman for Cameron said. "We will look very carefully at what we and other governments can do working together in response to this."
Read MoreSwedes want morewelfare: Prime Minister
Cameron's Conservatives want to stop what they regard as welfare abuse by poor immigrants from eastern Europe with no jobs and no health cover, and ease pressure on local services such as health and housing. Critics accuse him of exaggerating the problem to curry favour with voters who might turn to UKIP.
Cameron's bid to cap immigration in a more systematic way has provoked warnings from the European Commission, German Chancellor Angela Merkel and other EU leaders who regard freedom of movement as sacrosanct.