Homeless EU immigrants found to have no realistic prospect of finding work and likely to become a burden on the host state "should head home", said Roger Henderson, the deputy judge.
He ruled that Westminster City Council acted lawfully when officials refused to accommodate two European HIV victims earlier this year.
Gaudenzio Castelli, 35, an Italian, and Jose Tristan-Garcia, 32, a Spaniard, were not entitled to benefit from UK laws designed to help the homeless - even though they had entered the country lawfully as EU citizens, the judge said.
At the time the council made its decision in early summer both men were unemployed with no independent means and apparently no prospect of finding work.
As a result, neither had any right under EU law to remain in the UK, said the judge.
The landmark ruling will be welcomed by the Government as it fully supports social security guidelines established last year to curb so-called "benefit tourists".
Hugh McGeever, Westminster's housing demand manager, welcomed the judge's clarification of the law and said it could possibly affect hundreds of EU nationals and local authorities all over the country.
There were up to 12 similar cases in Westminster alone.
Judge Henderson said at the end of the hearing - unofficially estimated to have cost the legal aid fund and the city council at least pounds 50,000 - that the case had raised complex legal issues of national public importance.
He gave Mr Castelli and Mr Tristan-Garcia leave to appeal to the Court of Appeal. Westminster is reconsidering the case of Mr Tristan-Garcia as he has since found a job. Mr Castelli will continue to be granted temporary accommodation pending the outcome of the appeal.
Nick Partridge, chief executive of the Aids charity the Terrence Higgins Trust, said of the decision: "It is disgraceful that a man with a life- threatening illness has been released from hospital only to face life on the street."
The charity had backed the two men's unsuccessful application for a judicial review of the council's refusal to house them.
Mr Partridge said: "It is shocking that councils may no longer have a responsibility for vulnerable, ill people.
"This decision represents a further erosion by the courts of homeless people's rights to social housing. This is a particularly worrying development for people living with HIV, for whom a decent home is essential."
Both Mr Castelli and Mr Tristan-Garcia claimed Westminster Council's refusal to house them was in breach of its statutory duty under the 1985 Housing Act because of their medical condition.
Mr Castelli, a flower seller from Bergamo, first arrived in the UK in March 1994, hoping to start a plastic recycling business in London.
He told the council that he was frustrated by his failure to achieve his ambition, became involved in drug taking, and after his pounds 3,000 capital ran out, lived off his friends and charity from a church. His health deteriorated and he went into hospital several times.
The judge said Mr Tristan-Garcia came to England from Almeria in 1993, living with an uncle in Walthamstow, north-east London, and working as an office cleaner and barman.
From February 1994, he had no paid employment and obtained income support and housing benefit, plus a disability living allowance because of difficulties with walking.
The Home Office Immigration and Nationality Department wrote to both men saying it was not satisfied that they were "lawfully resident" because they were not employed or "seeking work with a genuine chance of obtaining work".Reuse content