Britain has won a European court case upholding its right to withhold welfare payments from EU nationals who do not have the right to reside in the UK.
The European Court of Justice ruling has been hailed by pro-Remain campaigners as a sign that the UK can act to prevent "benefit tourism" while staying in the EU.
The European Commission took a legal case against Britain at the Luxembourg court, on the grounds that it was discriminating against citizens of other EU countries who attempted to claim handouts including child benefit and child tax credit.
But judges ruled that Britain was entitled to treat foreign nationals differently from its own citizens when it comes to benefits, in order to limit the burden on taxpayers.
Their ruling stated that the "right to reside" test "does not go beyond what is necessary to attain the legitimate objective pursued by the UK, namely the need to protect its finances". There is nothing in EU law to stop member-states imposing the test on jobless foreign nationals, it said.
Conservative employment spokesman in the European Parliament Anthea McIntyre MEP said: "This is a victory for the UK and for common sense. It vindicates the Prime Minister's stance, which has always been that free movement means freedom to work, not to claim benefits.
"The Commission may have attempted to dictate to Britain, but it has been put in its place by the EU's own institutions."
And Liberal Democrat MEP Catherine Bearder added: "This ruling is a victory for Britain, confirming we have a fair immigration system as well as having full access to the EU single market.
"The right to travel and work abroad is a two-way street. Around 1.2 million Brits choose to live in the EU. Leaving Europe will not only cost British jobs and push up prices, it will also reduce opportunities for Brits to live, work and study abroad."
The IPPR thinktank said the decision could prove awkward to the Vote Leave campaign, which has said that lawful EU residents in the UK would be granted indefinite leave to remain following Brexit - a status which would grant them more generous benefit rights than the ECJ ruling.
IPPR research fellow Marley Morris said: "This decision by the European Court of Justice is another sign - on top of other recent judgments - that it is becoming more sympathetic to the UK's interpretation of free movement rules.
"But there are no guarantees that this will last for ever, and future judgments may go against the UK. Conversely, a vote for Brexit on June 23 is likely to create its own legal quagmire, as the subsequent negotiations will have to resolve the free movement rights of EU citizens currently living in the UK and UK citizens currently living in other EU countries in a fair and consistent way."
A spokesman for HM Revenue and Customs said: "The UK welcomes the Court of Justice of the European Union's judgment, which supports our view that we are entitled to ensure only EU migrants who have a right to be in the UK can claim our benefits."