The European Court of Justice has said member states can deny certain payments to unemployed EU citizens who move to that country just to claim benefits.
The court said Germany was right to deny an unemployed Romanian woman a particular allowance because she showed no sign of seeking work.
Correspondents say the ruling sets a precedent for the rest of the EU.
Politicians in several member states have promised to crack down on what they call “benefit tourism”.
British Prime Minister David Cameron welcomed the ruling, describing it as “simple common sense”.
Meanwhile German MEP Manfred Weber, the leader of the main conservative group in the European Parliament, told the Associated Press news agency that the decision proved EU countries could “avoid social benefits tourism without violating the free movement of citizens”.
“It sends a clear signal to the member states and to the British prime minister in particular.”
The ruling only relates to non-contributory benefits, where the claimant does not make a contribution through the tax system.
Tuesday’s ruling from the Luxembourg-based court relates to a case involving a Romanian woman and her son living in Germany who had been denied access to a non-contributory subsistence allowance from its social security system.
The court said the defendant did not have sufficient financial resources to claim residency in Germany after an initial three months, so she could not claim that the rules excluding her from certain benefits were discriminatory.
The EU court said there was no discrimination involved in denying a citizen from another EU country access to a non-contributory benefit which is available to German citizens.
The court pointed to safeguards in EU rules which it said aim to “prevent economically inactive Union citizens from using the host member state’s welfare system to fund their means of subsistence”.
National parliaments, the ruling stated, have the “competence to define the extent of the social cover” offered in the form of certain non-contributory benefits.
The UK government has already tightened the rules so EU migrants have to wait three months after arriving in the UK before they can claim child benefit and child tax credits.
Migrants are also barred from claiming jobseeker’s allowance after three months if they are not looking for work, and the prime minister has called for reforms to the principle of free movement of people within the EU.
The European Commission said it had consistently stressed that freedom of movement of people was about the right of circulation, not about an unrestricted right to claim benefits.
It said it supported action by member states to tackle abuse of the benefits system by migrants.