In a landmark judgement, the EU's top court confirmed the UK can restrict child benefit and child tax credit for arrivals from the EU using a "right to reside test."
The Luxembourg-based court rejected a challenge to the UK's case from the European Commission.
It had questioned whether the UK's decision to introduce a "right to reside" test for a number of social security benefits such as child benefit, child tax credit and income support was discriminatory.
However, the court, in its ruling on Tuesday, upheld the UK's argument that non-active people coming to the UK must have sufficient resources to not become a burden on the UK's social security schemes.
In dismissing the Commission's action, the court said it rejects its "principal argument" that the UK legislation imposes a condition supplementing that of habitual residence contained in the regulation.
In a statement, the court states that "there is nothing to prevent the grant of social benefits to EU citizens who are not economically active being made subject to the requirement that those citizens fulfil the conditions for possessing a right to reside lawfully in the host member state."
The ECJ also said it holds that the condition requiring a right to reside in the UK "gives rise to unequal treatment."
The judges consider that this difference in treatment can be justified by a legitimate objective such as the need to protect the finances of the host member state.
"It follows that the condition does not go beyond what is necessary to attain the legitimate objective pursued by the UK, namely the need to protect its finances," said the court.
"However, the court considers that this difference in treatment can be justified by a legitimate objective such as the need to protect the finances of the host member state, provided that it does not go beyond what is necessary to attain that objective.
"In this regard, the court finds that the UK authorities verify whether residence is lawful in accordance with the conditions laid down in the directive on the free movement of citizens.
"Thus, this verification is not carried out systematically by the UK authorities for each claim, but only in the event of doubt. It follows that the condition does not go beyond what is necessary to attain the legitimate objective pursued by the UK, namely the need to protect its finances."
Reaction was swift, with UK Conservative employment spokesperson in the European Parliament Anthea McIntyre saying she welcomed the ruling.
McIntyre said: "This is a victory for the UK and for common sense. It vindicates the Prime Minister's renegotiation 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."
Further comment came from ALDE group deputy Catherine Bearder who said, "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."